Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 59 additions & 19 deletions crates/project-model/src/build_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
CargoConfig, CargoFeatures, CargoWorkspace, InvocationStrategy, ManifestPath, Package, Sysroot,
TargetKind,
cargo_config_file::{LockfileCopy, LockfileUsage, make_lockfile_copy},
sysroot::RustLibSrcWorkspace,
utf8_stdout,
};

Expand Down Expand Up @@ -92,14 +93,15 @@ impl WorkspaceBuildScripts {
sysroot,
toolchain,
)?;
Self::run_per_ws(cmd, workspace, progress)
Self::run_per_ws(cmd, workspace, sysroot, progress)
}

/// Runs the build scripts by invoking the configured command *once*.
/// This populates the outputs for all passed in workspaces.
pub(crate) fn run_once(
config: &CargoConfig,
workspaces: &[&CargoWorkspace],
sysroots: &[&Sysroot],
progress: &dyn Fn(String),
working_directory: &AbsPathBuf,
) -> io::Result<Vec<WorkspaceBuildScripts>> {
Expand All @@ -118,7 +120,8 @@ impl WorkspaceBuildScripts {
// NB: Cargo.toml could have been modified between `cargo metadata` and
// `cargo check`. We shouldn't assume that package ids we see here are
// exactly those from `config`.
let mut by_id = FxHashMap::default();
// `None` entries mark sysroot packages: recognized but not stored in any workspace output.
let mut by_id: FxHashMap<Arc<PackageId>, Option<(Package, usize)>> = FxHashMap::default();
// some workspaces might depend on the same crates, so we need to duplicate the outputs
// to those collisions
let mut collisions = Vec::new();
Expand All @@ -132,28 +135,51 @@ impl WorkspaceBuildScripts {
if by_id.contains_key(&workspace[package].id) {
collisions.push((&workspace[package].id, idx, package));
} else {
by_id.insert(workspace[package].id.clone(), (package, idx));
by_id.insert(workspace[package].id.clone(), Some((package, idx)));
}
}
res
})
.collect();

// Insert sysroot package IDs so that messages for stdlib crates emitted when
// build-std is enabled are recognized rather than reported as unknown packages.
for sysroot in sysroots {
if let RustLibSrcWorkspace::Workspace { ws, .. } = sysroot.workspace() {
for package in ws.packages() {
by_id.entry(ws[package].id.clone()).or_insert(None);
}
}
}

let errors = Self::run_command(
cmd,
|package, cb| {
if let Some(&(package, workspace)) = by_id.get(package) {
cb(&workspaces[workspace][package].name, &mut res[workspace].outputs[package]);
} else {
tracing::error!("Received compiler message for unknown package: {}", package);
match by_id.get(package) {
Some(Some((package, workspace))) => {
cb(
&workspaces[*workspace][*package].name,
&mut res[*workspace].outputs[*package],
);
}
Some(None) => {
// Sysroot package (e.g. core when build-std is enabled);
// its outputs are handled separately.
Copy link
Copy Markdown
Member

@Veykril Veykril Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

I am pretty sure if we are running into build-std, we need to actually handle the packages here and then disable the special case handling in those cases. Otherwise we will end up trying to load in correct proc-macros for std I think. (in theory at least)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

I am pretty sure if we are running into build-std, we need to actually handle the packages here and then disable the special case handling in those cases. Otherwise we will end up trying to load in correct proc-macros for std I think. (in theory at least)

Got it, makes sense. Quick question before I rework this...should I detect that build-std is active by checking if we receive compiler messages for sysroot packages during the run, or should I check the cargo config/command args upfront?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pro-actively recognize build-std, by checking whether build-std is requested, https://github.com/rust-lang/rust-analyzer/blob/49094dbbeae543853ff4b89da27e0eab59101cd8/crates/project-model/src/cargo_config_file.rs

}
None => {
tracing::error!(
"Received compiler message for unknown package: {}",
package
);
}
}
},
progress,
)?;
res.iter_mut().for_each(|it| it.error.clone_from(&errors));
collisions.into_iter().for_each(|(id, workspace, package)| {
if let Some(&(p, w)) = by_id.get(id) {
res[workspace].outputs[package] = res[w].outputs[p].clone();
if let Some(Some((p, w))) = by_id.get(id) {
res[workspace].outputs[package] = res[*w].outputs[*p].clone();
}
});

Expand Down Expand Up @@ -281,30 +307,44 @@ impl WorkspaceBuildScripts {
fn run_per_ws(
cmd: Command,
workspace: &CargoWorkspace,
sysroot: &Sysroot,
progress: &dyn Fn(String),
) -> io::Result<WorkspaceBuildScripts> {
let mut res = WorkspaceBuildScripts::default();
let outputs = &mut res.outputs;
// NB: Cargo.toml could have been modified between `cargo metadata` and
// `cargo check`. We shouldn't assume that package ids we see here are
// exactly those from `config`.
let mut by_id: FxHashMap<Arc<PackageId>, Package> = FxHashMap::default();
// `None` entries mark sysroot packages: recognized but not stored in the workspace output.
let mut by_id: FxHashMap<Arc<PackageId>, Option<Package>> = FxHashMap::default();
for package in workspace.packages() {
outputs.insert(package, BuildScriptOutput::default());
by_id.insert(workspace[package].id.clone(), package);
by_id.insert(workspace[package].id.clone(), Some(package));
}
// Insert sysroot package IDs so that messages for stdlib crates emitted when
// build-std is enabled are recognized rather than reported as unknown packages.
if let RustLibSrcWorkspace::Workspace { ws, .. } = sysroot.workspace() {
for package in ws.packages() {
by_id.entry(ws[package].id.clone()).or_insert(None);
}
}

res.error = Self::run_command(
cmd,
|package, cb| {
if let Some(&package) = by_id.get(package) {
cb(&workspace[package].name, &mut outputs[package]);
} else {
never!(
"Received compiler message for unknown package: {}\n {}",
package,
by_id.keys().join(", ")
);
match by_id.get(package) {
Some(Some(package)) => cb(&workspace[*package].name, &mut outputs[*package]),
Some(None) => {
// Sysroot package (e.g. core when build-std is enabled);
// its outputs are handled separately, so discard here.
}
None => {
never!(
"Received compiler message for unknown package: {}\n {}",
package,
by_id.keys().join(", ")
);
}
}
},
progress,
Expand Down
7 changes: 4 additions & 3 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,17 @@ impl ProjectWorkspace {
return workspaces.iter().map(|it| it.run_build_scripts(config, progress)).collect();
}

let cargo_ws: Vec<_> = workspaces
let (cargo_ws, sysroots): (Vec<_>, Vec<_>) = workspaces
.iter()
.filter_map(|it| match &it.kind {
ProjectWorkspaceKind::Cargo { cargo, .. } => Some(cargo),
ProjectWorkspaceKind::Cargo { cargo, .. } => Some((cargo, &it.sysroot)),
_ => None,
})
.collect();
.unzip();
let outputs = &mut match WorkspaceBuildScripts::run_once(
config,
&cargo_ws,
&sysroots,
progress,
working_directory,
) {
Expand Down