Skip to content

Commit 1787c14

Browse files
committed
Auto merge of rust-lang#14358 - Veykril:err-reporting, r=Veykril
Report sysroot and rustc crate loading errors Also aggregates the warnings and errors so we don't discard previous ones. cc rust-lang/rust-analyzer#14327
2 parents 579797f + d9c7d28 commit 1787c14

File tree

13 files changed

+194
-157
lines changed

13 files changed

+194
-157
lines changed

crates/project-model/src/cargo_workspace.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ops::Index<Target> for CargoWorkspace {
5050

5151
/// Describes how to set the rustc source directory.
5252
#[derive(Clone, Debug, PartialEq, Eq)]
53-
pub enum RustcSource {
53+
pub enum RustLibSource {
5454
/// Explicit path for the rustc source directory.
5555
Path(AbsPathBuf),
5656
/// Try to automatically detect where the rustc source directory is.
@@ -95,10 +95,10 @@ pub struct CargoConfig {
9595
/// rustc target
9696
pub target: Option<String>,
9797
/// Sysroot loading behavior
98-
pub sysroot: Option<RustcSource>,
98+
pub sysroot: Option<RustLibSource>,
9999
pub sysroot_src: Option<AbsPathBuf>,
100100
/// rustc private crate source
101-
pub rustc_source: Option<RustcSource>,
101+
pub rustc_source: Option<RustLibSource>,
102102
/// crates to disable `#[cfg(test)]` on
103103
pub unset_test_crates: UnsetTestCrates,
104104
/// Invoke `cargo check` through the RUSTC_WRAPPER.

crates/project-model/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub use crate::{
4444
build_scripts::WorkspaceBuildScripts,
4545
cargo_workspace::{
4646
CargoConfig, CargoFeatures, CargoWorkspace, Package, PackageData, PackageDependency,
47-
RustcSource, Target, TargetData, TargetKind, UnsetTestCrates,
47+
RustLibSource, Target, TargetData, TargetKind, UnsetTestCrates,
4848
},
4949
manifest_path::ManifestPath,
5050
project_json::{ProjectJson, ProjectJsonData},

crates/project-model/src/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
2424
let project_workspace = ProjectWorkspace::Cargo {
2525
cargo: cargo_workspace,
2626
build_scripts: WorkspaceBuildScripts::default(),
27-
sysroot: None,
28-
rustc: None,
27+
sysroot: Err(None),
28+
rustc: Err(None),
2929
rustc_cfg: Vec::new(),
3030
cfg_overrides,
3131
toolchain: None,
@@ -37,7 +37,7 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
3737
fn load_rust_project(file: &str) -> CrateGraph {
3838
let data = get_test_json_file(file);
3939
let project = rooted_project_json(data);
40-
let sysroot = Some(get_fake_sysroot());
40+
let sysroot = Ok(get_fake_sysroot());
4141
let project_workspace = ProjectWorkspace::Json { project, sysroot, rustc_cfg: Vec::new() };
4242
to_crate_graph(project_workspace)
4343
}

crates/project-model/src/workspace.rs

+94-115
Large diffs are not rendered by default.

crates/rust-analyzer/src/cli/analysis_stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ide_db::base_db::{
2424
use itertools::Itertools;
2525
use oorandom::Rand32;
2626
use profile::{Bytes, StopWatch};
27-
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustcSource};
27+
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
2828
use rayon::prelude::*;
2929
use rustc_hash::FxHashSet;
3030
use stdx::format_to;
@@ -57,7 +57,7 @@ impl flags::AnalysisStats {
5757
let mut cargo_config = CargoConfig::default();
5858
cargo_config.sysroot = match self.no_sysroot {
5959
true => None,
60-
false => Some(RustcSource::Discover),
60+
false => Some(RustLibSource::Discover),
6161
};
6262
let no_progress = &|_| ();
6363

crates/rust-analyzer/src/cli/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Analyze all modules in a project for diagnostics. Exits with a non-zero
22
//! status code if any errors are found.
33
4-
use project_model::{CargoConfig, RustcSource};
4+
use project_model::{CargoConfig, RustLibSource};
55
use rustc_hash::FxHashSet;
66

77
use hir::{db::HirDatabase, Crate, Module};
@@ -16,7 +16,7 @@ use crate::cli::{
1616
impl flags::Diagnostics {
1717
pub fn run(self) -> anyhow::Result<()> {
1818
let mut cargo_config = CargoConfig::default();
19-
cargo_config.sysroot = Some(RustcSource::Discover);
19+
cargo_config.sysroot = Some(RustLibSource::Discover);
2020
let load_cargo_config = LoadCargoConfig {
2121
load_out_dirs_from_check: !self.disable_build_scripts,
2222
with_proc_macro_server: ProcMacroServerChoice::Sysroot,

crates/rust-analyzer/src/cli/lsif.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ide_db::LineIndexDatabase;
1313
use ide_db::base_db::salsa::{self, ParallelDatabase};
1414
use ide_db::line_index::WideEncoding;
1515
use lsp_types::{self, lsif};
16-
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustcSource};
16+
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
1717
use vfs::{AbsPathBuf, Vfs};
1818

1919
use crate::cli::load_cargo::ProcMacroServerChoice;
@@ -290,7 +290,7 @@ impl flags::Lsif {
290290
eprintln!("Generating LSIF started...");
291291
let now = Instant::now();
292292
let mut cargo_config = CargoConfig::default();
293-
cargo_config.sysroot = Some(RustcSource::Discover);
293+
cargo_config.sysroot = Some(RustLibSource::Discover);
294294
let no_progress = &|_| ();
295295
let load_cargo_config = LoadCargoConfig {
296296
load_out_dirs_from_check: true,

crates/rust-analyzer/src/cli/scip.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ide::{
1515
TokenStaticData,
1616
};
1717
use ide_db::LineIndexDatabase;
18-
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustcSource};
18+
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
1919
use scip::types as scip_types;
2020
use std::env;
2121

@@ -30,7 +30,7 @@ impl flags::Scip {
3030
eprintln!("Generating SCIP start...");
3131
let now = Instant::now();
3232
let mut cargo_config = CargoConfig::default();
33-
cargo_config.sysroot = Some(RustcSource::Discover);
33+
cargo_config.sysroot = Some(RustLibSource::Discover);
3434

3535
let no_progress = &|s| (eprintln!("rust-analyzer: Loading {s}"));
3636
let load_cargo_config = LoadCargoConfig {

crates/rust-analyzer/src/cli/ssr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Applies structured search replace rules from the command line.
22
33
use ide_ssr::MatchFinder;
4-
use project_model::{CargoConfig, RustcSource};
4+
use project_model::{CargoConfig, RustLibSource};
55

66
use crate::cli::{
77
flags,
@@ -13,7 +13,7 @@ impl flags::Ssr {
1313
pub fn run(self) -> Result<()> {
1414
use ide_db::base_db::SourceDatabaseExt;
1515
let mut cargo_config = CargoConfig::default();
16-
cargo_config.sysroot = Some(RustcSource::Discover);
16+
cargo_config.sysroot = Some(RustLibSource::Discover);
1717
let load_cargo_config = LoadCargoConfig {
1818
load_out_dirs_from_check: true,
1919
with_proc_macro_server: ProcMacroServerChoice::Sysroot,

crates/rust-analyzer/src/config.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ide_db::{
2222
use itertools::Itertools;
2323
use lsp_types::{ClientCapabilities, MarkupKind};
2424
use project_model::{
25-
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustcSource,
25+
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustLibSource,
2626
UnsetTestCrates,
2727
};
2828
use rustc_hash::{FxHashMap, FxHashSet};
@@ -1137,16 +1137,16 @@ impl Config {
11371137
pub fn cargo(&self) -> CargoConfig {
11381138
let rustc_source = self.data.rustc_source.as_ref().map(|rustc_src| {
11391139
if rustc_src == "discover" {
1140-
RustcSource::Discover
1140+
RustLibSource::Discover
11411141
} else {
1142-
RustcSource::Path(self.root_path.join(rustc_src))
1142+
RustLibSource::Path(self.root_path.join(rustc_src))
11431143
}
11441144
});
11451145
let sysroot = self.data.cargo_sysroot.as_ref().map(|sysroot| {
11461146
if sysroot == "discover" {
1147-
RustcSource::Discover
1147+
RustLibSource::Discover
11481148
} else {
1149-
RustcSource::Path(self.root_path.join(sysroot))
1149+
RustLibSource::Path(self.root_path.join(sysroot))
11501150
}
11511151
});
11521152
let sysroot_src =

crates/rust-analyzer/src/lsp_utils.rs

+35-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,41 @@ impl Progress {
3636
}
3737

3838
impl GlobalState {
39-
pub(crate) fn show_message(&mut self, typ: lsp_types::MessageType, message: String) {
40-
let message = message;
41-
self.send_notification::<lsp_types::notification::ShowMessage>(
42-
lsp_types::ShowMessageParams { typ, message },
43-
)
39+
pub(crate) fn show_message(
40+
&mut self,
41+
typ: lsp_types::MessageType,
42+
message: String,
43+
show_open_log_button: bool,
44+
) {
45+
match self.config.open_server_logs() && show_open_log_button {
46+
true => self.send_request::<lsp_types::request::ShowMessageRequest>(
47+
lsp_types::ShowMessageRequestParams {
48+
typ,
49+
message,
50+
actions: Some(vec![lsp_types::MessageActionItem {
51+
title: "Open server logs".to_owned(),
52+
properties: Default::default(),
53+
}]),
54+
},
55+
|this, resp| {
56+
let lsp_server::Response { error: None, result: Some(result), .. } = resp
57+
else { return };
58+
if let Ok(Some(_item)) = crate::from_json::<
59+
<lsp_types::request::ShowMessageRequest as lsp_types::request::Request>::Result,
60+
>(
61+
lsp_types::request::ShowMessageRequest::METHOD, &result
62+
) {
63+
this.send_notification::<lsp_ext::OpenServerLogs>(());
64+
}
65+
},
66+
),
67+
false => self.send_notification::<lsp_types::notification::ShowMessage>(
68+
lsp_types::ShowMessageParams {
69+
typ,
70+
message,
71+
},
72+
),
73+
}
4474
}
4575

4676
/// Sends a notification to the client containing the error `message`.

crates/rust-analyzer/src/main_loop.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,19 @@ impl GlobalState {
406406

407407
if self.config.server_status_notification() {
408408
self.send_notification::<lsp_ext::ServerStatusNotification>(status);
409-
} else if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message)
410-
{
411-
self.show_and_log_error(message.clone(), None);
409+
} else if let (health, Some(message)) = (status.health, &status.message) {
410+
let open_log_button = tracing::enabled!(tracing::Level::ERROR)
411+
&& (self.fetch_build_data_error().is_err()
412+
|| self.fetch_workspace_error().is_err());
413+
self.show_message(
414+
match health {
415+
lsp_ext::Health::Ok => lsp_types::MessageType::INFO,
416+
lsp_ext::Health::Warning => lsp_types::MessageType::WARNING,
417+
lsp_ext::Health::Error => lsp_types::MessageType::ERROR,
418+
},
419+
message.clone(),
420+
open_log_button,
421+
);
412422
}
413423
}
414424
}
@@ -919,6 +929,7 @@ impl GlobalState {
919929
this.show_message(
920930
lsp_types::MessageType::WARNING,
921931
error.to_string(),
932+
false,
922933
);
923934
}
924935
this.update_configuration(config);

crates/rust-analyzer/src/reload.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,55 @@ impl GlobalState {
9090
quiescent: self.is_quiescent(),
9191
message: None,
9292
};
93+
let mut message = String::new();
9394

9495
if self.proc_macro_changed {
9596
status.health = lsp_ext::Health::Warning;
96-
status.message =
97-
Some("Reload required due to source changes of a procedural macro.".into())
97+
message.push_str("Reload required due to source changes of a procedural macro.\n\n");
9898
}
9999
if let Err(_) = self.fetch_build_data_error() {
100100
status.health = lsp_ext::Health::Warning;
101-
status.message =
102-
Some("Failed to run build scripts of some packages, check the logs.".to_string());
101+
message.push_str("Failed to run build scripts of some packages.\n\n");
103102
}
104103
if !self.config.cargo_autoreload()
105104
&& self.is_quiescent()
106105
&& self.fetch_workspaces_queue.op_requested()
107106
{
108107
status.health = lsp_ext::Health::Warning;
109-
status.message = Some("Workspace reload required".to_string())
108+
message.push_str("Auto-reloading is disabled and the workspace has changed, a manual workspace reload is required.\n\n");
110109
}
111-
112-
if let Err(_) = self.fetch_workspace_error() {
113-
status.health = lsp_ext::Health::Error;
114-
status.message = Some("Failed to load workspaces".to_string())
115-
}
116-
117110
if self.config.linked_projects().is_empty()
118111
&& self.config.detached_files().is_empty()
119112
&& self.config.notifications().cargo_toml_not_found
120113
{
121114
status.health = lsp_ext::Health::Warning;
122-
status.message = Some("Failed to discover workspace".to_string())
115+
message.push_str("Failed to discover workspace.\n\n");
116+
}
117+
118+
for ws in self.workspaces.iter() {
119+
let (ProjectWorkspace::Cargo { sysroot, .. }
120+
| ProjectWorkspace::Json { sysroot, .. }
121+
| ProjectWorkspace::DetachedFiles { sysroot, .. }) = ws;
122+
if let Err(Some(e)) = sysroot {
123+
status.health = lsp_ext::Health::Warning;
124+
message.push_str(e);
125+
message.push_str("\n\n");
126+
}
127+
if let ProjectWorkspace::Cargo { rustc: Err(Some(e)), .. } = ws {
128+
status.health = lsp_ext::Health::Warning;
129+
message.push_str(e);
130+
message.push_str("\n\n");
131+
}
123132
}
124133

134+
if let Err(_) = self.fetch_workspace_error() {
135+
status.health = lsp_ext::Health::Error;
136+
message.push_str("Failed to load workspaces.\n\n");
137+
}
138+
139+
if !message.is_empty() {
140+
status.message = Some(message.trim_end().to_owned());
141+
}
125142
status
126143
}
127144

0 commit comments

Comments
 (0)