Skip to content

Commit c1ea2b5

Browse files
committed
Auto merge of rust-lang#17302 - mladedav:dm/fix-clear, r=Veykril
fix diagnostics clearing when flychecks run per-workspace This might be causing rust-lang#17300 or it's a different bug with the same functionality. I wonder if the decision to clear diagnostics should stay in the main loop or maybe the flycheck itself should track it and tell the mainloop? I have used a hash map but we could just as well use a vector since the IDs are `usizes` in some given range starting at 0. It would be probably faster but this just felt a bit cleaner and it allows us to change the ID to newtype later and we can just use a hasher that returns the underlying integer.
2 parents d4a5cb9 + f476d37 commit c1ea2b5

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/tools/rust-analyzer/crates/flycheck/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub enum Message {
163163
/// Request adding a diagnostic with fixes included to a file
164164
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },
165165

166+
/// Request clearing all previous diagnostics
167+
ClearDiagnostics { id: usize },
168+
166169
/// Request check progress notification to client
167170
Progress {
168171
/// Flycheck instance ID
@@ -180,6 +183,9 @@ impl fmt::Debug for Message {
180183
.field("workspace_root", workspace_root)
181184
.field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
182185
.finish(),
186+
Message::ClearDiagnostics { id } => {
187+
f.debug_struct("ClearDiagnostics").field("id", id).finish()
188+
}
183189
Message::Progress { id, progress } => {
184190
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
185191
}
@@ -220,13 +226,22 @@ struct FlycheckActor {
220226
command_handle: Option<CommandHandle<CargoCheckMessage>>,
221227
/// The receiver side of the channel mentioned above.
222228
command_receiver: Option<Receiver<CargoCheckMessage>>,
229+
230+
status: FlycheckStatus,
223231
}
224232

225233
enum Event {
226234
RequestStateChange(StateChange),
227235
CheckEvent(Option<CargoCheckMessage>),
228236
}
229237

238+
#[derive(PartialEq)]
239+
enum FlycheckStatus {
240+
Started,
241+
DiagnosticSent,
242+
Finished,
243+
}
244+
230245
const SAVED_FILE_PLACEHOLDER: &str = "$saved_file";
231246

232247
impl FlycheckActor {
@@ -248,6 +263,7 @@ impl FlycheckActor {
248263
manifest_path,
249264
command_handle: None,
250265
command_receiver: None,
266+
status: FlycheckStatus::Finished,
251267
}
252268
}
253269

@@ -298,12 +314,14 @@ impl FlycheckActor {
298314
self.command_handle = Some(command_handle);
299315
self.command_receiver = Some(receiver);
300316
self.report_progress(Progress::DidStart);
317+
self.status = FlycheckStatus::Started;
301318
}
302319
Err(error) => {
303320
self.report_progress(Progress::DidFailToRestart(format!(
304321
"Failed to run the following command: {} error={}",
305322
formatted_command, error
306323
)));
324+
self.status = FlycheckStatus::Finished;
307325
}
308326
}
309327
}
@@ -323,7 +341,11 @@ impl FlycheckActor {
323341
error
324342
);
325343
}
344+
if self.status == FlycheckStatus::Started {
345+
self.send(Message::ClearDiagnostics { id: self.id });
346+
}
326347
self.report_progress(Progress::DidFinish(res));
348+
self.status = FlycheckStatus::Finished;
327349
}
328350
Event::CheckEvent(Some(message)) => match message {
329351
CargoCheckMessage::CompilerArtifact(msg) => {
@@ -341,11 +363,15 @@ impl FlycheckActor {
341363
message = msg.message,
342364
"diagnostic received"
343365
);
366+
if self.status == FlycheckStatus::Started {
367+
self.send(Message::ClearDiagnostics { id: self.id });
368+
}
344369
self.send(Message::AddDiagnostic {
345370
id: self.id,
346371
workspace_root: self.root.clone(),
347372
diagnostic: msg,
348373
});
374+
self.status = FlycheckStatus::DiagnosticSent;
349375
}
350376
},
351377
}
@@ -362,6 +388,7 @@ impl FlycheckActor {
362388
);
363389
command_handle.cancel();
364390
self.report_progress(Progress::DidCancel);
391+
self.status = FlycheckStatus::Finished;
365392
}
366393
}
367394

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub(crate) struct GlobalState {
8787
pub(crate) flycheck_sender: Sender<flycheck::Message>,
8888
pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
8989
pub(crate) last_flycheck_error: Option<String>,
90-
pub(crate) diagnostics_received: bool,
9190

9291
// Test explorer
9392
pub(crate) test_run_session: Option<Vec<flycheck::CargoTestHandle>>,
@@ -225,7 +224,6 @@ impl GlobalState {
225224
flycheck_sender,
226225
flycheck_receiver,
227226
last_flycheck_error: None,
228-
diagnostics_received: false,
229227

230228
test_run_session: None,
231229
test_run_sender,

src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,6 @@ impl GlobalState {
804804
fn handle_flycheck_msg(&mut self, message: flycheck::Message) {
805805
match message {
806806
flycheck::Message::AddDiagnostic { id, workspace_root, diagnostic } => {
807-
if !self.diagnostics_received {
808-
self.diagnostics.clear_check(id);
809-
self.diagnostics_received = true;
810-
}
811807
let snap = self.snapshot();
812808
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
813809
&self.config.diagnostics_map(),
@@ -833,12 +829,11 @@ impl GlobalState {
833829
}
834830
}
835831

832+
flycheck::Message::ClearDiagnostics { id } => self.diagnostics.clear_check(id),
833+
836834
flycheck::Message::Progress { id, progress } => {
837835
let (state, message) = match progress {
838-
flycheck::Progress::DidStart => {
839-
self.diagnostics_received = false;
840-
(Progress::Begin, None)
841-
}
836+
flycheck::Progress::DidStart => (Progress::Begin, None),
842837
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
843838
flycheck::Progress::DidCancel => {
844839
self.last_flycheck_error = None;
@@ -852,9 +847,6 @@ impl GlobalState {
852847
flycheck::Progress::DidFinish(result) => {
853848
self.last_flycheck_error =
854849
result.err().map(|err| format!("cargo check failed to start: {err}"));
855-
if !self.diagnostics_received {
856-
self.diagnostics.clear_check(id);
857-
}
858850
(Progress::End, None)
859851
}
860852
};

0 commit comments

Comments
 (0)