-
Notifications
You must be signed in to change notification settings - Fork 716
Fix panic in textDocument/onTypeFormatting when tokenAtPosition is nil #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: jakebailey <[email protected]>
Co-authored-by: jakebailey <[email protected]>
Co-authored-by: jakebailey <[email protected]>
@copilot this is a bad fix. These helpers should not have to adapt to callers passing in bad nil values. It's the callers that are wrong |
Fixed in 646b07d (amending previous commits). The caller now properly computes |
Co-authored-by: jakebailey <[email protected]>
There was a problem hiding this 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 panic in the language server that occurred when pressing enter in an empty TypeScript file, which triggered textDocument/onTypeFormatting
. The issue was caused by passing a nil
tokenAtPosition
parameter to the isInComment
function, which then attempted to use it without null checking.
- Fixed
getFormattingEditsAfterKeystroke
to properly computetokenAtPosition
before callingisInComment
- Added test cases to reproduce and verify the fix for the panic scenario
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
internal/ls/format.go | Fixed nil pointer dereference by computing tokenAtPosition before calling isInComment |
internal/ls/format_test.go | Added test cases to reproduce the original panic scenario with empty files and simple statements |
Fixes a panic that occurred when pressing enter in an empty TypeScript file, which triggered
textDocument/onTypeFormatting
.Problem
The language server would panic with
invalid memory address or nil pointer dereference
when:.ts
file<enter>
to insert a newlineThe stack trace showed the panic occurred in
getRangeOfEnclosingComment
when it tried to use anil
tokenAtPosition
node:Root Cause
In
getFormattingEditsAfterKeystroke
, the call toisInComment(file, position, nil)
passednil
for thetokenAtPosition
parameter. Thisnil
value was then passed togetRangeOfEnclosingComment
, which attempted to use it without checking, causing nil pointer dereferences.Solution
Fixed the caller (
getFormattingEditsAfterKeystroke
) to properly computetokenAtPosition
usingastnav.GetTokenAtPosition(file, position)
before callingisInComment
. This follows the same pattern used elsewhere in the codebase (e.g., incompletions.go
) and respects the contract documented in the function comments: "It is the caller's responsibility to callastnav.GetTokenAtPosition
to compute a defaulttokenAtPosition
."Testing
Added two test cases that reproduce the original panic:
All tests pass, including the full test suite.
Fixes #1666
Original prompt
Fixes #1666
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.