Skip to content

Fix crash locating a content mapper option diagnostic inherited through extends - #65652

Open
Ben Snyder (benpsnyder) wants to merge 2 commits into
microsoft:mainfrom
benpsnyder:fix/content-mapper-option-diagnostic-location
Open

Ben Snyder (benpsnyder) wants to merge 2 commits into
microsoft:mainfrom
benpsnyder:fix/content-mapper-option-diagnostic-location

Conversation

@benpsnyder

Copy link
Copy Markdown

Fixes #65651

Analysis

GetContentMapperOptionDiagnosticLocation looks for the contentMappers syntax only in the project's own config file. When the entry is inherited through extends, that file has no contentMappers property:

// tsconfig.base.json
{ "contentMappers": [{ "package": "fake-mapper", "extensions": [".fake"], "options": { "a": 1 } }] }
// tsconfig.json
{ "extends": "./tsconfig.base.json" }

so getContentMapperSyntax returns nil for both the mapper and its options. When the mapper answers openProject with an option diagnostic whose path is not empty (here ["a"]), the walk over the path calls ast.IsObjectLiteralExpression(nil) (or ast.IsArrayLiteralExpression(nil) for an index segment) and tsc --runExternalCode panics. With an empty path nothing is dereferenced, but the diagnostic has no location.

The parser already records which config file declared the setting (parsedConfig.contentMapperSourceFile) and uses it for every content mapper diagnostic it reports while parsing. That file was not kept on the ParsedCommandLine, so this later lookup, made when the program collects the mapper's option diagnostics, could not use it.

Fix

Keep the declaring config file on the ParsedCommandLine (set where it is built, copied by WithFileNames and ReloadFileNamesOfParsedCommandLine) and look the option up there, falling back to the project's own file. An option diagnostic for an inherited entry now points at the option in the base config (tsconfig.base.json(1,93): error fake1: Option 'a' must be a string. for the example above), as the parser's own content mapper diagnostics already do. The existing nil check now runs before the path walk, so a config with no contentMappers syntax gets the diagnostic without a location instead of a panic.

Alternative considered: dropping the location for an inherited entry, reporting the diagnostic with no file. That also removes the crash, but it discards the location the mapper asked for when the base config's syntax is already available.

Tests:

  • TestContentMapperOptionDiagnosticLocationInheritedFromExtendedConfig: a base config in another directory, with a nested option, an array element, an empty path, and a mapper not in the config. Each asserts the file and the exact text of the range.
  • TestContentMapperOptionDiagnosticLocationIsInDeclaringConfig: an entry inherited through two levels of extends, one from an earlier entry of an extends list, and an entry the child overrides, whose diagnostic must stay in the child's tsconfig.json.
  • TestContentMapperOptionDiagnosticLocationWithoutContentMappersSyntax: property and index segments when no config has contentMappers syntax.
  • TestTscContentMapperOptionDiagnosticsFromExtendedConfig, with a new baseline: the full tsc run, which reports ../base/tsconfig.base.json(6,48): error mapper123: Option 'name' requires a string.

Without the fix, every new case panics except two: the child override passes, and the empty path returns no file. The tsc test panics in GetContentMapperOptionDiagnosticLocation, as in the issue.

Not changed: the mapper's position is still found with slices.Index(config.ContentMappers(), mapper), which is not its position in the contentMappers array when an earlier entry was dropped. With [{ "package": "missing-mapper", "extensions": [".other"] }, { "package": "fake-mapper", "extensions": [".fake"], "options": { "a": 1 } }] in tsconfig.json, the diagnostic for a is reported at the first entry, before and after this change. That is a wrong location, not a crash, and is left for a separate change.

Copilot Checklist

I successfully ran the applicable command at the end of my session, and it completed without error:

  • npx hereby validate
  • npx hereby validate --api (for TypeScript API changes)

Not ticked: npx hereby validate failed only in test:tsc, where internal/fswatch and internal/lsp/lspwatcher tests timed out waiting for file events on this machine, and the same failure occurs on unmodified main at 88071f4.

…gh 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 <noreply@anthropic.com>
…s 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 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change to locate content mapper diagnostics in the configuration that declared them.

No open issues 🎉

What changed in this PR

Fixes #65651 by making the compiler locate content mapper option diagnostics in the config that declared contentMappers, including when inherited through extends, rather than crashing.

Changes:

  • Preserve the declaring config file when creating or copying parsed configurations.
  • Use that file for diagnostic locations and return no location safely when syntax is missing.
  • Add regression tests for inheritance, child overrides, nested options, and command-line diagnostic output.
File Description
tsc/​testdata/​baselines/​reference/​tsc/​contentMapperOptionDiagnostics/​mapper-option-diagnostic-from-extended-config-in-another-directory.js Records the expected diagnostic location in the base config.
tsc/​internal/​tsoptions/​tsconfigparsing.go Retains the declaring file and guards diagnostic lookup against missing syntax.
tsc/​internal/​tsoptions/​tsconfigparsing_test.go Tests inherited locations, overrides, option paths, and missing syntax.
tsc/​internal/​tsoptions/​parsedcommandline.go Stores and preserves the declaring file across file-name updates.
tsc/​internal/​execute/​tsctests/​tsc_test.go Adds an end-to-end regression test using a base config in another directory.

Review effort: Balanced

@benpsnyder

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative considered: dropping the location for an inherited entry, reporting the diagnostic with no file. That also removes the crash, but it discards the location the mapper asked for when the base config's syntax is already available.

I know it's not ideal, but I think we want to go with this approach for now. This is how every other program-time config error is reported when the offending location comes from an extended config. The current approach clashes with how project diagnostics are published in the LSP (they always go to project's own tsconfig.json). If we're going to improve on this, we should do it across the board for program diagnostics on extended configs.

@github-project-automation github-project-automation Bot moved this from Not started to Waiting on author in PR Backlog Nov 25, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Waiting on author

Development

Successfully merging this pull request may close these issues.

tsc panics when a content mapper reports an option diagnostic for a contentMappers entry inherited through extends

3 participants