Skip to content

Conversation

@jakebailey
Copy link
Member

@jakebailey jakebailey commented Oct 22, 2025

Fixes #1867

Analogous to "this shouldn't happen" 🙃

/** @internal */
export function getMappedDocumentSpan(documentSpan: DocumentSpan, sourceMapper: SourceMapper, fileExists?: (path: string) => boolean): DocumentSpan | undefined {
    const { fileName, textSpan } = documentSpan;
    const newPosition = getMappedLocation({ fileName, pos: textSpan.start }, sourceMapper, fileExists);
    if (!newPosition) return undefined;
    const newEndPosition = getMappedLocation({ fileName, pos: textSpan.start + textSpan.length }, sourceMapper, fileExists);
    const newLength = newEndPosition
        ? newEndPosition.pos - newPosition.pos
        : textSpan.length; // This shouldn't happen

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a crash in the language service when handling source map positions where the end position is nil. The fix adds a nil check for the end position in getMappedLocation and provides a fallback calculation based on the start position and range length.

Key changes:

  • Added nil handling for end position in source map location mapping
  • Re-enabled a previously failing test that was affected by this bug
  • Updated baseline outputs for multiple fourslash tests

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/ls/source_map.go Added nil check for endPos with fallback calculation using start position and range length
internal/fourslash/tests/gen/findAllRefsForDefaultExport03_test.go Removed t.Skip() call to re-enable test
internal/fourslash/_scripts/failingTests.txt Removed TestFindAllRefsForDefaultExport03 from failing tests list
testdata/baselines/reference/fourslash/findAllReferences/*.baseline.jsonc Generated baseline outputs for newly passing tests
testdata/baselines/reference/fourslash/documentHighlights/*.baseline.jsonc Generated baseline outputs for newly passing tests

if endPos == nil {
endPos = &sourcemap.DocumentPosition{
FileName: startPos.FileName,
Pos: startPos.Pos + fileRange.Len(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right as a port, but since this is happening, do we need to ensure this position is inside the length of the file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure; my understanding is that the conversion to an LSP range will clamp it via the line map, but maybe not... I can grab the script and min the text length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like PositionToLineAndCharacter will crash with a slice range out of bounds when rescanning the line for the UTF-16 character offset. Maybe it’s worth adding a guard there and not worrying about it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I can. I'm questioning whether or not it's a good idea, though, given we don't do the same sort of bounds checks when going from an LSP position to a byte position... Could go either way. Either way it's sort of papering over something.

@jakebailey jakebailey added this pull request to the merge queue Oct 22, 2025
Merged via the queue into main with commit 0d152d3 Oct 22, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/fix-1867 branch October 22, 2025 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants