Skip to content

Commit dc6aa05

Browse files
committed
Auto merge of rust-lang#12009 - matklad:debug-reloads, r=matklad
internal: more visibility into why things happen
2 parents 74cbc20 + 3f4235d commit dc6aa05

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

crates/rust-analyzer/src/global_state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ impl GlobalState {
192192
if let Some(path) = vfs.file_path(file.file_id).as_path() {
193193
let path = path.to_path_buf();
194194
if reload::should_refresh_for_change(&path, file.change_kind) {
195-
self.fetch_workspaces_queue.request_op();
195+
self.fetch_workspaces_queue
196+
.request_op(format!("vfs file change: {}", path.display()));
196197
}
197198
fs_changes.push((path, file.change_kind));
198199
if file.is_created_or_deleted() {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ impl GlobalState {
149149
);
150150
}
151151

152-
self.fetch_workspaces_queue.request_op();
153-
if self.fetch_workspaces_queue.should_start_op() {
154-
self.fetch_workspaces();
152+
self.fetch_workspaces_queue.request_op("startup".to_string());
153+
if let Some(cause) = self.fetch_workspaces_queue.should_start_op() {
154+
self.fetch_workspaces(cause);
155155
}
156156

157157
while let Some(event) = self.next_event(&inbox) {
@@ -240,7 +240,8 @@ impl GlobalState {
240240
let workspaces_updated = !Arc::ptr_eq(&old, &self.workspaces);
241241

242242
if self.config.run_build_scripts() && workspaces_updated {
243-
self.fetch_build_data_queue.request_op()
243+
self.fetch_build_data_queue
244+
.request_op(format!("workspace updated"));
244245
}
245246

246247
(Progress::End, None)
@@ -312,7 +313,8 @@ impl GlobalState {
312313

313314
self.prime_caches_queue.op_completed(());
314315
if cancelled {
315-
self.prime_caches_queue.request_op();
316+
self.prime_caches_queue
317+
.request_op("restart after cancelation".to_string());
316318
}
317319
}
318320
};
@@ -443,7 +445,7 @@ impl GlobalState {
443445
flycheck.update();
444446
}
445447
if self.config.prefill_caches() {
446-
self.prime_caches_queue.request_op();
448+
self.prime_caches_queue.request_op("became quiescent".to_string());
447449
}
448450
}
449451

@@ -493,14 +495,15 @@ impl GlobalState {
493495
}
494496

495497
if self.config.cargo_autoreload() {
496-
if self.fetch_workspaces_queue.should_start_op() {
497-
self.fetch_workspaces();
498+
if let Some(cause) = self.fetch_workspaces_queue.should_start_op() {
499+
self.fetch_workspaces(cause);
498500
}
499501
}
500-
if self.fetch_build_data_queue.should_start_op() {
501-
self.fetch_build_data();
502+
if let Some(cause) = self.fetch_build_data_queue.should_start_op() {
503+
self.fetch_build_data(cause);
502504
}
503-
if self.prime_caches_queue.should_start_op() {
505+
if let Some(cause) = self.prime_caches_queue.should_start_op() {
506+
tracing::debug!(%cause, "will prime caches");
504507
let num_worker_threads = self.config.prime_caches_num_threads();
505508

506509
self.task_pool.handle.spawn_with_sender({
@@ -569,7 +572,7 @@ impl GlobalState {
569572

570573
RequestDispatcher { req: Some(req), global_state: self }
571574
.on_sync_mut::<lsp_ext::ReloadWorkspace>(|s, ()| {
572-
s.fetch_workspaces_queue.request_op();
575+
s.fetch_workspaces_queue.request_op("reload workspace request".to_string());
573576
Ok(())
574577
})?
575578
.on_sync_mut::<lsp_types::request::Shutdown>(|s, ()| {
@@ -714,7 +717,7 @@ impl GlobalState {
714717
}
715718
if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) {
716719
if reload::should_refresh_for_change(&abs_path, ChangeKind::Modify) {
717-
this.fetch_workspaces_queue.request_op();
720+
this.fetch_workspaces_queue.request_op(format!("DidSaveTextDocument {}", abs_path.display()));
718721
}
719722
}
720723
Ok(())

crates/rust-analyzer/src/op_queue.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
//! Bookkeeping to make sure only one long-running operation is being executed
22
//! at a time.
33
4+
pub(crate) type Cause = String;
5+
46
pub(crate) struct OpQueue<Output> {
5-
op_requested: bool,
7+
op_requested: Option<Cause>,
68
op_in_progress: bool,
79
last_op_result: Output,
810
}
911

1012
impl<Output: Default> Default for OpQueue<Output> {
1113
fn default() -> Self {
12-
Self { op_requested: false, op_in_progress: false, last_op_result: Default::default() }
14+
Self { op_requested: None, op_in_progress: false, last_op_result: Default::default() }
1315
}
1416
}
1517

1618
impl<Output> OpQueue<Output> {
17-
pub(crate) fn request_op(&mut self) {
18-
self.op_requested = true;
19+
pub(crate) fn request_op(&mut self, reason: Cause) {
20+
self.op_requested = Some(reason);
1921
}
20-
pub(crate) fn should_start_op(&mut self) -> bool {
22+
pub(crate) fn should_start_op(&mut self) -> Option<Cause> {
2123
if self.op_in_progress {
22-
return false;
24+
return None;
2325
}
24-
self.op_in_progress = self.op_requested;
25-
self.op_requested = false;
26-
self.op_in_progress
26+
self.op_in_progress = self.op_requested.is_some();
27+
self.op_requested.take()
2728
}
2829
pub(crate) fn op_completed(&mut self, result: Output) {
2930
assert!(self.op_in_progress);
@@ -38,6 +39,6 @@ impl<Output> OpQueue<Output> {
3839
self.op_in_progress
3940
}
4041
pub(crate) fn op_requested(&self) -> bool {
41-
self.op_requested
42+
self.op_requested.is_some()
4243
}
4344
}

crates/rust-analyzer/src/reload.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
global_state::GlobalState,
1919
lsp_ext,
2020
main_loop::Task,
21+
op_queue::Cause,
2122
};
2223

2324
#[derive(Debug)]
@@ -49,7 +50,7 @@ impl GlobalState {
4950
self.analysis_host.update_lru_capacity(self.config.lru_capacity());
5051
}
5152
if self.config.linked_projects() != old_config.linked_projects() {
52-
self.fetch_workspaces_queue.request_op()
53+
self.fetch_workspaces_queue.request_op("linked projects changed".to_string())
5354
} else if self.config.flycheck() != old_config.flycheck() {
5455
self.reload_flycheck();
5556
}
@@ -92,8 +93,8 @@ impl GlobalState {
9293
status
9394
}
9495

95-
pub(crate) fn fetch_workspaces(&mut self) {
96-
tracing::info!("will fetch workspaces");
96+
pub(crate) fn fetch_workspaces(&mut self, cause: Cause) {
97+
tracing::info!(%cause, "will fetch workspaces");
9798

9899
self.task_pool.handle.spawn_with_sender({
99100
let linked_projects = self.config.linked_projects();
@@ -144,7 +145,8 @@ impl GlobalState {
144145
});
145146
}
146147

147-
pub(crate) fn fetch_build_data(&mut self) {
148+
pub(crate) fn fetch_build_data(&mut self, cause: Cause) {
149+
tracing::debug!(%cause, "will fetch build data");
148150
let workspaces = Arc::clone(&self.workspaces);
149151
let config = self.config.cargo();
150152
self.task_pool.handle.spawn_with_sender(move |sender| {

0 commit comments

Comments
 (0)