Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tools/scripts/tsc/generate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down
42 changes: 42 additions & 0 deletions tsc/internal/execute/tsctests/showconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"} {
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/tsoptions/options_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions tsc/internal/tsoptions/showconfig.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
97 changes: 97 additions & 0 deletions tsc/internal/tsoptions/showconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
]
}
Original file line number Diff line number Diff line change
@@ -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"
]
}