From 9d10ede6e3df2e9f522cded1d31ca89d10734850 Mon Sep 17 00:00:00 2001
From: Weihang Lo
Date: Mon, 16 Dec 2024 23:23:36 -0500
Subject: [PATCH] fix(build-std): revert 125e873dffc4b68b
This is kinda a revert of 125e873dffc4b68b263c5decd88750ec10fd441e
in terms of the behavior.
After this, now `std_resolve` is always resolved by the same set of
packages that Cargo will use to generate the unit graph, (technically
the same set of crates + `sysroot`), by sharing the same set of primary
packages via `std_crates` functions.
---
src/cargo/core/compiler/standard_lib.rs | 21 +++++++++++++++++----
src/cargo/ops/cargo_compile/mod.rs | 11 ++++++++---
src/cargo/ops/cargo_fetch.rs | 10 ++++++++--
tests/build-std/main.rs | 5 -----
4 files changed, 33 insertions(+), 14 deletions(-)
diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs
index 1d55a739f949..e2f3aa45537e 100644
--- a/src/cargo/core/compiler/standard_lib.rs
+++ b/src/cargo/core/compiler/standard_lib.rs
@@ -44,10 +44,14 @@ fn std_crates<'a>(crates: &'a [String], default: &'static str, units: &[Unit]) -
}
/// Resolve the standard library dependencies.
+///
+/// * `crates` is the arg value from `-Zbuild-std`.
pub fn resolve_std<'gctx>(
ws: &Workspace<'gctx>,
target_data: &mut RustcTargetData<'gctx>,
build_config: &BuildConfig,
+ crates: &[String],
+ kinds: &[CompileKind],
) -> CargoResult<(PackageSet<'gctx>, Resolve, ResolvedFeatures)> {
if build_config.build_plan {
ws.gctx()
@@ -65,10 +69,19 @@ pub fn resolve_std<'gctx>(
// `[dev-dependencies]`. No need for us to generate a `Resolve` which has
// those included because we'll never use them anyway.
std_ws.set_require_optional_deps(false);
- // `sysroot` + the default feature set below should give us a good default
- // Resolve, which includes `libtest` as well.
- let specs = Packages::Packages(vec!["sysroot".into()]);
- let specs = specs.to_package_id_specs(&std_ws)?;
+ let specs = {
+ // If there is any need std, resolve with it.
+ // This may need a UI overhaul if `build-std` wants to fully support multi-targets.
+ let core_only = kinds
+ .iter()
+ .all(|kind| target_data.info(*kind).support_core_only());
+ let mut crates = std_crates(crates, if core_only { "core" } else { "std" }, &[]);
+ // `sysroot` is not in the default set because it is optional, but it needs
+ // to be part of the resolve in case we do need it or `libtest`.
+ crates.insert("sysroot");
+ let specs = Packages::Packages(crates.into_iter().map(Into::into).collect());
+ specs.to_package_id_specs(&std_ws)?
+ };
let features = match &gctx.cli_unstable().build_std_features {
Some(list) => list.clone(),
None => vec![
diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs
index c89c064c7800..04520d643407 100644
--- a/src/cargo/ops/cargo_compile/mod.rs
+++ b/src/cargo/ops/cargo_compile/mod.rs
@@ -289,9 +289,14 @@ pub fn create_bcx<'a, 'gctx>(
resolved_features,
} = resolve;
- let std_resolve_features = if gctx.cli_unstable().build_std.is_some() {
- let (std_package_set, std_resolve, std_features) =
- standard_lib::resolve_std(ws, &mut target_data, &build_config)?;
+ let std_resolve_features = if let Some(crates) = &gctx.cli_unstable().build_std {
+ let (std_package_set, std_resolve, std_features) = standard_lib::resolve_std(
+ ws,
+ &mut target_data,
+ &build_config,
+ crates,
+ &build_config.requested_kinds,
+ )?;
pkg_set.add_set(std_package_set);
Some((std_resolve, std_features))
} else {
diff --git a/src/cargo/ops/cargo_fetch.rs b/src/cargo/ops/cargo_fetch.rs
index b1e596f6485c..4ff89491d865 100644
--- a/src/cargo/ops/cargo_fetch.rs
+++ b/src/cargo/ops/cargo_fetch.rs
@@ -64,8 +64,14 @@ pub fn fetch<'a>(
}
// If -Zbuild-std was passed, download dependencies for the standard library.
- if gctx.cli_unstable().build_std.is_some() {
- let (std_package_set, _, _) = standard_lib::resolve_std(ws, &mut data, &build_config)?;
+ if let Some(crates) = &gctx.cli_unstable().build_std {
+ let (std_package_set, _, _) = standard_lib::resolve_std(
+ ws,
+ &mut data,
+ &build_config,
+ crates,
+ &build_config.requested_kinds,
+ )?;
packages.add_set(std_package_set);
}
diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs
index 8f48b436174f..0c5d1330cca4 100644
--- a/tests/build-std/main.rs
+++ b/tests/build-std/main.rs
@@ -422,10 +422,5 @@ fn test_panic_abort() {
.build_std_arg("std,panic_abort")
.env("RUSTFLAGS", "-C panic=abort")
.arg("-Zbuild-std-features=panic_immediate_abort")
- .with_status(101)
- .with_stderr_data(str![[r#"
-[ERROR] package ID specification `panic_unwind` did not match any packages
-
-"#]])
.run();
}