From 7c72e5d51354e630f12c01ec79347be47d95780e Mon Sep 17 00:00:00 2001 From: Sumit Date: Sat, 5 Dec 2026 12:55:37 +0530 Subject: [PATCH] fix(55825): rebase inherited paths in --showConfig Serialize paths relative to the consuming config when PathsBasePath differs, so extended mappings match runtime resolution. --- tools/scripts/tsc/generate-options.ts | 5 + .../execute/tsctests/showconfig_test.go | 42 ++++++++ tsc/internal/tsoptions/options_generated.go | 2 +- tsc/internal/tsoptions/showconfig.go | 59 +++++++++++ tsc/internal/tsoptions/showconfig_test.go | 97 +++++++++++++++++++ ...th-inherited-paths-from-extended-config.js | 40 ++++++++ ...rited-paths-from-parent-extended-config.js | 33 +++++++ 7 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-extended-config.js create mode 100644 tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-parent-extended-config.js diff --git a/tools/scripts/tsc/generate-options.ts b/tools/scripts/tsc/generate-options.ts index ff734be30d94c..32449a4470aae 100644 --- a/tools/scripts/tsc/generate-options.ts +++ b/tools/scripts/tsc/generate-options.ts @@ -360,6 +360,11 @@ ${ const declaration = option.declaration!; let value = field; let condition = `${field} != ${zeroValue(option)}`; + if (option.name === "paths") { + return `if ${condition} { + result.Set(${name}, serializeCompilerOptionPaths(${field}, options.PathsBasePath, configFilePath, caseSensitivity)) +}`; + } switch (optionKind(option)) { case "Enum": return `if ${condition} { diff --git a/tsc/internal/execute/tsctests/showconfig_test.go b/tsc/internal/execute/tsctests/showconfig_test.go index a8a8bd8f3e3c0..dc4ecd01b1b47 100644 --- a/tsc/internal/execute/tsctests/showconfig_test.go +++ b/tsc/internal/execute/tsctests/showconfig_test.go @@ -266,6 +266,48 @@ func TestShowConfig(t *testing.T) { }, commandLineArgs: []string{"-p", "tsconfig.json", "--showConfig"}, }, + { + // https://github.com/microsoft/TypeScript/issues/55825 + subScenario: "Show TSConfig with inherited paths from extended config", + files: FileMap{ + "/home/src/workspaces/project/file.ts": `export const a = 1;`, + "/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(` + { + "extends": "./this-path-should-be-in-config/tsconfig.json" + }`), + "/home/src/workspaces/project/this-path-should-be-in-config/tsconfig.json": stringtestutil.Dedent(` + { + "compilerOptions": { + "paths": { + "foo": ["./bar"], + "@lib/*": ["./lib/*"] + } + } + }`), + "/home/src/workspaces/project/this-path-should-be-in-config/bar.ts": `export const bar = 1;`, + }, + commandLineArgs: []string{"-p", "tsconfig.json", "--showConfig"}, + }, + { + subScenario: "Show TSConfig with inherited paths from parent extended config", + files: FileMap{ + "/home/src/workspaces/project/pkg/file.ts": `export const a = 1;`, + "/home/src/workspaces/project/pkg/tsconfig.json": stringtestutil.Dedent(` + { + "extends": "../tsconfig.base.json" + }`), + "/home/src/workspaces/project/tsconfig.base.json": stringtestutil.Dedent(` + { + "compilerOptions": { + "paths": { + "@/*": ["./src/*"] + } + } + }`), + }, + cwd: "/home/src/workspaces/project/pkg", + commandLineArgs: []string{"-p", "tsconfig.json", "--showConfig"}, + }, } for _, value := range []string{"true", "false"} { diff --git a/tsc/internal/tsoptions/options_generated.go b/tsc/internal/tsoptions/options_generated.go index 2ddffdef901f8..e7757a31a3a39 100644 --- a/tsc/internal/tsoptions/options_generated.go +++ b/tsc/internal/tsoptions/options_generated.go @@ -1910,7 +1910,7 @@ func serializeCompilerOptions(options *core.CompilerOptions, configFilePath tspa result.Set("outDir", serializeCompilerOptionPath(options.OutDir.AsPath(), configFilePath, caseSensitivity)) } if options.Paths != nil { - result.Set("paths", options.Paths) + result.Set("paths", serializeCompilerOptionPaths(options.Paths, options.PathsBasePath, configFilePath, caseSensitivity)) } if options.Plugins != nil { result.Set("plugins", options.Plugins) diff --git a/tsc/internal/tsoptions/showconfig.go b/tsc/internal/tsoptions/showconfig.go index 453264cbf3038..0b25c240705bc 100644 --- a/tsc/internal/tsoptions/showconfig.go +++ b/tsc/internal/tsoptions/showconfig.go @@ -1,6 +1,8 @@ package tsoptions import ( + "strings" + "github.com/microsoft/TypeScript/tsc/internal/collections" "github.com/microsoft/TypeScript/tsc/internal/core" "github.com/microsoft/TypeScript/tsc/internal/tspath" @@ -158,6 +160,63 @@ func serializeCompilerOptionPath(value tspath.RootedPath, configFilePath tspath. return value.AsString() } +// serializeCompilerOptionPaths rewrites inherited path mappings relative to the consuming +// config when PathsBasePath differs from that config's directory. Entries remain relative to +// PathsBasePath; same-directory mappings are returned unchanged. +func serializeCompilerOptionPaths( + paths *collections.OrderedMap[string, []string], + pathsBasePath tspath.RootedDirectoryPath, + configFilePath tspath.RootedFilePath, + caseSensitivity tspath.CaseSensitivity, +) *collections.OrderedMap[string, []string] { + if paths == nil { + return nil + } + configDir := configFilePath.Directory() + if pathsBasePath == "" || pathsBasePath == configDir { + return paths + } + relativeBase := tspath.ConvertToRelativePath(pathsBasePath.AsString(), configDir, caseSensitivity) + if relativeBase == "" { + // PathsBasePath and configDir refer to the same directory (including case-only + // differences on case-insensitive hosts); leave mappings unchanged. + return paths + } + result := collections.NewOrderedMapWithSizeHint[string, []string](paths.Size()) + for key, values := range paths.Entries() { + rewritten := make([]string, len(values)) + for i, value := range values { + rewritten[i] = rebaseCompilerOptionPathMapping(value, relativeBase) + } + result.Set(key, rewritten) + } + return result +} + +// rebaseCompilerOptionPathMapping relocates a single paths entry from PathsBasePath into the +// consuming config directory. Absolute entries are left alone. The prefix before the first '*' +// is normalized; the '*' and any following segments are appended unchanged so substitution +// semantics (including patterns like "*/../x") match runtime resolution. +func rebaseCompilerOptionPathMapping(value string, relativeBase string) string { + if value == "" || tspath.GetEncodedRootLength(value) != 0 { + return value + } + star := strings.IndexByte(value, '*') + prefix, suffix := value, "" + if star >= 0 { + prefix, suffix = value[:star], value[star:] + } + hadTrailingSep := prefix != "" && tspath.HasTrailingDirectorySeparator(prefix) + normalized := tspath.NormalizePath(tspath.CombinePaths(relativeBase, prefix)) + if hadTrailingSep && !tspath.HasTrailingDirectorySeparator(normalized) { + normalized += "/" + } + if prefix == "" && suffix != "" && !tspath.HasTrailingDirectorySeparator(normalized) { + normalized += "/" + } + return tspath.EnsurePathIsNonModuleName(normalized + suffix) +} + func serializeCompilerOptionEnumList(values []string, enumMap *collections.OrderedMap[string, any]) []string { result := make([]string, len(values)) for i, value := range values { diff --git a/tsc/internal/tsoptions/showconfig_test.go b/tsc/internal/tsoptions/showconfig_test.go index 3c4ca7458830e..851cc3bc52347 100644 --- a/tsc/internal/tsoptions/showconfig_test.go +++ b/tsc/internal/tsoptions/showconfig_test.go @@ -31,6 +31,10 @@ func reflectedSerializeCompilerOptions(options *core.CompilerOptions, configFile continue } value := fieldValue.Interface() + if option.Name == "paths" { + result.Set(option.Name, serializeCompilerOptionPaths(options.Paths, options.PathsBasePath, configFilePath, caseSensitivity)) + continue + } if enumMap := option.EnumMap(); enumMap != nil { for key, enumValue := range enumMap.Entries() { if reflect.ValueOf(enumValue).Int() == fieldValue.Int() { @@ -171,6 +175,99 @@ func TestShowConfigSerialization(t *testing.T) { Plugins: []core.PluginImport{{Name: "plugin"}}, MaxNodeModuleJsDepth: new(0), }) + check(t, &core.CompilerOptions{ + Paths: paths, + PathsBasePath: tspath.RootedDirectoryPath("/project/this-path-should-be-in-config"), + }) +} + +func TestSerializeCompilerOptionPaths(t *testing.T) { + t.Parallel() + + configFilePath := tspath.RootedFilePath("/project/tsconfig.json") + caseSensitivity := tspath.CaseSensitive + configDir := configFilePath.Directory() + + t.Run("unchanged when PathsBasePath matches config dir", func(t *testing.T) { + t.Parallel() + paths := &collections.OrderedMap[string, []string]{} + paths.Set("foo", []string{"./bar", "./lib/"}) + got := serializeCompilerOptionPaths(paths, configDir, configFilePath, caseSensitivity) + if got != paths { + t.Fatalf("expected original paths map identity when base matches config dir") + } + }) + + t.Run("unchanged when PathsBasePath empty", func(t *testing.T) { + t.Parallel() + paths := &collections.OrderedMap[string, []string]{} + paths.Set("foo", []string{"./bar"}) + got := serializeCompilerOptionPaths(paths, "", configFilePath, caseSensitivity) + if got != paths { + t.Fatalf("expected original paths map identity when PathsBasePath is empty") + } + }) + + t.Run("rebases relative to extended subdirectory", func(t *testing.T) { + t.Parallel() + paths := &collections.OrderedMap[string, []string]{} + paths.Set("foo", []string{"./bar", "./lib/", "./*/../x", "*", "./*", "src/*", "src*", "/abs/x", ""}) + got := serializeCompilerOptionPaths( + paths, + tspath.RootedDirectoryPath("/project/this-path-should-be-in-config"), + configFilePath, + caseSensitivity, + ) + want := map[string][]string{ + "foo": { + "./this-path-should-be-in-config/bar", + "./this-path-should-be-in-config/lib/", + "./this-path-should-be-in-config/*/../x", + "./this-path-should-be-in-config/*", + "./this-path-should-be-in-config/*", + "./this-path-should-be-in-config/src/*", + "./this-path-should-be-in-config/src*", + "/abs/x", + "", + }, + } + for key, expected := range want { + actual := got.GetOrZero(key) + if !slices.Equal(actual, expected) { + t.Errorf("%s: got %#v, want %#v", key, actual, expected) + } + } + }) + + t.Run("rebases relative to parent extended config", func(t *testing.T) { + t.Parallel() + paths := &collections.OrderedMap[string, []string]{} + paths.Set("@/*", []string{"./src/*"}) + got := serializeCompilerOptionPaths( + paths, + tspath.RootedDirectoryPath("/project"), + tspath.RootedFilePath("/project/pkg/tsconfig.json"), + caseSensitivity, + ) + if actual := got.GetOrZero("@/*"); !slices.Equal(actual, []string{"../src/*"}) { + t.Errorf("@/*: got %#v, want []string{\"../src/*\"}", actual) + } + }) + + t.Run("unchanged when PathsBasePath differs only by case", func(t *testing.T) { + t.Parallel() + paths := &collections.OrderedMap[string, []string]{} + paths.Set("a", []string{"*", "./bar", "./lib/"}) + got := serializeCompilerOptionPaths( + paths, + tspath.RootedDirectoryPath("/Project"), + tspath.RootedFilePath("/project/tsconfig.json"), + tspath.CaseInsensitive, + ) + if got != paths { + t.Fatalf("expected original paths map identity for case-only PathsBasePath difference, got %#v", got.GetOrZero("a")) + } + }) } func TestShowConfigEnumSerialization(t *testing.T) { diff --git a/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-extended-config.js b/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-extended-config.js new file mode 100644 index 0000000000000..224a4924ac5f3 --- /dev/null +++ b/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-extended-config.js @@ -0,0 +1,40 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/project/file.ts] *new* +export const a = 1; +//// [/home/src/workspaces/project/this-path-should-be-in-config/bar.ts] *new* +export const bar = 1; +//// [/home/src/workspaces/project/this-path-should-be-in-config/tsconfig.json] *new* +{ + "compilerOptions": { + "paths": { + "foo": ["./bar"], + "@lib/*": ["./lib/*"] + } + } +} +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ + "extends": "./this-path-should-be-in-config/tsconfig.json" +} + +tsc -p tsconfig.json --showConfig +ExitStatus:: Success +Output:: +{ + "compilerOptions": { + "paths": { + "foo": [ + "./this-path-should-be-in-config/bar" + ], + "@lib/*": [ + "./this-path-should-be-in-config/lib/*" + ] + } + }, + "files": [ + "./file.ts", + "./this-path-should-be-in-config/bar.ts" + ] +} diff --git a/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-parent-extended-config.js b/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-parent-extended-config.js new file mode 100644 index 0000000000000..1c489e0c06707 --- /dev/null +++ b/tsc/testdata/baselines/reference/tsc/showConfig/Show-TSConfig-with-inherited-paths-from-parent-extended-config.js @@ -0,0 +1,33 @@ +currentDirectory::/home/src/workspaces/project/pkg +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/project/pkg/file.ts] *new* +export const a = 1; +//// [/home/src/workspaces/project/pkg/tsconfig.json] *new* +{ + "extends": "../tsconfig.base.json" +} +//// [/home/src/workspaces/project/tsconfig.base.json] *new* +{ + "compilerOptions": { + "paths": { + "@/*": ["./src/*"] + } + } +} + +tsc -p tsconfig.json --showConfig +ExitStatus:: Success +Output:: +{ + "compilerOptions": { + "paths": { + "@/*": [ + "../src/*" + ] + } + }, + "files": [ + "./file.ts" + ] +}