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

Fix external helper crash after module-status updates - #3994

Merged
Jake Bailey (jakebailey) merged 18 commits into
mainfrom
copilot/fix-check-external-emit-helpers-again
Jun 12, 2026
Merged

Jake Bailey (jakebailey) merged 18 commits into
mainfrom
copilot/fix-check-external-emit-helpers-again

Conversation

Copilot AI commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

checkExternalEmitHelpers could panic after an incremental program update when a file changed from script to module. The reused program state could lack the synthetic tslib import specifier needed for helper resolution.

  • Program reuse

    • Update reused program side tables when importHelpers synthetic imports are added or removed.
    • Refresh the synthetic tslib import state, module resolution cache, include reasons, and directly included helper files instead of forcing a full rebuild based on module-status changes.
    • Store reused importHelpers specifier state in a compact lazy wrapper so the synthetic AST node is created only on first access.
  • Regression coverage

    • Added a fourslash test for updating a file to an exported decorated class under importHelpers.
    • Verifies the updated program rebuilds synthetic tslib state and completes semantic diagnostics without errors.
declare function dec(value: Function): void;

@dec
export class C {}

Copilot AI and others added 3 commits May 19, 2026 21:11
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Copilot AI changed the title [WIP] Fix crash in checkExternalEmitHelpers Fix external helper crash after module-status updates May 19, 2026
@DanielRosenwasser

Copy link
Copy Markdown
Member

Perfect - can you write this as a fourslash test instead? Make sure the fourslash test actually fails without the fix.

@DanielRosenwasser

Copy link
Copy Markdown
Member

Copilot

Copilot AI and others added 3 commits May 19, 2026 21:59
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>

Copilot AI commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

Perfect - can you write this as a fourslash test instead? Make sure the fourslash test actually fails without the fix.

Addressed in f732c93. I replaced the checker-level regression with a fourslash test and verified it fails without the production fix with the textDocument/diagnostic nil-pointer panic.

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

Note

Copilot was unable to run its full agentic suite in this review.

Fixes an incremental-program reuse edge case that could crash external helper resolution when a file changes from script to module (e.g., via adding an export) under importHelpers.

Changes:

  • Mark program state as non-reusable when a file’s module-status indicators (ExternalModuleIndicator / CommonJSModuleIndicator) change.
  • Add a fourslash regression test that edits a script into an exported decorated module under importHelpers and verifies diagnostics remain error-free.
Show a summary per file
File Description
internal/fourslash/tests/importHelpersAfterScriptBecomesDecoratedModule_test.go Adds regression test covering script→decorated module transition with importHelpers.
internal/compiler/program.go Prevents incremental reuse when module-status indicators change, forcing rebuild of module-resolution/helper state.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

Comment thread internal/compiler/program.go Outdated
return file2 != nil &&
file1.ParseOptions() == file2.ParseOptions() &&
file1.UsesUriStyleNodeCoreModules == file2.UsesUriStyleNodeCoreModules &&
(file1.ExternalModuleIndicator != nil) == (file2.ExternalModuleIndicator != 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.

Don't we need to check that they're equal in some way?

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) May 20, 2026 •

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 don't think so - my understanding is that these are used to determine if the program is sufficiently structurally the same to avoid rebuilding a full program - not that the files themselves are "equal" before/after. Below, we check that the rest of the structure is equal (e.g. same imports, global modifications, etc.)

From program.go's perspective, the external module indicator probably should not be used to determine anything about the file other than "is this a module, or is this a global?"

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.

Actually, I'm not sure I understand why this fix works at all. These are actually a pure function of the file contents + external module indicator options (which is just the file path+reads), which are part of the cache key for files, so when would these change and us not invalidate, or, why would reusing them ever be wrong

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) May 20, 2026 •

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'm not sure which portion you're thinking of, but basically here's what's happening.

First off, every Program includes processedFiles which contains a map of filePath -> synthesizedStringLiteralsToBeUsedAsImportSpecifiers. This seems to me to be calculated at parse-time, but this state is located on the program, not on the file. So as far as I understand, the parse cache wouldn't really come into play here.

Now when a file change comes in, the idea is we try to reuse a lot of the program state for the new Program. UpdateProgram does this via canReplaceFileInProgram. The checks here ensure that we cannot reuse program state related to locating tslib for a script -> module transition.

