Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Fix nil dereference in resolveHelpersModule via checkExternalEmitHelpers - #3879

Closed
Daniel Rosenwasser (DanielRosenwasser) with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-check-external-emit-helpers
Closed

Daniel Rosenwasser (DanielRosenwasser) with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-check-external-emit-helpers

Conversation

Copilot AI commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

resolveHelpersModule passes the result of GetImportHelpersImportSpecifier directly to resolveExternalModule without a nil check. The synthetic tslib import specifier is only created during file loading when specific conditions hold (JS file, or non-declaration file with isolatedModules or already-detected external module status). In language service scenarios, checkExternalEmitHelpers can run on files where the specifier was never created, causing a nil dereference in IsStringLiteralLike.

Changes

  • internal/checker/checker.go: In resolveHelpersModule, fall back to file.AsNode() when GetImportHelpersImportSpecifier returns nil. This gives resolveExternalModule a valid location node for determining the importing source file and default resolution mode.

  • testdata/tests/cases/compiler/checkExternalEmitHelpersCrash.ts: Compiler test exercising checkExternalEmitHelpers with importHelpers: true.

  • internal/fourslash/tests/checkExternalEmitHelpersCrash_test.go: Fourslash test covering the LS diagnostics path with both async functions and ES decorators (the two crash stacks from the issue).

Copilot AI and others added 2 commits May 15, 2026 23:21
…s nil

Handle nil return from GetImportHelpersImportSpecifier in resolveHelpersModule
by falling back to the source file node. This prevents a nil pointer dereference
in resolveExternalModule when the synthetic tslib import specifier wasn't created
during file loading (e.g., in language service scenarios).

Fixes #1358

Co-authored-by: DanielRosenwasser <[email protected]>
Copilot AI changed the title [WIP] Fix crash via checkExternalEmitHelpers Fix nil dereference in resolveHelpersModule via checkExternalEmitHelpers May 15, 2026
@DanielRosenwasser

Copy link
Copy Markdown
Member

Copilot I don't see a panic from that test locally.

The fourslash and compiler tests were not triggering the nil
dereference. The specifier is always created for files that pass
IsEffectiveExternalModule under normal program construction, so the
crash likely requires a specific LS scenario (e.g., project references
with differing importHelpers settings) that is difficult to reproduce
in unit tests.

The defensive nil check in resolveHelpersModule is retained.

Co-authored-by: DanielRosenwasser <[email protected]>

Copilot AI commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

Copilot I don't see a panic from that test locally.

You're right — I verified that neither the fourslash test nor the compiler test panics without the fix. The specifier is always created for files that pass IsEffectiveExternalModule under normal program construction because the file loader's condition (isJavaScriptFile || (!file.IsDeclarationFile && (optionsForFile.GetIsolatedModules() || isExternalModuleFile))) aligns with the checker's IsEffectiveExternalModule check for .ts files.

My best theory for how this manifests in production is through project references where the referenced project has different importHelpers settings — the file loader uses optionsForFile (which could be the referenced project's options without importHelpers) while the checker uses the main project's compilerOptions (with importHelpers: true). But I couldn't reproduce that in a fourslash test either.

I've removed the tests that don't prove anything (11b59a8). The defensive nil check is retained since it's correct and harmless. If you have a specific repro case or more context on the scenario, I can try to write a proper test for it.

@jakebailey
Jake Bailey (jakebailey) marked this pull request as ready for review June 13, 2026 00:13
Copilot AI review requested due to automatic review settings June 13, 2026 00:13

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.

Pull request overview

This PR prevents a crash in checkExternalEmitHelpers/resolveHelpersModule when a file doesn’t have a pre-created synthetic tslib import specifier (notably in language service + project reference redirect scenarios), and adds regression coverage for the redirect-driven case.

Changes:

  • Ensure fileLoader.resolveImportsAndModuleAugmentations creates the synthetic tslib import specifier when importHelpers is enabled on the referencing project’s options, even if the redirected project’s options disable it.
  • Add an LSP/project-references regression test to assert importHelpers specifier creation for redirected source files and to exercise diagnostics on the redirected file.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/project/projectreferencesprogram_test.go Adds a regression subtest covering project-reference redirect behavior and diagnostics for a redirected source file with importHelpers enabled in the referencing project.
internal/compiler/fileloader.go Updates synthetic tslib import-specifier creation logic to also consider the base (referencing) compiler options in redirect scenarios.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@jakebailey

Copy link
Copy Markdown
Member

#3878 is actually already fixed, but this is actually yet another crash

file := program.GetSourceFile("/user/username/projects/myproject/dependency/fn.ts")
assert.Assert(t, file != nil)
assert.Assert(t, program.Options().ImportHelpers.IsTrue())
assert.Assert(t, program.GetImportHelpersImportSpecifier(file.Path()) != nil)

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.

Why is this the expected behavior? The file is not one that will be emitted by this program. It will be emitted by a referenced program under "importHelpers": false. Under CLI compilation, this file would have been redirected to a declaration file, which would also not have an import helpers synthetic import, by merit of being already emitted to .d.ts. I feel like this is the wrong place for the fix.

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.

I think I'll just close this, I think we already fixed the problem in the other PR, and this turned into some nonsense crash copilot tried to make in the process of fixing this PR which was already redundant

@jakebailey
Jake Bailey (jakebailey) deleted the copilot/fix-check-external-emit-helpers branch June 29, 2026 16:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash via checkExternalEmitHelpers

6 participants