Skip to content

Clamp received lsproto.Positions#2966

Closed
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:clamp-lsp-pos
Closed

Clamp received lsproto.Positions#2966
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:clamp-lsp-pos

Conversation

@Andarist
Copy link
Copy Markdown
Contributor

@Andarist Andarist commented Mar 3, 2026

fixes #2959

assert.DeepEqual(t, *actualConfig2.JS(), *expectedPrefs2)
})

t.Run("definition with stale position after file shrink does not panic", func(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this not an entirely illegal situation? The client can't give us positions that our out of range for its own view...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair, I mentioned this is is a tradeoff here: #2966 (comment) . I think that in the long run this should return an explicit error (instead panicing) if the drift like this is not acceptable (which is a fair stance!).

As to the "proper" solution. I suspect this will require "snapshot freezing" like the one in #2905 . Once that PR settles - I'll take another look at this specific crash here again.

char := core.TextPos(lineAndCharacter.Character)

if line < 0 || int(line) >= len(lineMap.LineStarts) {
panic(fmt.Sprintf("bad line number. Line: %d, lineMap length: %d", line, len(lineMap.LineStarts)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A rogue client can always send some invalid position. It's definitely a tradeoff and we could return an error instead but clamping to the bounds seems somewhat benign to me here.

@DanielRosenwasser
Copy link
Copy Markdown
Member

It is possible/probable that we will eventually do something like this (or at least silently error in the output pane), but I think right now we do want to find which sorts of issues are coming up so we can either figure out what we're doing wrong in request handling, or in VS Code's language server client.

@Andarist
Copy link
Copy Markdown
Contributor Author

Andarist commented Apr 9, 2026

superseded by #3365

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 handling request textDocument/definition: bad line number

3 participants