Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/project-model/src/workspace.rs')
-rw-r--r--crates/project-model/src/workspace.rs126
1 files changed, 78 insertions, 48 deletions
diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs
index aa2e15930c..f01daa82b6 100644
--- a/crates/project-model/src/workspace.rs
+++ b/crates/project-model/src/workspace.rs
@@ -2,6 +2,7 @@
//! metadata` or `rust-project.json`) into representation stored in the salsa
//! database -- `CrateGraph`.
+use std::thread::Builder;
use std::{collections::VecDeque, fmt, fs, iter, ops::Deref, sync, thread};
use anyhow::Context;
@@ -12,7 +13,7 @@ use base_db::{
};
use cfg::{CfgAtom, CfgDiff, CfgOptions};
use intern::{Symbol, sym};
-use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
+use paths::{AbsPath, AbsPathBuf, Utf8Path, Utf8PathBuf};
use rustc_hash::{FxHashMap, FxHashSet};
use semver::Version;
use span::{Edition, FileId};
@@ -301,31 +302,39 @@ impl ProjectWorkspace {
// We can speed up loading a bit by spawning all of these processes in parallel (especially
// on systems were process spawning is delayed)
let join = thread::scope(|s| {
- let rustc_cfg = s.spawn(|| {
- rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), extra_env)
- });
- let target_data = s.spawn(|| {
- target_data::get(
- toolchain_config,
- targets.first().map(Deref::deref),
- extra_env,
- ).inspect_err(|e| {
- tracing::error!(%e, "failed fetching data layout for {cargo_toml:?} workspace")
+ let rustc_cfg = Builder::new()
+ .name("ProjectWorkspace::rustc_cfg".to_owned())
+ .spawn_scoped(s, || {
+ rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), extra_env)
})
- });
-
- let rustc_dir = s.spawn(|| {
- let rustc_dir = match rustc_source {
- Some(RustLibSource::Path(path)) => ManifestPath::try_from(path.clone())
- .map_err(|p| Some(format!("rustc source path is not absolute: {p}"))),
- Some(RustLibSource::Discover) => {
- sysroot.discover_rustc_src().ok_or_else(|| {
- Some("Failed to discover rustc source for sysroot.".to_owned())
+ .expect("failed to spawn thread");
+ let target_data = Builder::new()
+ .name("ProjectWorkspace::target_data".to_owned())
+ .spawn_scoped(s, || {
+ target_data::get(toolchain_config, targets.first().map(Deref::deref), extra_env)
+ .inspect_err(|e| {
+ tracing::error!(%e,
+ "failed fetching data layout for \
+ {cargo_toml:?} workspace"
+ )
})
- }
- None => Err(None),
- };
- rustc_dir.and_then(|rustc_dir| {
+ })
+ .expect("failed to spawn thread");
+
+ let rustc_dir = Builder::new()
+ .name("ProjectWorkspace::rustc_dir".to_owned())
+ .spawn_scoped(s, || {
+ let rustc_dir = match rustc_source {
+ Some(RustLibSource::Path(path)) => ManifestPath::try_from(path.clone())
+ .map_err(|p| Some(format!("rustc source path is not absolute: {p}"))),
+ Some(RustLibSource::Discover) => {
+ sysroot.discover_rustc_src().ok_or_else(|| {
+ Some("Failed to discover rustc source for sysroot.".to_owned())
+ })
+ }
+ None => Err(None),
+ };
+ rustc_dir.and_then(|rustc_dir| {
info!(workspace = %cargo_toml, rustc_dir = %rustc_dir, "Using rustc source");
match FetchMetadata::new(
&rustc_dir,
@@ -359,31 +368,44 @@ impl ProjectWorkspace {
Err(e) => {
tracing::error!(
%e,
- "Failed to read Cargo metadata from rustc source at {rustc_dir}",
+ "Failed to read Cargo metadata from rustc source \
+ at {rustc_dir}",
);
Err(Some(format!(
- "Failed to read Cargo metadata from rustc source at {rustc_dir}: {e}"
+ "Failed to read Cargo metadata from rustc source \
+ at {rustc_dir}: {e}"
)))
}
}
})
- });
-
- let cargo_metadata = s.spawn(|| fetch_metadata.exec(false, progress));
- let loaded_sysroot = s.spawn(|| {
- sysroot.load_workspace(
- &RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
- config,
- workspace_dir,
- &targets,
- toolchain.clone(),
- )),
- config.no_deps,
- progress,
- )
- });
- let cargo_env =
- s.spawn(move || cargo_config_env(cargo_toml, &config_file, &config.extra_env));
+ })
+ .expect("failed to spawn thread");
+
+ let cargo_metadata = Builder::new()
+ .name("ProjectWorkspace::cargo_metadata".to_owned())
+ .spawn_scoped(s, || fetch_metadata.exec(false, progress))
+ .expect("failed to spawn thread");
+ let loaded_sysroot = Builder::new()
+ .name("ProjectWorkspace::loaded_sysroot".to_owned())
+ .spawn_scoped(s, || {
+ sysroot.load_workspace(
+ &RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
+ config,
+ workspace_dir,
+ &targets,
+ toolchain.clone(),
+ )),
+ config.no_deps,
+ progress,
+ )
+ })
+ .expect("failed to spawn thread");
+ let cargo_env = Builder::new()
+ .name("ProjectWorkspace::cargo_env".to_owned())
+ .spawn_scoped(s, move || {
+ cargo_config_env(cargo_toml, &config_file, &config.extra_env)
+ })
+ .expect("failed to spawn thread");
thread::Result::Ok((
rustc_cfg.join()?,
target_data.join()?,
@@ -1194,6 +1216,7 @@ fn cargo_to_crate_graph(
load,
crate_ws_data.clone(),
);
+ let cargo_path = sysroot.tool_path(Tool::Cargo, cargo.workspace_root(), cargo.env());
let cfg_options = CfgOptions::from_iter(rustc_cfg);
@@ -1268,6 +1291,7 @@ fn cargo_to_crate_graph(
} else {
Arc::new(pkg_data.manifest.parent().to_path_buf())
},
+ &cargo_path,
);
if let TargetKind::Lib { .. } = kind {
lib_tgt = Some((crate_id, name.clone()));
@@ -1375,6 +1399,7 @@ fn cargo_to_crate_graph(
},
// FIXME: This looks incorrect but I don't think this causes problems.
crate_ws_data,
+ &cargo_path,
);
}
}
@@ -1453,6 +1478,7 @@ fn handle_rustc_crates(
override_cfg: &CfgOverrides,
build_scripts: &WorkspaceBuildScripts,
crate_ws_data: Arc<CrateWorkspaceData>,
+ cargo_path: &Utf8Path,
) {
let mut rustc_pkg_crates = FxHashMap::default();
// The root package of the rustc-dev component is rustc_driver, so we match that
@@ -1503,6 +1529,7 @@ fn handle_rustc_crates(
} else {
Arc::new(pkg_data.manifest.parent().to_path_buf())
},
+ cargo_path,
);
pkg_to_lib_crate.insert(pkg, crate_id);
// Add dependencies on core / std / alloc for this crate
@@ -1560,11 +1587,12 @@ fn add_target_crate_root(
build_data: Option<(&BuildScriptOutput, bool)>,
cfg_options: CfgOptions,
file_id: FileId,
- cargo_name: &str,
+ cargo_crate_name: &str,
kind: TargetKind,
origin: CrateOrigin,
crate_ws_data: Arc<CrateWorkspaceData>,
proc_macro_cwd: Arc<AbsPathBuf>,
+ cargo_path: &Utf8Path,
) -> CrateBuilderId {
let edition = pkg.edition;
let potential_cfg_options = if pkg.features.is_empty() {
@@ -1591,8 +1619,8 @@ fn add_target_crate_root(
let mut env = cargo.env().clone();
inject_cargo_package_env(&mut env, pkg);
- inject_cargo_env(&mut env);
- inject_rustc_tool_env(&mut env, cargo_name, kind);
+ inject_cargo_env(&mut env, cargo_path);
+ inject_rustc_tool_env(&mut env, cargo_crate_name, kind);
if let Some(envs) = build_data.map(|(it, _)| &it.envs) {
env.extend_from_other(envs);
@@ -1600,7 +1628,7 @@ fn add_target_crate_root(
let crate_id = crate_graph.add_crate_root(
file_id,
edition,
- Some(CrateDisplayName::from_canonical_name(cargo_name)),
+ Some(CrateDisplayName::from_canonical_name(cargo_crate_name)),
Some(pkg.version.to_string()),
cfg_options,
potential_cfg_options,
@@ -1614,7 +1642,9 @@ fn add_target_crate_root(
let proc_macro = match build_data {
Some((BuildScriptOutput { proc_macro_dylib_path, .. }, has_errors)) => {
match proc_macro_dylib_path {
- ProcMacroDylibPath::Path(path) => Ok((cargo_name.to_owned(), path.clone())),
+ ProcMacroDylibPath::Path(path) => {
+ Ok((cargo_crate_name.to_owned(), path.clone()))
+ }
ProcMacroDylibPath::NotBuilt => Err(ProcMacroLoadingError::NotYetBuilt),
ProcMacroDylibPath::NotProcMacro | ProcMacroDylibPath::DylibNotFound
if has_errors =>