Skip to content

Commit

Permalink
Add a clear diagnostics command to flycheck.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matts1 committed Apr 17, 2023
1 parent bab80da commit 27d5ecc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/flycheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ impl FlycheckHandle {
}

pub enum Message {
/// Request clearing diagnostics for a file.
ClearDiagnostics { id: usize },

/// Request adding a diagnostic with fixes included to a file
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },

Expand All @@ -136,6 +139,7 @@ impl fmt::Debug for Message {
Message::Progress { id, progress } => {
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
}
Message::ClearDiagnostics { id } => f.debug_struct("Clear").field("id", id).finish(),
}
}
}
Expand Down Expand Up @@ -264,6 +268,10 @@ impl FlycheckActor {
self.report_progress(Progress::DidCheckCrate(msg.target.name));
}

CargoMessage::ClearDiagnostics => {
self.send(Message::ClearDiagnostics { id: self.id });
}

CargoMessage::Diagnostic(msg) => {
tracing::trace!(
flycheck_id = self.id,
Expand Down Expand Up @@ -461,6 +469,10 @@ impl CargoActor {
let mut read_at_least_one_stdout_message = false;
let mut read_at_least_one_stderr_message = false;
let process_line = |line: &str, error: &mut String| {
if line == "CLEAR" {
self.sender.send(CargoMessage::ClearDiagnostics).unwrap();
return true;
}
// Try to deserialize a message from Cargo or Rustc.
let mut deserializer = serde_json::Deserializer::from_str(line);
deserializer.disable_recursion_limit();
Expand Down Expand Up @@ -516,6 +528,7 @@ impl CargoActor {
enum CargoMessage {
CompilerArtifact(cargo_metadata::Artifact),
Diagnostic(Diagnostic),
ClearDiagnostics,
}

#[derive(Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ impl GlobalState {

fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
match message {
flycheck::Message::ClearDiagnostics { id } => self.diagnostics.clear_check(id),
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
let snap = self.snapshot();
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
Expand Down

0 comments on commit 27d5ecc

Please sign in to comment.