Skip to content

Commit cd4100d

Browse files
authored
Merge pull request #20092 from Veykril/push-kollxvpyknqs
Drop rustc workspace loading error, if we don't needs its sources
2 parents ca062eb + 7d6f77f commit cd4100d

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct CargoWorkspace {
4848
is_sysroot: bool,
4949
/// Environment variables set in the `.cargo/config` file.
5050
config_env: Env,
51+
requires_rustc_private: bool,
5152
}
5253

5354
impl ops::Index<Package> for CargoWorkspace {
@@ -513,6 +514,7 @@ impl CargoWorkspace {
513514
let workspace_root = AbsPathBuf::assert(meta.workspace_root);
514515
let target_directory = AbsPathBuf::assert(meta.target_directory);
515516
let mut is_virtual_workspace = true;
517+
let mut requires_rustc_private = false;
516518

517519
meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
518520
for meta_pkg in meta.packages {
@@ -577,6 +579,7 @@ impl CargoWorkspace {
577579
metadata: meta.rust_analyzer.unwrap_or_default(),
578580
});
579581
let pkg_data = &mut packages[pkg];
582+
requires_rustc_private |= pkg_data.metadata.rustc_private;
580583
pkg_by_id.insert(id, pkg);
581584
for meta_tgt in meta_targets {
582585
let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
@@ -626,6 +629,7 @@ impl CargoWorkspace {
626629
target_directory,
627630
manifest_path: ws_manifest_path,
628631
is_virtual_workspace,
632+
requires_rustc_private,
629633
is_sysroot,
630634
config_env: cargo_config_env,
631635
}
@@ -724,4 +728,8 @@ impl CargoWorkspace {
724728
pub fn is_sysroot(&self) -> bool {
725729
self.is_sysroot
726730
}
731+
732+
pub fn requires_rustc_private(&self) -> bool {
733+
self.requires_rustc_private
734+
}
727735
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,17 @@ impl ProjectWorkspace {
408408
))
409409
});
410410

411-
let (rustc_cfg, data_layout, rustc, loaded_sysroot, cargo_metadata, cargo_config_extra_env) =
412-
match join {
413-
Ok(it) => it,
414-
Err(e) => std::panic::resume_unwind(e),
415-
};
411+
let (
412+
rustc_cfg,
413+
data_layout,
414+
mut rustc,
415+
loaded_sysroot,
416+
cargo_metadata,
417+
cargo_config_extra_env,
418+
) = match join {
419+
Ok(it) => it,
420+
Err(e) => std::panic::resume_unwind(e),
421+
};
416422

417423
let (meta, error) = cargo_metadata.with_context(|| {
418424
format!(
@@ -425,6 +431,14 @@ impl ProjectWorkspace {
425431
sysroot.set_workspace(loaded_sysroot);
426432
}
427433

434+
if !cargo.requires_rustc_private() {
435+
if let Err(e) = &mut rustc {
436+
// We don't need the rustc sources here,
437+
// so just discard the error.
438+
_ = e.take();
439+
}
440+
}
441+
428442
Ok(ProjectWorkspace {
429443
kind: ProjectWorkspaceKind::Cargo {
430444
cargo,
@@ -1208,14 +1222,10 @@ fn cargo_to_crate_graph(
12081222
// Mapping of a package to its library target
12091223
let mut pkg_to_lib_crate = FxHashMap::default();
12101224
let mut pkg_crates = FxHashMap::default();
1211-
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
1212-
let mut has_private = false;
12131225
let workspace_proc_macro_cwd = Arc::new(cargo.workspace_root().to_path_buf());
12141226

12151227
// Next, create crates for each package, target pair
12161228
for pkg in cargo.packages() {
1217-
has_private |= cargo[pkg].metadata.rustc_private;
1218-
12191229
let cfg_options = {
12201230
let mut cfg_options = cfg_options.clone();
12211231

@@ -1360,7 +1370,7 @@ fn cargo_to_crate_graph(
13601370
add_dep(crate_graph, from, name, to);
13611371
}
13621372

1363-
if has_private {
1373+
if cargo.requires_rustc_private() {
13641374
// If the user provided a path to rustc sources, we add all the rustc_private crates
13651375
// and create dependencies on them for the crates which opt-in to that
13661376
if let Some((rustc_workspace, rustc_build_scripts)) = rustc {

0 commit comments

Comments
 (0)