From 5781002caa68f860048aee84e3f9b7defde52121 Mon Sep 17 00:00:00 2001 From: Ben Snyder Date: Mon, 23 Nov 2026 21:59:53 -0500 Subject: [PATCH 1/2] Fix crash locating a content mapper option diagnostic inherited through extends GetContentMapperOptionDiagnosticLocation looked for the contentMappers syntax only in the project's own tsconfig.json. When the entry was inherited through "extends", no syntax was found, the node stayed nil, and walking a non-empty option path dereferenced it. Look in the config file that declared the setting, which the parser already tracks for its own content mapper diagnostics, so the diagnostic points at the option in the base config. Return no location instead of walking a nil node when no syntax exists at all. Co-Authored-By: Claude Opus 5.5 --- tsc/internal/execute/tsctests/tsc_test.go | 23 +++++ tsc/internal/tsoptions/parsedcommandline.go | 5 ++ tsc/internal/tsoptions/tsconfigparsing.go | 19 ++-- .../tsoptions/tsconfigparsing_test.go | 90 +++++++++++++++++++ ...om-extended-config-in-another-directory.js | 52 +++++++++++ 5 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js diff --git a/tsc/internal/execute/tsctests/tsc_test.go b/tsc/internal/execute/tsctests/tsc_test.go index ee429a826f94a..4b8ac3f5891e8 100644 --- a/tsc/internal/execute/tsctests/tsc_test.go +++ b/tsc/internal/execute/tsctests/tsc_test.go @@ -5519,6 +5519,29 @@ func TestTscContentMapperOptionDiagnostics(t *testing.T) { }).run(t, "contentMapperOptionDiagnostics") } +func TestTscContentMapperOptionDiagnosticsFromExtendedConfig(t *testing.T) { + t.Parallel() + (&tscInput{ + subScenario: "mapper option diagnostic from extended config in another directory", + files: FileMap{ + "/home/src/workspaces/base/tsconfig.base.json": stringtestutil.Dedent(` + { + "contentMappers": [ + { + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + } + ] + }`), + "/home/src/workspaces/project/tsconfig.json": `{ "extends": "../base/tsconfig.base.json" }`, + "/home/src/workspaces/project/app.vue": `export const value = 1;`, + "/home/src/workspaces/node_modules/mapper/package.json": contentmappertest.PackageJSON(contentmappertest.DynamicVerbatimMapper), + }, + commandLineArgs: []string{"--runExternalCode", "--pretty", "false"}, + }).run(t, "contentMapperOptionDiagnostics") +} + func TestTscContentMapperFailures(t *testing.T) { t.Parallel() failMapperPackageJSON := stringtestutil.Dedent(` diff --git a/tsc/internal/tsoptions/parsedcommandline.go b/tsc/internal/tsoptions/parsedcommandline.go index 8a116bf3fcb46..2d850750bdc77 100644 --- a/tsc/internal/tsoptions/parsedcommandline.go +++ b/tsc/internal/tsoptions/parsedcommandline.go @@ -76,6 +76,9 @@ type ParsedCommandLine struct { contentMapperExtensionRewritesOnce sync.Once contentMapperFileMatcherOnce sync.Once contentMapperFileMatcher *contentmapper.FileMatcher + // contentMapperSourceFile is the config file whose "contentMappers" setting is in effect: + // this config's own file, or the extended config the setting was inherited from. + contentMapperSourceFile *ast.SourceFile locale locale.Locale localeOnce sync.Once @@ -115,6 +118,7 @@ func (p *ParsedCommandLine) WithFileNames(fileNames []tspath.RootedFilePath) *Pa includeGlobs: p.includeGlobs, literalFileNamesLen: p.literalFileNamesLen, rootFileNamesForDiagnostics: p.rootFileNamesForDiagnostics, + contentMapperSourceFile: p.contentMapperSourceFile, } } @@ -586,6 +590,7 @@ func (p *ParsedCommandLine) ReloadFileNamesOfParsedCommandLine(fs vfs.FS) *Parse includeGlobs: p.includeGlobs, literalFileNamesLen: literalFileNamesLen, rootFileNamesForDiagnostics: p.rootFileNamesForDiagnostics, + contentMapperSourceFile: p.contentMapperSourceFile, } return &parsedCommandLine } diff --git a/tsc/internal/tsoptions/tsconfigparsing.go b/tsc/internal/tsoptions/tsconfigparsing.go index 92ba6b48d09ad..89d3fd9c504b7 100644 --- a/tsc/internal/tsoptions/tsconfigparsing.go +++ b/tsc/internal/tsoptions/tsconfigparsing.go @@ -1615,6 +1615,8 @@ func parseJsonConfigFileContentWorker( baseDirectory: basePathForFileNames, caseSensitivity: fs.CaseSensitivity(), literalFileNamesLen: literalFileNamesLen, + + contentMapperSourceFile: contentMapperSourceFile, } } @@ -1801,12 +1803,21 @@ func GetContentMapperOptionDiagnosticLocation(config *ParsedCommandLine, mapper if config == nil || config.ConfigFile == nil { return nil, core.UndefinedTextRange() } + // The "contentMappers" setting may be inherited through "extends", so look for its syntax in the + // config file that declared it, as the diagnostics reported while parsing it do. + file := config.contentMapperSourceFile + if file == nil { + file = config.ConfigFile.SourceFile + } index := slices.Index(config.ContentMappers(), mapper) - mapperNode := getContentMapperSyntax(config.ConfigFile.SourceFile, index, "") - node := getContentMapperSyntax(config.ConfigFile.SourceFile, index, "options") + mapperNode := getContentMapperSyntax(file, index, "") + node := getContentMapperSyntax(file, index, "options") if node == nil { node = mapperNode } + if node == nil { + return nil, core.UndefinedTextRange() + } for _, segment := range path { var next *ast.Node switch { @@ -1825,10 +1836,6 @@ func GetContentMapperOptionDiagnosticLocation(config *ParsedCommandLine, mapper } node = next } - if node == nil { - return nil, core.UndefinedTextRange() - } - file := config.ConfigFile.SourceFile return file, core.NewTextRange(scanner.SkipTrivia(file.Text(), node.Pos()), node.End()) } diff --git a/tsc/internal/tsoptions/tsconfigparsing_test.go b/tsc/internal/tsoptions/tsconfigparsing_test.go index 5c66aa449b4be..80853087484a7 100644 --- a/tsc/internal/tsoptions/tsconfigparsing_test.go +++ b/tsc/internal/tsoptions/tsconfigparsing_test.go @@ -1398,6 +1398,96 @@ func TestContentMapperOptionDiagnosticLocation(t *testing.T) { assert.Equal(t, file.Text()[loc.Pos():loc.End()], "1") } +func TestContentMapperOptionDiagnosticLocationInheritedFromExtendedConfig(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: `{ "extends": "../base/tsconfig.base.json" }`, + configFileName: "tsconfig.json", + basePath: "/project", + allFileList: map[string]string{ + "/base/tsconfig.base.json": `{ + "contentMappers": [{ + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + }] + }`, + "/project/index.ts": "export {};", + "/node_modules/mapper/package.json": `{ "name": "mapper", "version": "1.0.0", "typescript": { "contentMapper": { "exec": ["mapper"] } } }`, + }, + existingOptions: &core.CompilerOptions{RunExternalCode: core.TSTrue}, + } + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + assert.Equal(t, len(parsed.Errors), 0) + assert.Equal(t, len(parsed.ContentMappers()), 1) + + for name, test := range map[string]struct { + mapper *contentmapper.Mapper + path []contentmapper.OptionPathSegment + want string + }{ + "nested option": { + mapper: parsed.ContentMappers()[0], + path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}, {Property: "name"}}, + want: "1", + }, + "array element": { + mapper: parsed.ContentMappers()[0], + path: []contentmapper.OptionPathSegment{{Property: "plugins"}, {Index: 0, IsIndex: true}}, + want: `{ "name": 1 }`, + }, + "options object": { + mapper: parsed.ContentMappers()[0], + want: `{ "plugins": [{ "name": 1 }] }`, + }, + "unknown mapper": { + mapper: &contentmapper.Mapper{}, + path: []contentmapper.OptionPathSegment{{Property: "plugins"}}, + want: `[{ + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + }]`, + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, test.mapper, test.path) + assert.Assert(t, file != nil) + assert.Equal(t, file.FileName(), tspath.RootedFilePath("/base/tsconfig.base.json")) + assert.Equal(t, file.Text()[loc.Pos():loc.End()], test.want) + }) + } +} + +func TestContentMapperOptionDiagnosticLocationWithoutContentMappersSyntax(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: `{}`, + configFileName: "tsconfig.json", + basePath: "/", + allFileList: map[string]string{ + "/index.ts": "export {};", + }, + } + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + mapper := &contentmapper.Mapper{} + parsed.ParsedConfig.ContentMappers = []*contentmapper.Mapper{mapper} + for name, path := range map[string][]contentmapper.OptionPathSegment{ + "property": {{Property: "plugins"}}, + "index": {{Index: 0, IsIndex: true}}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, mapper, path) + assert.Assert(t, file == nil) + assert.Equal(t, loc, core.UndefinedTextRange()) + }) + } +} + func TestContentMappersAreInheritedFromExtendedConfig(t *testing.T) { t.Parallel() config := testConfig{ diff --git a/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js b/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js new file mode 100644 index 0000000000000..52cc168d820b4 --- /dev/null +++ b/tsc/testdata/baselines/reference/tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js @@ -0,0 +1,52 @@ +currentDirectory::/home/src/workspaces/project +useCaseSensitiveFileNames::true +Input:: +//// [/home/src/workspaces/base/tsconfig.base.json] *new* +{ + "contentMappers": [ + { + "package": "mapper", + "extensions": [".vue"], + "options": { "plugins": [{ "name": 1 }] } + } + ] +} +//// [/home/src/workspaces/node_modules/mapper/package.json] *new* +{ + "name": "mapper", + "version": "1.0.0", + "typescript": { "contentMapper": { "exec": ["dynamic-verbatim-mapper"], "dynamicConfig": true } } +} +//// [/home/src/workspaces/project/app.vue] *new* +export const value = 1; +//// [/home/src/workspaces/project/tsconfig.json] *new* +{ "extends": "../base/tsconfig.base.json" } + +tsc --runExternalCode --pretty false +ExitStatus:: DiagnosticsPresent_OutputsGenerated +Output:: +../base/tsconfig.base.json(6,48): error mapper123: Option 'name' requires a string. +//// [/home/src/tslibs/TS/Lib/lib.es2026.full.d.ts] *Lib* +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +interface SymbolConstructor { + (desc?: string | number): symbol; + for(name: string): symbol; + readonly toStringTag: symbol; +} +declare var Symbol: SymbolConstructor; +interface Symbol { + readonly [Symbol.toStringTag]: string; +} +declare const console: { log(msg: any): void; }; + From fb24fd0b6eaae469dfe5834d397b5e09822d6c83 Mon Sep 17 00:00:00 2001 From: Ben Snyder Date: Mon, 23 Nov 2026 22:53:43 -0500 Subject: [PATCH 2/2] Test content mapper option diagnostic locations through extends chains and overrides Cover an entry inherited through two levels of "extends" and from an earlier entry of an "extends" list, both of which panicked before the fix, and an entry the child config overrides, whose diagnostic must stay in the child's tsconfig.json. Co-Authored-By: Claude Opus 5.5 --- .../tsoptions/tsconfigparsing_test.go | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tsc/internal/tsoptions/tsconfigparsing_test.go b/tsc/internal/tsoptions/tsconfigparsing_test.go index 80853087484a7..f5a67e50521f7 100644 --- a/tsc/internal/tsoptions/tsconfigparsing_test.go +++ b/tsc/internal/tsoptions/tsconfigparsing_test.go @@ -1461,6 +1461,73 @@ func TestContentMapperOptionDiagnosticLocationInheritedFromExtendedConfig(t *tes } } +func TestContentMapperOptionDiagnosticLocationIsInDeclaringConfig(t *testing.T) { + t.Parallel() + contentMappers := func(name string) string { + return `"contentMappers": [{ "package": "mapper", "extensions": [".vue"], "options": { "plugins": [{ "name": "` + name + `" }] } }]` + } + for name, test := range map[string]struct { + jsonText string + files map[string]string + wantFile tspath.RootedFilePath + want string + }{ + "two levels of extends": { + jsonText: `{ "extends": "../config/middle.json" }`, + files: map[string]string{ + "/config/middle.json": `{ "extends": "./base.json" }`, + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + }, + wantFile: "/config/base.json", + want: `"base"`, + }, + "earlier entry of an extends list": { + jsonText: `{ "extends": ["../config/base.json", "../config/other.json"] }`, + files: map[string]string{ + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + "/config/other.json": `{ "compilerOptions": { "strict": true } }`, + }, + wantFile: "/config/base.json", + want: `"base"`, + }, + "overridden in the child": { + jsonText: `{ "extends": "../config/base.json", ` + contentMappers("child") + ` }`, + files: map[string]string{ + "/config/base.json": `{ ` + contentMappers("base") + ` }`, + }, + wantFile: "/project/tsconfig.json", + want: `"child"`, + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + config := testConfig{ + jsonText: test.jsonText, + configFileName: "tsconfig.json", + basePath: "/project", + allFileList: map[string]string{ + "/project/index.ts": "export {};", + "/node_modules/mapper/package.json": `{ "name": "mapper", "version": "1.0.0", "typescript": { "contentMapper": { "exec": ["mapper"] } } }`, + }, + existingOptions: &core.CompilerOptions{RunExternalCode: core.TSTrue}, + } + maps.Copy(config.allFileList, test.files) + fs := tsoptionstest.NewVFS(config.allFileList, tspath.CaseSensitive /*caseSensitivity*/) + parsed := getParsedWithJsonSourceFileApi(config, fs, config.basePath) + assert.Equal(t, len(parsed.Errors), 0) + assert.Equal(t, len(parsed.ContentMappers()), 1) + file, loc := tsoptions.GetContentMapperOptionDiagnosticLocation(parsed, parsed.ContentMappers()[0], []contentmapper.OptionPathSegment{ + {Property: "plugins"}, + {Index: 0, IsIndex: true}, + {Property: "name"}, + }) + assert.Assert(t, file != nil) + assert.Equal(t, file.FileName(), test.wantFile) + assert.Equal(t, file.Text()[loc.Pos():loc.End()], test.want) + }) + } +} + func TestContentMapperOptionDiagnosticLocationWithoutContentMappersSyntax(t *testing.T) { t.Parallel() config := testConfig{