Repository navigation
Fix external helper crash after module-status updates - #3994
Jake Bailey (jakebailey) merged 18 commits into
Conversation
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
|
Perfect - can you write this as a fourslash test instead? Make sure the fourslash test actually fails without the fix. |
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Addressed in f732c93. I replaced the checker-level regression with a fourslash test and verified it fails without the production fix with the |
There was a problem hiding this comment.
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
importHelpersand 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
| return file2 != nil && | ||
| file1.ParseOptions() == file2.ParseOptions() && | ||
| file1.UsesUriStyleNodeCoreModules == file2.UsesUriStyleNodeCoreModules && | ||
| (file1.ExternalModuleIndicator != nil) == (file2.ExternalModuleIndicator != nil) && |
There was a problem hiding this comment.
Don't we need to check that they're equal in some way?
There was a problem hiding this comment.
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?"
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yeah, I'm pretty sure the problem here is that those side tables are not being updated properly
There was a problem hiding this comment.
Daniel Rosenwasser (@DanielRosenwasser) did we decide not to address this?
There was a problem hiding this comment.
I guess we can update the new state instead.
Copilot do that.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Alright, you all asked for it! 😄
| 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 |
There was a problem hiding this comment.
Does this mean every program update would allocate a full arena slab for a synthetic string literal?
There was a problem hiding this comment.
Sort of, it slowly increases so only a couple of them get allocated tops
There was a problem hiding this comment.
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
|
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? |
|
Copilot explore Jake Bailey (@jakebailey)'s feedback. |
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
|
I have no idea what copilot is doing. this is even more complicated and the large updateImportHelpersImportSpecifier remains |
Co-authored-by: DanielRosenwasser <[email protected]>
Co-authored-by: DanielRosenwasser <[email protected]>
Addressed in c05f034. The reused program now stores a compact lazy importHelpers specifier wrapper and defers creating the synthetic |
|
Again, updateImportHelpersImportSpecifier is still there. I don't understand why this is so complicated? |
* 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
…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]>
checkExternalEmitHelperscould panic after an incremental program update when a file changed from script to module. The reused program state could lack the synthetictslibimport specifier needed for helper resolution.Program reuse
importHelperssynthetic imports are added or removed.tslibimport state, module resolution cache, include reasons, and directly included helper files instead of forcing a full rebuild based on module-status changes.importHelpersspecifier state in a compact lazy wrapper so the synthetic AST node is created only on first access.Regression coverage
importHelpers.tslibstate and completes semantic diagnostics without errors.