Skip to content

Commit fd59d49

Browse files
Merge pull request rust-lang#20866 from ShoyuVanilla/metadata-err
fix: Run `cargo metadata` on sysroot with cwd=sysroot
2 parents 7c871c2 + 4182a95 commit fd59d49

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

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

Lines changed: 22 additions & 9 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,
@@ -210,7 +219,6 @@ impl Sysroot {
210219
&self,
211220
sysroot_source_config: &RustSourceWorkspaceConfig,
212221
no_deps: bool,
213-
current_dir: &AbsPath,
214222
target_dir: &Utf8Path,
215223
progress: &dyn Fn(String),
216224
) -> Option<RustLibSrcWorkspace> {
@@ -224,7 +232,7 @@ impl Sysroot {
224232
if fs::metadata(&library_manifest).is_ok() {
225233
match self.load_library_via_cargo(
226234
&library_manifest,
227-
current_dir,
235+
src_root,
228236
target_dir,
229237
cargo_config,
230238
no_deps,
@@ -294,7 +302,9 @@ impl Sysroot {
294302
&& let Some(src_root) = &self.rust_lib_src_root
295303
{
296304
let has_core = match &self.workspace {
297-
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+
}
298308
RustLibSrcWorkspace::Json(project_json) => project_json
299309
.crates()
300310
.filter_map(|(_, krate)| krate.display_name.clone())
@@ -333,7 +343,7 @@ impl Sysroot {
333343

334344
// Make sure we never attempt to write to the sysroot
335345
let locked = true;
336-
let (mut res, _) =
346+
let (mut res, err) =
337347
FetchMetadata::new(library_manifest, current_dir, &cargo_config, self, no_deps)
338348
.exec(target_dir, locked, progress)?;
339349

@@ -388,7 +398,10 @@ impl Sysroot {
388398

389399
let cargo_workspace =
390400
CargoWorkspace::new(res, library_manifest.clone(), Default::default(), true);
391-
Ok(RustLibSrcWorkspace::Workspace(cargo_workspace))
401+
Ok(RustLibSrcWorkspace::Workspace {
402+
ws: cargo_workspace,
403+
metadata_err: err.map(|e| format!("{e:#}")),
404+
})
392405
}
393406
}
394407

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,14 @@ fn smoke_test_real_sysroot_cargo() {
241241
let loaded_sysroot = sysroot.load_workspace(
242242
&RustSourceWorkspaceConfig::default_cargo(),
243243
false,
244-
&cwd,
245244
&Utf8PathBuf::default(),
246245
&|_| (),
247246
);
248247
if let Some(loaded_sysroot) = loaded_sysroot {
249248
sysroot.set_workspace(loaded_sysroot);
250249
}
251250
assert!(
252-
matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace(_)),
251+
matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace { .. }),
253252
"got {}",
254253
sysroot.workspace()
255254
);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ impl ProjectWorkspace {
383383
toolchain.clone(),
384384
)),
385385
config.no_deps,
386-
workspace_dir,
387386
&target_dir,
388387
progress,
389388
)
@@ -487,7 +486,6 @@ impl ProjectWorkspace {
487486
sysroot.load_workspace(
488487
&RustSourceWorkspaceConfig::Json(*sysroot_project),
489488
config.no_deps,
490-
project_root,
491489
&target_dir,
492490
progress,
493491
)
@@ -499,7 +497,6 @@ impl ProjectWorkspace {
499497
toolchain.clone(),
500498
)),
501499
config.no_deps,
502-
project_root,
503500
&target_dir,
504501
progress,
505502
)
@@ -561,7 +558,6 @@ impl ProjectWorkspace {
561558
toolchain.clone(),
562559
)),
563560
config.no_deps,
564-
dir,
565561
&target_dir,
566562
&|_| (),
567563
);
@@ -747,7 +743,7 @@ impl ProjectWorkspace {
747743
pub fn to_roots(&self) -> Vec<PackageRoot> {
748744
let mk_sysroot = || {
749745
let mut r = match self.sysroot.workspace() {
750-
RustLibSrcWorkspace::Workspace(ws) => ws
746+
RustLibSrcWorkspace::Workspace { ws, .. } => ws
751747
.packages()
752748
.filter_map(|pkg| {
753749
if ws[pkg].is_local {
@@ -1735,7 +1731,7 @@ fn sysroot_to_crate_graph(
17351731
) -> (SysrootPublicDeps, Option<CrateBuilderId>) {
17361732
let _p = tracing::info_span!("sysroot_to_crate_graph").entered();
17371733
match sysroot.workspace() {
1738-
RustLibSrcWorkspace::Workspace(cargo) => {
1734+
RustLibSrcWorkspace::Workspace { ws: cargo, .. } => {
17391735
let (sysroot_cg, sysroot_pm) = cargo_to_crate_graph(
17401736
load,
17411737
None,

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/rustc_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ impl Tester {
7878
let loaded_sysroot = sysroot.load_workspace(
7979
&RustSourceWorkspaceConfig::default_cargo(),
8080
false,
81-
&path,
8281
&Utf8PathBuf::default(),
8382
&|_| (),
8483
);

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)