Skip to content

Commit 27d5ecc

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 27d5ecc

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,9 @@ impl FlycheckHandle {
113113
}
114114

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

@@ -136,6 +139,7 @@ impl fmt::Debug for Message {
136139
Message::Progress { id, progress } => {
137140
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
138141
}
142+
Message::ClearDiagnostics { id } => f.debug_struct("Clear").field("id", id).finish(),
139143
}
140144
}
141145
}
@@ -264,6 +268,10 @@ impl FlycheckActor {
264268
self.report_progress(Progress::DidCheckCrate(msg.target.name));
265269
}
266270

271+
CargoMessage::ClearDiagnostics => {
272+
self.send(Message::ClearDiagnostics { id: self.id });
273+
}
274+
267275
CargoMessage::Diagnostic(msg) => {
268276
tracing::trace!(
269277
flycheck_id = self.id,
@@ -461,6 +469,10 @@ impl CargoActor {
461469
let mut read_at_least_one_stdout_message = false;
462470
let mut read_at_least_one_stderr_message = false;
463471
let process_line = |line: &str, error: &mut String| {
472+
if line == "CLEAR" {
473+
self.sender.send(CargoMessage::ClearDiagnostics).unwrap();
474+
return true;
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)