Skip to content

Commit 0d19ccb

Browse files
committed
Make project loading errors less intrusive
1 parent 116775b commit 0d19ccb

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ impl fmt::Debug for Event {
111111

112112
impl GlobalState {
113113
fn run(mut self, inbox: Receiver<lsp_server::Message>) -> Result<()> {
114-
if self.config.linked_projects().is_empty()
115-
&& self.config.detached_files().is_empty()
116-
&& self.config.notifications().cargo_toml_not_found
117-
{
118-
self.show_and_log_error("rust-analyzer failed to discover workspace".to_string(), None);
119-
};
114+
self.update_status_or_notify();
120115

121116
if self.config.did_save_text_document_dynamic_registration() {
122117
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
@@ -394,25 +389,28 @@ impl GlobalState {
394389
});
395390
}
396391

392+
self.update_status_or_notify();
393+
394+
let loop_duration = loop_start.elapsed();
395+
if loop_duration > Duration::from_millis(100) && was_quiescent {
396+
tracing::warn!("overly long loop turn: {:?}", loop_duration);
397+
self.poke_rust_analyzer_developer(format!("overly long loop turn: {loop_duration:?}"));
398+
}
399+
Ok(())
400+
}
401+
402+
fn update_status_or_notify(&mut self) {
397403
let status = self.current_status();
398404
if self.last_reported_status.as_ref() != Some(&status) {
399405
self.last_reported_status = Some(status.clone());
400406

401407
if self.config.server_status_notification() {
402408
self.send_notification::<lsp_ext::ServerStatusNotification>(status);
403-
} else {
404-
if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
405-
self.show_message(lsp_types::MessageType::ERROR, message.clone());
406-
}
409+
} else if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message)
410+
{
411+
self.show_and_log_error(message.clone(), None);
407412
}
408413
}
409-
410-
let loop_duration = loop_start.elapsed();
411-
if loop_duration > Duration::from_millis(100) && was_quiescent {
412-
tracing::warn!("overly long loop turn: {:?}", loop_duration);
413-
self.poke_rust_analyzer_developer(format!("overly long loop turn: {loop_duration:?}"));
414-
}
415-
Ok(())
416414
}
417415

418416
fn handle_task(&mut self, prime_caches_progress: &mut Vec<PrimeCachesProgress>, task: Task) {
@@ -445,6 +443,9 @@ impl GlobalState {
445443
ProjectWorkspaceProgress::Report(msg) => (Progress::Report, Some(msg)),
446444
ProjectWorkspaceProgress::End(workspaces) => {
447445
self.fetch_workspaces_queue.op_completed(Some(workspaces));
446+
if let Err(e) = self.fetch_workspace_error() {
447+
tracing::error!("FetchWorkspaceError:\n{e}")
448+
}
448449

449450
let old = Arc::clone(&self.workspaces);
450451
self.switch_workspaces("fetched workspace".to_string());
@@ -466,6 +467,9 @@ impl GlobalState {
466467
BuildDataProgress::Report(msg) => (Some(Progress::Report), Some(msg)),
467468
BuildDataProgress::End(build_data_result) => {
468469
self.fetch_build_data_queue.op_completed(build_data_result);
470+
if let Err(e) = self.fetch_build_data_error() {
471+
tracing::error!("FetchBuildDataError:\n{e}")
472+
}
469473

470474
self.switch_workspaces("fetched build data".to_string());
471475

@@ -491,6 +495,7 @@ impl GlobalState {
491495
}
492496
}
493497
}
498+
vfs::loader::Message::Progress { n_total: 0, .. } => {}
494499
vfs::loader::Message::Progress { n_total, n_done, config_version } => {
495500
always!(config_version <= self.vfs_config_version);
496501

@@ -554,7 +559,10 @@ impl GlobalState {
554559
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
555560
flycheck::Progress::DidCancel => (Progress::End, None),
556561
flycheck::Progress::DidFailToRestart(err) => {
557-
self.show_and_log_error("cargo check failed".to_string(), Some(err));
562+
self.show_and_log_error(
563+
"cargo check failed to start".to_string(),
564+
Some(err),
565+
);
558566
return;
559567
}
560568
flycheck::Progress::DidFinish(result) => {

crates/rust-analyzer/src/reload.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,19 @@ impl GlobalState {
108108
status.message = Some("Workspace reload required".to_string())
109109
}
110110

111-
if let Err(error) = self.fetch_workspace_error() {
111+
if let Err(_) = self.fetch_workspace_error() {
112112
status.health = lsp_ext::Health::Error;
113-
status.message = Some(error)
113+
status.message = Some("Failed to load workspaces".to_string())
114114
}
115115

116116
if self.config.linked_projects().is_empty()
117117
&& self.config.detached_files().is_empty()
118118
&& self.config.notifications().cargo_toml_not_found
119119
{
120120
status.health = lsp_ext::Health::Warning;
121-
status.message = Some("Workspace reload required".to_string())
121+
status.message = Some("Failed to discover workspace".to_string())
122122
}
123+
123124
status
124125
}
125126

@@ -201,21 +202,14 @@ impl GlobalState {
201202
let _p = profile::span("GlobalState::switch_workspaces");
202203
tracing::info!(%cause, "will switch workspaces");
203204

204-
if let Err(error_message) = self.fetch_workspace_error() {
205-
if !self.config.server_status_notification() {
206-
self.show_and_log_error(error_message, None);
207-
}
205+
if let Err(_) = self.fetch_workspace_error() {
208206
if !self.workspaces.is_empty() {
209207
// It only makes sense to switch to a partially broken workspace
210208
// if we don't have any workspace at all yet.
211209
return;
212210
}
213211
}
214212

215-
if let Err(error) = self.fetch_build_data_error() {
216-
self.show_and_log_error("failed to run build scripts".to_string(), Some(error));
217-
}
218-
219213
let Some(workspaces) = self.fetch_workspaces_queue.last_op_result() else { return; };
220214
let workspaces =
221215
workspaces.iter().filter_map(|res| res.as_ref().ok().cloned()).collect::<Vec<_>>();
@@ -394,7 +388,7 @@ impl GlobalState {
394388
tracing::info!("did switch workspaces");
395389
}
396390

397-
fn fetch_workspace_error(&self) -> Result<(), String> {
391+
pub(super) fn fetch_workspace_error(&self) -> Result<(), String> {
398392
let mut buf = String::new();
399393

400394
let Some(last_op_result) = self.fetch_workspaces_queue.last_op_result() else { return Ok(()) };
@@ -415,7 +409,7 @@ impl GlobalState {
415409
Err(buf)
416410
}
417411

418-
fn fetch_build_data_error(&self) -> Result<(), String> {
412+
pub(super) fn fetch_build_data_error(&self) -> Result<(), String> {
419413
let mut buf = String::new();
420414

421415
for ws in &self.fetch_build_data_queue.last_op_result().1 {

0 commit comments

Comments
 (0)