Skip to content

Commit 4182a95

Browse files
committed
fix: Report metadata errors for sysroot
1 parent 0aa39c4 commit 4182a95

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/tools/rust-analyzer/crates/project-model/src/sysroot.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Sysroot {
3030

3131
#[derive(Debug, Clone, Eq, PartialEq)]
3232
pub enum RustLibSrcWorkspace {
33-
Workspace(CargoWorkspace),
33+
Workspace { ws: CargoWorkspace, metadata_err: Option<String> },
3434
Json(ProjectJson),
3535
Stitched(stitched::Stitched),
3636
Empty,
@@ -39,7 +39,9 @@ pub enum RustLibSrcWorkspace {
3939
impl fmt::Display for RustLibSrcWorkspace {
4040
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4141
match self {
42-
RustLibSrcWorkspace::Workspace(ws) => write!(f, "workspace {}", ws.workspace_root()),
42+
RustLibSrcWorkspace::Workspace { ws, .. } => {
43+
write!(f, "workspace {}", ws.workspace_root())
44+
}
4345
RustLibSrcWorkspace::Json(json) => write!(f, "json {}", json.manifest_or_root()),
4446
RustLibSrcWorkspace::Stitched(stitched) => {
4547
write!(f, "stitched with {} crates", stitched.crates.len())
@@ -74,7 +76,7 @@ impl Sysroot {
7476

7577
pub fn is_rust_lib_src_empty(&self) -> bool {
7678
match &self.workspace {
77-
RustLibSrcWorkspace::Workspace(ws) => ws.packages().next().is_none(),
79+
RustLibSrcWorkspace::Workspace { ws, .. } => ws.packages().next().is_none(),
7880
RustLibSrcWorkspace::Json(project_json) => project_json.n_crates() == 0,
7981
RustLibSrcWorkspace::Stitched(stitched) => stitched.crates.is_empty(),
8082
RustLibSrcWorkspace::Empty => true,
@@ -85,9 +87,16 @@ impl Sysroot {
8587
self.error.as_deref()
8688
}
8789

90+
pub fn metadata_error(&self) -> Option<&str> {
91+
match &self.workspace {
92+
RustLibSrcWorkspace::Workspace { metadata_err, .. } => metadata_err.as_deref(),
93+
_ => None,
94+
}
95+
}
96+
8897
pub fn num_packages(&self) -> usize {
8998
match &self.workspace {
90-
RustLibSrcWorkspace::Workspace(ws) => ws.packages().count(),
99+
RustLibSrcWorkspace::Workspace { ws, .. } => ws.packages().count(),
91100
RustLibSrcWorkspace::Json(project_json) => project_json.n_crates(),
92101
RustLibSrcWorkspace::Stitched(stitched) => stitched.crates.len(),
93102
RustLibSrcWorkspace::Empty => 0,
@@ -293,7 +302,9 @@ impl Sysroot {
293302
&& let Some(src_root) = &self.rust_lib_src_root
294303
{
295304
let has_core = match &self.workspace {
296-
RustLibSrcWorkspace::Workspace(ws) => ws.packages().any(|p| ws[p].name == "core"),
305+
RustLibSrcWorkspace::Workspace { ws: workspace, .. } => {
306+
workspace.packages().any(|p| workspace[p].name == "core")
307+
}
297308
RustLibSrcWorkspace::Json(project_json) => project_json
298309
.crates()
299310
.filter_map(|(_, krate)| krate.display_name.clone())
@@ -332,7 +343,7 @@ impl Sysroot {
332343

333344
// Make sure we never attempt to write to the sysroot
334345
let locked = true;
335-
let (mut res, _) =
346+
let (mut res, err) =
336347
FetchMetadata::new(library_manifest, current_dir, &cargo_config, self, no_deps)
337348
.exec(target_dir, locked, progress)?;
338349

@@ -387,7 +398,10 @@ impl Sysroot {
387398

388399
let cargo_workspace =
389400
CargoWorkspace::new(res, library_manifest.clone(), Default::default(), true);
390-
Ok(RustLibSrcWorkspace::Workspace(cargo_workspace))
401+
Ok(RustLibSrcWorkspace::Workspace {
402+
ws: cargo_workspace,
403+
metadata_err: err.map(|e| format!("{e:#}")),
404+
})
391405
}
392406
}
393407

src/tools/rust-analyzer/crates/project-model/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn smoke_test_real_sysroot_cargo() {
248248
sysroot.set_workspace(loaded_sysroot);
249249
}
250250
assert!(
251-
matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace(_)),
251+
matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace { .. }),
252252
"got {}",
253253
sysroot.workspace()
254254
);

src/tools/rust-analyzer/crates/project-model/src/workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ impl ProjectWorkspace {
743743
pub fn to_roots(&self) -> Vec<PackageRoot> {
744744
let mk_sysroot = || {
745745
let mut r = match self.sysroot.workspace() {
746-
RustLibSrcWorkspace::Workspace(ws) => ws
746+
RustLibSrcWorkspace::Workspace { ws, .. } => ws
747747
.packages()
748748
.filter_map(|pkg| {
749749
if ws[pkg].is_local {
@@ -1731,7 +1731,7 @@ fn sysroot_to_crate_graph(
17311731
) -> (SysrootPublicDeps, Option<CrateBuilderId>) {
17321732
let _p = tracing::info_span!("sysroot_to_crate_graph").entered();
17331733
match sysroot.workspace() {
1734-
RustLibSrcWorkspace::Workspace(cargo) => {
1734+
RustLibSrcWorkspace::Workspace { ws: cargo, .. } => {
17351735
let (sysroot_cg, sysroot_pm) = cargo_to_crate_graph(
17361736
load,
17371737
None,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ impl GlobalState {
222222
message.push_str(err);
223223
message.push_str("\n\n");
224224
}
225+
if let Some(err) = ws.sysroot.metadata_error() {
226+
status.health |= lsp_ext::Health::Warning;
227+
format_to!(
228+
message,
229+
"Failed to read Cargo metadata with dependencies for sysroot of `{}`: ",
230+
ws.manifest_or_root()
231+
);
232+
message.push_str(err);
233+
message.push_str("\n\n");
234+
}
225235
if let ProjectWorkspaceKind::Cargo { rustc: Err(Some(err)), .. } = &ws.kind {
226236
status.health |= lsp_ext::Health::Warning;
227237
format_to!(

0 commit comments

Comments
 (0)