Skip to content

Drop rustc workspace loading error, if we don't needs its sources #20092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/project-model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct CargoWorkspace {
is_sysroot: bool,
/// Environment variables set in the `.cargo/config` file.
config_env: Env,
requires_rustc_private: bool,
}

impl ops::Index<Package> for CargoWorkspace {
Expand Down Expand Up @@ -513,6 +514,7 @@ impl CargoWorkspace {
let workspace_root = AbsPathBuf::assert(meta.workspace_root);
let target_directory = AbsPathBuf::assert(meta.target_directory);
let mut is_virtual_workspace = true;
let mut requires_rustc_private = false;

meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
for meta_pkg in meta.packages {
Expand Down Expand Up @@ -577,6 +579,7 @@ impl CargoWorkspace {
metadata: meta.rust_analyzer.unwrap_or_default(),
});
let pkg_data = &mut packages[pkg];
requires_rustc_private |= pkg_data.metadata.rustc_private;
pkg_by_id.insert(id, pkg);
for meta_tgt in meta_targets {
let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
Expand Down Expand Up @@ -626,6 +629,7 @@ impl CargoWorkspace {
target_directory,
manifest_path: ws_manifest_path,
is_virtual_workspace,
requires_rustc_private,
is_sysroot,
config_env: cargo_config_env,
}
Expand Down Expand Up @@ -724,4 +728,8 @@ impl CargoWorkspace {
pub fn is_sysroot(&self) -> bool {
self.is_sysroot
}

pub fn requires_rustc_private(&self) -> bool {
self.requires_rustc_private
}
}
30 changes: 20 additions & 10 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,17 @@ impl ProjectWorkspace {
))
});

let (rustc_cfg, data_layout, rustc, loaded_sysroot, cargo_metadata, cargo_config_extra_env) =
match join {
Ok(it) => it,
Err(e) => std::panic::resume_unwind(e),
};
let (
rustc_cfg,
data_layout,
mut rustc,
loaded_sysroot,
cargo_metadata,
cargo_config_extra_env,
) = match join {
Ok(it) => it,
Err(e) => std::panic::resume_unwind(e),
};

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

if !cargo.requires_rustc_private() {
if let Err(e) = &mut rustc {
// We don't need the rustc sources here,
// so just discard the error.
_ = e.take();
}
}

Ok(ProjectWorkspace {
kind: ProjectWorkspaceKind::Cargo {
cargo,
Expand Down Expand Up @@ -1208,14 +1222,10 @@ fn cargo_to_crate_graph(
// Mapping of a package to its library target
let mut pkg_to_lib_crate = FxHashMap::default();
let mut pkg_crates = FxHashMap::default();
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
let mut has_private = false;
let workspace_proc_macro_cwd = Arc::new(cargo.workspace_root().to_path_buf());

// Next, create crates for each package, target pair
for pkg in cargo.packages() {
has_private |= cargo[pkg].metadata.rustc_private;

let cfg_options = {
let mut cfg_options = cfg_options.clone();

Expand Down Expand Up @@ -1360,7 +1370,7 @@ fn cargo_to_crate_graph(
add_dep(crate_graph, from, name, to);
}

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