@weswigham Wesley Wigham (weswigham) May 26, 2026 •

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.

Don't we need to check that they're equal in some way?

The only way they'd be actually reference equal is if they came from the same file, so we'd have to do some kinda deep compare which seems weird, when all that really matters is "are both the input and output a module".

This change looks weird because ast.IsExternalOrCommonJSModule(file1) == ast.IsExternalOrCommonJSModule(file2) is what you'd actually write to express intent, even if this is equivalent.

But! That's just over-invalidating to patch the problem, which makes it a pretty bad fix - jake's right, no reason we can't reuse the source files in this case. updateFileIncludeProcessor just has to be updated to add/remove the synthetic import(s) from the processor, which shouldn't actually be hard.

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.

Yeah, I'm pretty sure the problem here is that those side tables are not being updated properly

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.

Daniel Rosenwasser (@DanielRosenwasser) did we decide not to address this?

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 guess we can update the new state instead.

Copilot do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in a096cd6. The program reuse path now updates the synthetic tslib importHelpers side tables instead of invalidating on module-status changes, and the fourslash regression still fails with the old behavior and passes with the new state update.

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.

Alright, you all asked for it! 😄

Comment thread internal/compiler/program.go Outdated
Comment on lines +422 to +427
factory := &ast.NodeFactory{}
specifier := factory.NewStringLiteral(externalHelpersModuleNameText, ast.TokenFlagsNone)
importDecl := factory.NewImportDeclaration(nil, nil, specifier, nil)
specifier.Parent = importDecl
importDecl.Parent = file.AsNode()
return specifier

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jun 8, 2026 •

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.

Does this mean every program update would allocate a full arena slab for a synthetic string literal?

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.

Sort of, it slowly increases so only a couple of them get allocated tops

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.

Interesting - looks like it just grows to one at a time every time we create a factory though so that's not bad. Hopefully slices don't require much more book-keeping for the runtime

@jakebailey

Copy link
Copy Markdown
Member

The amount of code that this now involves is pretty silly. Is there no way we can avoid this somehow? Like, store some more compact and copyable form, and then defer the creation of nodes to first access, that's cached but not copied between programs? or something?

@DanielRosenwasser

Copy link
Copy Markdown
Member

Copilot explore Jake Bailey (@jakebailey)'s feedback.

Copilot AI and others added 3 commits June 8, 2026 20:46
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
@jakebailey

Copy link
Copy Markdown
Member

I have no idea what copilot is doing. this is even more complicated and the large updateImportHelpersImportSpecifier remains

Copilot AI and others added 2 commits June 8, 2026 20:57

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Copilot explore Jake Bailey (@jakebailey)'s feedback.

Addressed in c05f034. The reused program now stores a compact lazy importHelpers specifier wrapper and defers creating the synthetic tslib AST node until first access, while still updating resolution/include side tables during program reuse.

@jakebailey

Copy link
Copy Markdown
Member

Again, updateImportHelpersImportSpecifier is still there. I don't understand why this is so complicated?

@jakebailey
Jake Bailey (jakebailey) added this pull request to the merge queue Jun 12, 2026
Merged via the queue into main with commit b1e0c7e Jun 12, 2026
21 checks passed
@jakebailey
Jake Bailey (jakebailey) deleted the copilot/fix-check-external-emit-helpers-again branch June 12, 2026 22:33
Christian Vuerings (christianvuerings) added a commit to christianvuerings/typescript-go that referenced this pull request Jun 12, 2026
* origin/main:
  Make LS checkers time out after being idle, segment based on use (take 2) (microsoft#3435)
  Fix external helper crash after module-status updates (microsoft#3994)
  Allow LSP server to use builtin watcher on Windows and macOS (microsoft#4288)
  Rebuilt CLI watcher around new `fswatch` package (microsoft#4026)
  Fix stack overflow crash in base constraint resolution (microsoft#4242)

# Conflicts:
#	internal/project/session.go
Jake Bailey (jakebailey) added a commit to jakebailey/TypeScript that referenced this pull request Aug 13, 2026
…script-go#3994)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: Jake Bailey <[email protected]>
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

7 participants