Skip to content

Commit 5d961af

Browse files
committed
Add a clear diagnostics command to flycheck.
This will allow for watcher-style flychecks. Currently, we can only run flychecks on every save. This allows for a long-running flycheck that can update diagnostics between file saves (eg. update on every build). Rationale: I'm trying to improve the experience developing using bazel + rust (see [issue](bazelbuild/rules_rust#1657)). The developer experience I'm looking at is: 1. Dev invokes "ibazel build //:my_crate //:my_crate_test". This ensures that whenever the source code changes, it rebuilds. both the crate and the test for the crate. 2. The discover project command is automatically invoked when you open main.rs, which generates a file listing the `.rustc-output` files mentioned below. 3. Dev modifies `my_crate/src/main.rs` and saves in vscode. 4. bazel is automatically invoked on save, and under the hood invokes rustc, which generates `bazel-out/k8-fastbuild/bin/my_crate/my_crate.rustc-output` and `bazel-out/k8-fastbuild/bin/my_crate_test/my_crate_test.rustc_output` for the original crate and the test crate respectively. 5. The non-test crate finishes building 6. A watcher script would see that `my_crate.rustc-output` has been updated, and update the rust-analyzer result for the file. 7. The test crate finishes building 8. The watcher script would see that `my_crate_test.rustc-output` has been updated, and update the rust-analyzer result for the file.
1 parent bab80da commit 5d961af

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

crates/flycheck/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ impl FlycheckHandle {
113113
}
114114

115115
pub enum Message {
116+
/// Request clearing diagnostics for a file.
117+
ClearDiagnostics{ id: usize },
116118
/// Request adding a diagnostic with fixes included to a file
117119
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },
118120

@@ -136,6 +138,9 @@ impl fmt::Debug for Message {
136138
Message::Progress { id, progress } => {
137139
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
138140
}
141+
Message::ClearDiagnostics {id} => {
142+
f.debug_struct("Clear").field("id", id).finish()
143+
}
139144
}
140145
}
141146
}
@@ -264,6 +269,10 @@ impl FlycheckActor {
264269
self.report_progress(Progress::DidCheckCrate(msg.target.name));
265270
}
266271

272+
CargoMessage::ClearDiagnostics => {
273+
self.send(Message::ClearDiagnostics{id: self.id});
274+
}
275+
267276
CargoMessage::Diagnostic(msg) => {
268277
tracing::trace!(
269278
flycheck_id = self.id,
@@ -461,6 +470,9 @@ impl CargoActor {
461470
let mut read_at_least_one_stdout_message = false;
462471
let mut read_at_least_one_stderr_message = false;
463472
let process_line = |line: &str, error: &mut String| {
473+
if line == "CLEAR" {
474+
self.sender.send(CargoMessage::ClearDiagnostics).unwrap();
475+
}
464476
// Try to deserialize a message from Cargo or Rustc.
465477
let mut deserializer = serde_json::Deserializer::from_str(line);
466478
deserializer.disable_recursion_limit();
@@ -516,6 +528,7 @@ impl CargoActor {
516528
enum CargoMessage {
517529
CompilerArtifact(cargo_metadata::Artifact),
518530
Diagnostic(Diagnostic),
531+
ClearDiagnostics,
519532
}
520533

521534
#[derive(Deserialize)]

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ impl GlobalState {
556556

557557
fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
558558
match message {
559+
flycheck::Message::ClearDiagnostics{id} => self.diagnostics.clear_check(id),
559560
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
560561
let snap = self.snapshot();
561562
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(

0 commit comments

Comments
 (0)