Fix declarationMap panic on cross-file reused nodes#3270
Draft
Fix declarationMap panic on cross-file reused nodes#3270
Conversation
…os for cross-file node positions During declaration emit, nodes reused from other files may have positions beyond the current source file's text length. In JavaScript, out-of-bounds string access returns NaN/undefined without crashing, but Go panics. Add bounds checks in: - scanner.SkipTriviaEx: return pos unchanged when pos >= len(text) - printer.emitPos: skip source map emission when pos > len(source text) Add regression test from the issue repro. Fixes #1291 Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/c52384fe-4753-44ce-85eb-f2f49ab53079 Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix panic with declarationMap in version 7.0.0-dev.20260323.1
Fix declarationMap panic on cross-file reused nodes
Mar 27, 2026
Member
|
This is not a good fix. You can only emit a position if a given node belongs to the containing tree, and that's not always the case. |
Member
|
This is wrong. #3274 |
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.
During declaration emit with
declarationMapenabled, nodes reused from other source files retain positions from their original file. When the printer emits source maps, it indexes into the current (shorter) file's text with those positions, causing a slice bounds panic. JavaScript handles this implicitly since out-of-boundscharCodeAtreturnsNaN, but Go panics.internal/scanner/scanner.go: Bounds check inSkipTriviaEx— returnposunchanged whenpos >= len(text)internal/printer/printer.go: Bounds check inemitPos— skip source map emission whenpos > len(sourceText)declarationMapReusedNodeFromDifferentFile.ts— reproduces the exact crash scenario where a helper file's node positions exceed the shorter index file's text length⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.