Skip to content

Content mapper auto import formatting panic - #64042

Open
Andrew Branch (andrewbranch) wants to merge 3 commits into
microsoft:mainfrom
andrewbranch:content-mapper-auto-import-formatting-panic
Open

Content mapper auto import formatting panic#64042
Andrew Branch (andrewbranch) wants to merge 3 commits into
microsoft:mainfrom
andrewbranch:content-mapper-auto-import-formatting-panic

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Fixes sveltejs/language-tools#3111 (comment)

The crash was a consequence of the very sketchy comment (inherited from Strada)

/** Note: this may mutate `nodeIn`. */
func (t *Tracker) getFormattedTextOfNode(nodeIn *ast.Node, targetSourceFile *ast.SourceFile, sourceFile *ast.SourceFile, pos int, options NodeOptions) string {

This was ok-ish before because nodeIn was 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.

Copilot AI balanced review requested due to automatic review settings August 26, 2026 22:45
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Aug 26, 2026

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

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

2 participants