Skip to content

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

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

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

Conversation

@benpsnyder

@benpsnyder Ben Snyder (benpsnyder) commented Nov 24, 2026 •

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, and the diagnostic already had no location.

Fix

The existing node == nil check now runs before the path walk. An option diagnostic for an entry inherited through extends is reported without a location (error fake1: Option 'a' must be a string. for the example above), the way createCompilerOptionsDiagnostic in program.go reports other program-time config errors whose syntax is in an extended config. A contentMappers entry the project's own tsconfig.json declares still gets its location there. Better locations for diagnostics from extended configs are left for a change that covers all program diagnostics.

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 expects no file and an undefined range.
  • TestContentMapperOptionDiagnosticLocationWithExtends: an entry inherited through two levels of extends and one from an earlier entry of an extends list, both with no location, and an entry the child overrides, whose diagnostic stays 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 error mapper123: Option 'name' requires a string.

With the check moved back below the loop, every case with a non-empty path panics; the empty path and the child override pass. 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)

…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
A contentMappers entry inherited through extends has no syntax in the
project's own tsconfig.json, so its option diagnostic now has no file,
as other program-time config errors from an extended config do. The
nil check moves above the option path walk, which crashed on the
missing node. This drops the declaring config file the PR had stored
on ParsedCommandLine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@benpsnyder

Copy link
Copy Markdown
Author

Changed in 91d7614: an option diagnostic for a contentMappers entry inherited through extends now has no location, as createCompilerOptionsDiagnostic does for other program-time config errors. An entry the child config declares itself still points into its tsconfig.json.

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