From 5f273d17ff3b3a5a2663f4660b4fad6eac2159dd Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 16 Dec 2024 23:23:36 -0500 Subject: [PATCH] fix(build-std): behavior revert of 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 | 23 +++++++++++++++++++---- src/cargo/ops/cargo_compile/mod.rs | 11 ++++++++--- src/cargo/ops/cargo_fetch.rs | 10 ++++++++-- tests/build-std/main.rs | 5 ----- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index c135a5d6d73..18253b5d465 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,21 @@ 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 anything looks like needing std, resolve with it. + // If not, we assume only `core` maye be needed, as `core the most fundamental crate. + // + // This may need a UI overhaul if `build-std` wants to fully support multi-targets. + let maybe_std = kinds + .iter() + .any(|kind| target_data.info(*kind).maybe_support_std()); + let mut crates = std_crates(crates, if maybe_std { "std" } else { "core" }, &[]); + // `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 c89c064c780..04520d64340 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 b1e596f6485..4ff89491d86 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 800495da340..976fc7e141b 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -438,10 +438,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(); }