-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Push per-file diagnostics for clients without pull diagnostics support #63921
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
Open
Christian Vuerings (christianvuerings)
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
christianvuerings:push-file-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,137 @@ | ||
| package lsp_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/TypeScript/tsc/internal/bundled" | ||
| "github.com/microsoft/TypeScript/tsc/internal/lsp" | ||
| "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" | ||
| "github.com/microsoft/TypeScript/tsc/internal/testutil/lsptestutil" | ||
| "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| // TestPushFileDiagnosticsGate verifies the initialization gate for per-file push | ||
| // diagnostics: clients without pull-diagnostics support receive them, while | ||
| // pull-capable clients and clients that set disablePushDiagnostics do not. | ||
| func TestPushFileDiagnosticsGate(t *testing.T) { | ||
| t.Parallel() | ||
| if !bundled.Embedded { | ||
| t.Skip("bundled files are not embedded") | ||
| } | ||
|
|
||
| const fileContent = `const x: number = "";` | ||
| files := map[string]string{ | ||
| "/home/project/tsconfig.json": `{}`, | ||
| "/home/project/index.ts": fileContent, | ||
| } | ||
| uri := lsproto.DocumentUri("file:///home/project/index.ts") | ||
|
|
||
| // run initializes a server with the given capabilities and options, opens the | ||
| // file, waits for background tasks and a transport round trip, and returns the | ||
| // publishDiagnostics params received for the file. | ||
| run := func(t *testing.T, caps *lsproto.ClientCapabilities, initializationOptions *lsproto.InitializationOptions) []*lsproto.PublishDiagnosticsParams { | ||
| onServerRequest := func(_ context.Context, req *lsproto.RequestMessage) *lsproto.ResponseMessage { | ||
| switch req.Method { | ||
| case lsproto.MethodWorkspaceConfiguration: | ||
| return &lsproto.ResponseMessage{ID: req.ID, JSONRPC: req.JSONRPC, Result: []any{nil, nil, nil, nil}} | ||
| case lsproto.MethodClientRegisterCapability, lsproto.MethodClientUnregisterCapability: | ||
| return &lsproto.ResponseMessage{ID: req.ID, JSONRPC: req.JSONRPC, Result: lsproto.Null{}} | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| fs := bundled.WrapFS(vfstest.FromMap(files, false)) | ||
| client, closeClient := lsptestutil.NewLSPClient(t, lsp.ServerOptions{ | ||
| Err: io.Discard, | ||
| Cwd: "/home/project", | ||
| FS: fs, | ||
| DefaultLibraryPath: bundled.LibPath(), | ||
| }, onServerRequest) | ||
| t.Cleanup(func() { assert.NilError(t, closeClient()) }) | ||
|
|
||
| var mu sync.Mutex | ||
| var published []*lsproto.PublishDiagnosticsParams | ||
| client.OnServerNotification = func(_ context.Context, req *lsproto.RequestMessage) { | ||
| if req.Method == lsproto.MethodTextDocumentPublishDiagnostics { | ||
| if params, err := lsproto.UnmarshalParams[*lsproto.PublishDiagnosticsParams](req); err == nil && params.Uri == uri { | ||
| mu.Lock() | ||
| published = append(published, params) | ||
| mu.Unlock() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var initOptionsOrNull *lsproto.InitializationOptionsOrNull | ||
| if initializationOptions != nil { | ||
| initOptionsOrNull = &lsproto.InitializationOptionsOrNull{InitializationOptions: initializationOptions} | ||
| } | ||
| initMsg, _, ok := lsptestutil.SendRequest(t, client, lsproto.InitializeInfo, &lsproto.InitializeParams{ | ||
| Capabilities: caps, | ||
| InitializationOptions: initOptionsOrNull, | ||
| }) | ||
| assert.Assert(t, ok && initMsg.AsResponse().Error == nil, "initialize failed") | ||
| lsptestutil.SendNotification(t, client, lsproto.InitializedInfo, &lsproto.InitializedParams{}) | ||
| <-client.Server.InitComplete() | ||
|
|
||
| lsptestutil.SendNotification(t, client, lsproto.TextDocumentDidOpenInfo, &lsproto.DidOpenTextDocumentParams{ | ||
| TextDocument: &lsproto.TextDocumentItem{Uri: uri, LanguageId: "typescript", Version: 1, Text: fileContent}, | ||
| }) | ||
|
|
||
| // A request round trip ensures the didOpen has been processed. | ||
| msg, _, ok := lsptestutil.SendRequest(t, client, lsproto.CustomProjectInfoInfo, &lsproto.ProjectInfoParams{ | ||
| TextDocument: lsproto.TextDocumentIdentifier{Uri: uri}, | ||
| }) | ||
| assert.Assert(t, ok && msg.AsResponse().Error == nil) | ||
| client.Server.Session().WaitForBackgroundTasks() | ||
|
|
||
| // Another round trip drains notifications written by the background tasks, | ||
| // since the client router processes the ordered output stream sequentially. | ||
| msg, _, ok = lsptestutil.SendRequest(t, client, lsproto.CustomProjectInfoInfo, &lsproto.ProjectInfoParams{ | ||
| TextDocument: lsproto.TextDocumentIdentifier{Uri: uri}, | ||
| }) | ||
| assert.Assert(t, ok && msg.AsResponse().Error == nil) | ||
|
|
||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| return append([]*lsproto.PublishDiagnosticsParams(nil), published...) | ||
| } | ||
|
|
||
| pushOnlyCaps := &lsproto.ClientCapabilities{ | ||
| TextDocument: &lsproto.TextDocumentClientCapabilities{ | ||
| PublishDiagnostics: &lsproto.PublishDiagnosticsClientCapabilities{}, | ||
| }, | ||
| } | ||
|
|
||
| t.Run("push-only client receives file diagnostics", func(t *testing.T) { | ||
| t.Parallel() | ||
| published := run(t, pushOnlyCaps, nil) | ||
| assert.Assert(t, len(published) > 0, "expected publishDiagnostics for the file") | ||
| last := published[len(published)-1] | ||
| assert.Equal(t, len(last.Diagnostics), 1) | ||
| }) | ||
|
|
||
| t.Run("pull-capable client receives no file diagnostics pushes", func(t *testing.T) { | ||
| t.Parallel() | ||
| pullCaps := &lsproto.ClientCapabilities{ | ||
| TextDocument: &lsproto.TextDocumentClientCapabilities{ | ||
| PublishDiagnostics: &lsproto.PublishDiagnosticsClientCapabilities{}, | ||
| Diagnostic: &lsproto.DiagnosticClientCapabilities{}, | ||
| }, | ||
| } | ||
| published := run(t, pullCaps, nil) | ||
| assert.Equal(t, len(published), 0) | ||
| }) | ||
|
|
||
| t.Run("disablePushDiagnostics disables file diagnostics pushes", func(t *testing.T) { | ||
| t.Parallel() | ||
| published := run(t, pushOnlyCaps, &lsproto.InitializationOptions{ | ||
| DisablePushDiagnostics: new(true), | ||
| }) | ||
| assert.Equal(t, len(published), 0) | ||
| }) | ||
| } |
Oops, something went wrong.
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.
Added in 2e7b1b3: TestPushFileDiagnosticsGate in tsc/internal/lsp runs the real initialize handshake and covers all three cases: a push-only client receives per-file publishes, a pull-capable client receives none, and disablePushDiagnostics disables them.