Skip to content

Fix declarationMap panic on cross-file reused nodes#3270

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-declaration-map-panic
Draft

Fix declarationMap panic on cross-file reused nodes#3270
Copilot wants to merge 2 commits intomainfrom
copilot/fix-declaration-map-panic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

During declaration emit with declarationMap enabled, 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-bounds charCodeAt returns NaN, but Go panics.

  • internal/scanner/scanner.go: Bounds check in SkipTriviaEx — return pos unchanged when pos >= len(text)
  • internal/printer/printer.go: Bounds check in emitPos — skip source map emission when pos > len(sourceText)
  • Test case: declarationMapReusedNodeFromDifferentFile.ts — reproduces the exact crash scenario where a helper file's node positions exceed the shorter index file's text length
// @declaration: true
// @declarationMap: true

// @filename: helper.ts
// ... long file with positions past index.ts length ...
export function getWidget(): (w: Widget) => keyof Widget { ... }

// @filename: index.ts
// short file — reused nodes from helper.ts have out-of-range positions
export const fn = getWidget();

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

…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
Copilot AI requested a review from DanielRosenwasser March 27, 2026 18:07
@DanielRosenwasser
Copy link
Copy Markdown
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.

@jakebailey
Copy link
Copy Markdown
Member

This is wrong. #3274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic with declarationMap in version 7.0.0-dev.20260323.1

3 participants