Content mapper auto import formatting panic - #64042
Open
Andrew Branch (andrewbranch) wants to merge 4 commits into
Open
Content mapper auto import formatting panic#64042Andrew Branch (andrewbranch) wants to merge 4 commits into
Andrew Branch (andrewbranch) wants to merge 4 commits into
Conversation
Copilot started reviewing on behalf of
Andrew Branch (andrewbranch)
August 26, 2026 22:46
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors change tracking to retain virtual coordinates until final LSP edit generation, preventing content-mapped auto-import formatting crashes.
Changes:
- Preserves virtual edit ranges through formatting.
- Clones nodes before assigning print positions.
- Adds hoisting and indentation regression tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
tsc/internal/testutil/contentmappertest/registry.go |
Registers the hoisting mapper. |
tsc/internal/testutil/contentmappertest/hoisting.go |
Implements the test mapper. |
tsc/internal/printer/changetrackerwriter.go |
Avoids mutating printed nodes. |
tsc/internal/ls/change/trackerimpl.go |
Formats virtual edits and performs final mapping. |
tsc/internal/ls/change/tracker.go |
Stores edits in virtual coordinates. |
tsc/internal/ls/change/delete.go |
Uses virtual ranges for deletions. |
tsc/internal/fourslash/tests/importFixIndentedStatements_test.go |
Tests indented import insertion. |
tsc/internal/fourslash/tests/contentMapperAutoImports_test.go |
Tests auto-imports at hoisted boundaries. |
Copilot started reviewing on behalf of
Andrew Branch (andrewbranch)
August 27, 2026 18:26
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
Suppressed comments (2)
tsc/internal/printer/changetrackerwriter.go:145
- The added fourslash regression no longer exercises this non-mutation guarantee: with virtual ranges retained,
computeNewTextformats its stored edit once rather than iterating all projections. Reverting this clone would therefore still leave that test passing. Add a focused printer test that positions/prints the same synthesized tree twice, verifies identical output, and verifies the caller-owned nodes did not acquire locations or changed parent links.
if visited == node {
newNode = visited.Clone(v.Factory)
}
tsc/internal/ls/change/trackerimpl.go:70
- Projection identity has already been discarded here, so
projections[fileName] > 1is too coarse. If one projection intentionally contains ordered same-position inserts (a, thenb) and another projection contributes any edit—or duplicates the same sequence—this code either removes a legitimate identical insert or treats the same-projectiona/bpair as a cross-projection conflict and drops the whole file. Keep the originating source file with each converted edit; preserve/validate ordering within each projection, and deduplicate or reject only edits compared across distinct projections.
if projections[fileName] > 1 {
textChanges = dedupeIdenticalEdits(textChanges)
changes[fileName] = textChanges
}
Andrew Branch (andrewbranch)
requested review from
Gabriela Araujo Britto (gabritto) and
Jake Bailey (jakebailey)
August 27, 2026 18:58
Gabriela Araujo Britto (gabritto)
approved these changes
Aug 27, 2026
Gabriela Araujo Britto (gabritto)
left a comment
Member
There was a problem hiding this comment.
Left a minor comment on comment clarity, but I think the approach here makes sense.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes sveltejs/language-tools#3111 (comment)
The crash was a consequence of the very sketchy comment (inherited from Strada)
This was ok-ish before because
nodeInwas freshly synthesized and only used once. But with content mappers, when a node was inserted into an original text position that maps to more than one verbatim location in the virtual text, we formatted the same synthesized node in both virtual locations to make sure it formats the same way. During this process, the assignment of node positions during the first pass messes up state for the second pass. The crash is fixed just by cloning the node instead of mutating it.However, this made me realize that the change tracker was modeling this poorly. Edits were being computed in virtual source files, then converted into original text coordinates for storage, and then converting back into virtual text coordinates for formatting, which is a lossy/ambiguous operation: original to virtual position is potentially a one-to-many. So this PR applies a refactor to keep change tracker edits in virtual coordinates until the very end when retrieving the final LSP edits.