Skip to content

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

Closed
benpsnyder wants to merge 2 commits into
mainfrom
fix/content-mapper-option-diagnostic-location
Closed

benpsnyder wants to merge 2 commits into
mainfrom
fix/content-mapper-option-diagnostic-location

Conversation

@benpsnyder

Copy link
Copy Markdown
Owner

No upstream issue: a search of microsoft/TypeScript and microsoft/typescript-go found no report of this crash, and none has been filed. This PR is on the fork only.

Analysis

tsc --runExternalCode panics with a nil pointer dereference when a content mapper answers openProject with an option diagnostic and the project's contentMappers entry is inherited through extends:

panic: runtime error: invalid memory address or nil pointer dereference
github.com/microsoft/TypeScript/tsc/internal/ast.IsObjectLiteralExpression(...)
github.com/microsoft/TypeScript/tsc/internal/tsoptions.GetContentMapperOptionDiagnosticLocation(...)
github.com/microsoft/TypeScript/tsc/internal/compiler.(*Program).collectContentMapperOptionDiagnostics.func1(...)

Minimal reproduction (found on typescript@7.1.0-beta, reproduced on main at 88071f4 with a locally built tsc):

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

The mapper answers openProject with optionDiagnostics: [{ path: ["a"], messageText: "bad option a", code: 9002 }]. Running tsc --noEmit --runExternalCode in app panics. With the same entry written in app/tsconfig.json itself, the diagnostic is reported at tsconfig.json(2,94); with no option diagnostic, the project checks clean.

Root cause: GetContentMapperOptionDiagnosticLocation looks for the contentMappers syntax only in config.ConfigFile.SourceFile, the project's own file. For an inherited entry that file has no contentMappers property, so getContentMapperSyntax returns nil for both the mapper and its options, and the loop over a non-empty option path calls ast.IsObjectLiteralExpression(nil) (or ast.IsArrayLiteralExpression(nil) for an index segment). The parser already records which config file declared the setting (parsedConfig.contentMapperSourceFile) and uses it for every content mapper diagnostic it reports itself, but that file was not kept on the ParsedCommandLine, so this later lookup could not use it.

Inputs that reach the nil, all with a non-empty path:

  • an entry inherited through extends (the reported case);
  • a mapper not in config.ContentMappers() (slices.Index returns -1) when the entry is inherited. With the entry in the project's own file, -1 resolves to the contentMappers array and did not crash;
  • a ParsedCommandLine whose config file has no contentMappers syntax at all but has mappers set.

With an empty path the nil was not dereferenced, but the diagnostic for an inherited entry lost its location.

Fix

Keep the config file that declared contentMappers on the ParsedCommandLine (set where it is built, copied by WithFileNames and ReloadFileNamesOfParsedCommandLine) and look up the option 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, as the parser's own content mapper diagnostics already do (../base/tsconfig.base.json(2,94): error fake9002: bad option a). The existing nil check now runs before the path walk, so a config with no contentMappers syntax gets a diagnostic without a location instead of a panic. The diagnostic is still always reported.

Alternative considered: attributing the diagnostic to the child's extends property. It is less precise, and pointing into the base config matches how the other content mapper diagnostics already treat inherited entries.

Not changed: the mapper's position is still found with slices.Index(config.ContentMappers(), mapper), which can differ from its position in the contentMappers array when an earlier entry was dropped (for example a failed package resolution). That gives a wrong location, not a crash, and is left for a separate change.

Tests:

  • TestContentMapperOptionDiagnosticLocationInheritedFromExtendedConfig (tsc/internal/tsoptions): base config in another directory; nested option, array element, empty path, and a mapper not in the config. Three panic on main and the empty-path case fails with a nil file.
  • TestContentMapperOptionDiagnosticLocationWithoutContentMappersSyntax: property and index segments with no contentMappers syntax. Both panic on main.
  • TestTscContentMapperOptionDiagnosticsFromExtendedConfig (tsc/internal/execute/tsctests), new baseline tsc/contentMapperOptionDiagnostics/mapper-option-diagnostic-from-extended-config-in-another-directory.js: the full tsc run with the test mapper. It panics on main with the reported stack, and with the fix reports ../base/tsconfig.base.json(6,48): error mapper123: Option 'name' requires a string.

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 checked, because npx hereby validate --all did not complete without error on this machine (macOS 27.2, Go 1.27.4, Node 26.7.0). It reported test:tsc, test:tools, test:codegen and lint as failed. Each failure also occurs on unmodified main at 88071f4 or comes from the host's toolchain:

  • test:tsc: 23 failures out of 173,495 tests, all in cmd/tsc TestCommandLineTermination, internal/fswatch and internal/lsp/lspwatcher. Each of these also fails on main. Run alone, TestCommandLineTermination (watch-mode processes that time out waiting to terminate) failed in 3 of 4 runs on main and 4 of 4 on this branch, and fswatch and lspwatcher failed on main too.
  • test:tools: tools/cmd/machotool does not link, because the Command Line Tools linker rejects the SDK's arm64e.x1 stubs. It fails the same way on main.
  • test:codegen: tools/scripts/gen/cache.test.mts expects /private/var/... and gets /var/.... It fails the same way on main.
  • lint: building golangci-lint fails at the same cgo link step. CGO_ENABLED=0 npx hereby lint passes with 0 issues in both modules.

What passed: npx hereby check:format, npx hereby tidy with no diff, CGO_ENABLED=0 npx hereby lint, the API tests included in validate --all, and go test for internal/tsoptions/..., internal/compiler/..., internal/execute/..., internal/contentmapper/... and internal/project/.... I also rebuilt tsc with npx hereby build and reran the reproduction above.

An AI coding agent (Claude) wrote this change at the owner's request.

benpsnyder and others added 2 commits November 23, 2026 21:59
…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>
@benpsnyder

Copy link
Copy Markdown
Owner Author

Replaced by microsoft#65652.

@benpsnyder benpsnyder closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant