Content mapper auto import formatting panic - #64042
Open
Andrew Branch (andrewbranch) wants to merge 3 commits into
Open
Content mapper auto import formatting panic#64042Andrew Branch (andrewbranch) wants to merge 3 commits into
Andrew Branch (andrewbranch) wants to merge 3 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. |
Comment on lines
+45
to
+47
| // order changes by start position | ||
| // If the start position is the same, put the shorter range first, since an empty range (x, x) may precede (x, y) but not vice-versa. | ||
| slices.SortStableFunc(textChanges, func(a, b *lsproto.TextEdit) int { return lsproto.CompareRanges(a.Range, b.Range) }) |
Comment on lines
+45
to
+54
| // order changes by start position | ||
| // If the start position is the same, put the shorter range first, since an empty range (x, x) may precede (x, y) but not vice-versa. | ||
| slices.SortStableFunc(textChanges, func(a, b *lsproto.TextEdit) int { return lsproto.CompareRanges(a.Range, b.Range) }) | ||
| // verify that change intervals do not overlap, except possibly at end points. | ||
| for i := range len(textChanges) - 1 { | ||
| if lsproto.ComparePositions(textChanges[i].Range.End, textChanges[i+1].Range.Start) > 0 { | ||
| // assert change[i].End <= change[i + 1].Start | ||
| panic(fmt.Sprintf("changes overlap: %v and %v", textChanges[i].Range, textChanges[i+1].Range)) | ||
| } | ||
| } |
| if pos < 0 || pos > len(original) { | ||
| return text | ||
| } | ||
| lineStart := strings.LastIndexByte(original[:pos], '\n') + 1 |
Comment on lines
+143
to
144
| if visited == node { | ||
| newNode = visited.Clone(v.Factory) |
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.