-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Keep LSP server alive after response marshal failures #4896
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
Merged
John Favret (johnfav03)
merged 7 commits into
microsoft:main
from
johnfav03:fix-selrange-marshal-depth
Aug 13, 2026
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2959209
remove fatality from response marshal failures
johnfav03 759088a
unexport messagemarshalerror
johnfav03 bf3742e
cap selectionranges at 1000 for json serializing
johnfav03 d330694
moved test into server_shutdown_test.go
johnfav03 db9f170
switched range limiting from post pass to depth counter
johnfav03 5bf55d0
renamed server_shutdown_test to server_test
johnfav03 515bb44
extracted selectionrangebuilder type
johnfav03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ls | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/ast" | ||
| "github.com/microsoft/typescript-go/internal/core" | ||
| "github.com/microsoft/typescript-go/internal/json" | ||
| "github.com/microsoft/typescript-go/internal/jsonrpc" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsconv" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| "github.com/microsoft/typescript-go/internal/parser" | ||
| ) | ||
|
|
||
| func TestSelectionRangeDepthIsLimited(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const nestingDepth = 12000 | ||
| text := "const x = " + strings.Repeat("(", nestingDepth) + "1" + strings.Repeat(")", nestingDepth) + ";" | ||
| sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ | ||
| FileName: "/index.ts", | ||
| Path: "/index.ts", | ||
| }, text, core.ScriptKindTS) | ||
| lineMap := lsconv.ComputeLSPLineStarts(text) | ||
| languageService := &LanguageService{ | ||
| converters: lsconv.NewConverters(lsproto.PositionEncodingKindUTF16, func(string) *lsconv.LSPLineMap { | ||
| return lineMap | ||
| }), | ||
| } | ||
|
|
||
| result := getSmartSelectionRange(languageService, sourceFile, len("const x = ")+nestingDepth) | ||
| depth := 0 | ||
| var outermost *lsproto.SelectionRange | ||
| for current := result; current != nil; current = current.Parent { | ||
| depth++ | ||
| outermost = current | ||
| } | ||
|
|
||
| if depth != maxSelectionRangeDepth { | ||
| t.Fatalf("selection range depth = %d, want %d", depth, maxSelectionRangeDepth) | ||
| } | ||
| innerRange := languageService.converters.ToLSPRange(sourceFile, core.NewTextRange(len("const x = ")+nestingDepth, len("const x = ")+nestingDepth+1)) | ||
| if result.Range != innerRange { | ||
| t.Fatalf("innermost selection range = %v, want %v", result.Range, innerRange) | ||
| } | ||
| fullRange := languageService.converters.ToLSPRange(sourceFile, core.NewTextRange(sourceFile.Pos(), sourceFile.End())) | ||
| if outermost.Range != fullRange { | ||
| t.Fatalf("outermost selection range = %v, want full file range %v", outermost.Range, fullRange) | ||
| } | ||
| results := []*lsproto.SelectionRange{result} | ||
| response := lsproto.SelectionRangesOrNull{SelectionRanges: &results} | ||
| id := jsonrpc.NewIDString("selectionRange") | ||
| message := (&lsproto.ResponseMessage{ID: id, Result: &response}).Message() | ||
| if _, err := json.Marshal(message); err != nil { | ||
| t.Fatalf("failed to marshal limited selection range: %v", err) | ||
| } | ||
| } |
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
|
johnfav03 marked this conversation as resolved.
|
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
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.
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.
I'm having trouble understanding the range thing here, are you basically just doing a ring buffer at this point?
Is there any harm in just stopping after its len is too big and just bailing out early?
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.
Yeah, it's effectively a ring buffer; I figured this was appropriate here because we discover ranges from broadest to most specific, so stopping early would ignore the selection steps closest to the cursor. If you think it's better to just bail to save traversal cost though I can definitely implement that
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.
I think it's fine, I just think the implementation is a little hard to understand, compared to maybe some sort of extracted type?