Skip to content

Commit f0db648

Browse files
Merge #9558
9558: Do not erase Cargo diagnostics from the closed documents r=matklad a=SomeoneToIgnore Fixes #6850 The LSP specification at https://microsoft.github.io/language-server-protocol/specifications/specification-3-14/#textDocument_publishDiagnostics states that > Diagnostics notification are sent from the server to the client to signal results of validation runs. > > Diagnostics are “owned” by the server so it is the server’s responsibility to clear them if necessary. The following rule is used for VS Code servers that generate diagnostics: > > * if a language is single file only (for example HTML) then diagnostics are cleared by the server when the file is closed. > * if a language has a project system (for example C#) diagnostics are not cleared when a file closes. When a project is opened all diagnostics for all files are recomputed (or read from a cache). > > When a file changes it is the server’s responsibility to re-compute diagnostics and push them to the client. If the computed set is empty it has to push the empty array to clear former diagnostics. Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side. So for projects we should not clear any diagnostics from cargo/json projects. Our "standalone file" mode is in a way a project too, with sysroot attached and a potential support for dynamic standalone files. Co-authored-by: Kirill Bulatov <[email protected]>
2 parents cfb7d3a + 8ed9724 commit f0db648

File tree

1 file changed

+2
-15
lines changed

1 file changed

+2
-15
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,9 @@ impl GlobalState {
627627
Ok(())
628628
})?
629629
.on::<lsp_types::notification::DidCloseTextDocument>(|this, params| {
630-
let mut version = None;
631630
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
632-
match this.mem_docs.remove(&path) {
633-
Some(doc) => version = Some(doc.version),
634-
None => log::error!("orphan DidCloseTextDocument: {}", path),
631+
if this.mem_docs.remove(&path).is_none() {
632+
log::error!("orphan DidCloseTextDocument: {}", path);
635633
}
636634

637635
this.semantic_tokens_cache.lock().remove(&params.text_document.uri);
@@ -640,17 +638,6 @@ impl GlobalState {
640638
this.loader.handle.invalidate(path.to_path_buf());
641639
}
642640
}
643-
644-
// Clear the diagnostics for the previously known version of the file.
645-
// This prevents stale "cargo check" diagnostics if the file is
646-
// closed, "cargo check" is run and then the file is reopened.
647-
this.send_notification::<lsp_types::notification::PublishDiagnostics>(
648-
lsp_types::PublishDiagnosticsParams {
649-
uri: params.text_document.uri,
650-
diagnostics: Vec::new(),
651-
version,
652-
},
653-
);
654641
Ok(())
655642
})?
656643
.on::<lsp_types::notification::DidSaveTextDocument>(|this, params| {

0 commit comments

Comments
 (0)