From 9af393032143fc113403846836d15a0dc75dc8f2 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 5 Aug 2024 23:20:39 -0400 Subject: [PATCH 1/3] test(build-std): expand assertion to ensure no index update --- tests/build-std/main.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 1075643219c..aec951a9578 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -119,8 +119,31 @@ fn basic() { "#]]) .run(); - p.cargo("run").build_std().target_host().run(); - p.cargo("test").build_std().target_host().run(); + p.cargo("run") + .build_std() + .target_host() + .with_stderr_data(str![[r#" +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] `target/[HOST_TARGET]/debug/foo` + +"#]]) + .run(); + p.cargo("test") + .build_std() + .target_host() + .with_stderr_data(str![[r#" +[COMPILING] rustc-std-workspace-std [..] +... +[COMPILING] test v0.0.0 ([..]) +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] unittests src/lib.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH]) +[RUNNING] unittests src/main.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH]) +[RUNNING] tests/smoke.rs (target/[HOST_TARGET]/debug/deps/smoke-[HASH]) +[DOCTEST] foo + +"#]]) + .run(); // Check for hack that removes dylibs. let deps_dir = Path::new("target") From da8138e3c426f3f4edf3463913ad876d1481da36 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 5 Aug 2024 23:22:35 -0400 Subject: [PATCH 2/3] fix(build-std): remove hack on creating virtual std workspace Starting from rust-lang/rust#128534 (nightly-2024-08-05), stnadard library has its own Cargo workspace. Hence `-Zbuild-std` no longer need to fake a virtual workspace. This also adjusts Cargo.toml in `mock-std` to align with std's Cargo.toml --- src/cargo/core/compiler/standard_lib.rs | 56 ++------------------- tests/testsuite/mock-std/Cargo.toml | 8 --- tests/testsuite/mock-std/library/Cargo.toml | 11 ++++ 3 files changed, 14 insertions(+), 61 deletions(-) delete mode 100644 tests/testsuite/mock-std/Cargo.toml create mode 100644 tests/testsuite/mock-std/library/Cargo.toml diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 8b24cf7cdcb..92b3474d3f0 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -6,13 +6,12 @@ use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit}; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures}; use crate::core::resolver::HasDevUnits; -use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace}; +use crate::core::{PackageId, PackageSet, Resolve, Workspace}; use crate::ops::{self, Packages}; use crate::util::errors::CargoResult; use crate::GlobalContext; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use std::rc::Rc; use super::BuildConfig; @@ -74,60 +73,11 @@ pub fn resolve_std<'gctx>( } let src_path = detect_sysroot_src_path(target_data)?; - let to_patch = [ - "rustc-std-workspace-core", - "rustc-std-workspace-alloc", - "rustc-std-workspace-std", - ]; - let patches = to_patch - .iter() - .map(|&name| { - let source_path = SourceId::for_path(&src_path.join("library").join(name))?; - let dep = Dependency::parse(name, None, source_path)?; - Ok(dep) - }) - .collect::>>()?; - let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap(); - let patch = HashMap::from([(crates_io_url, patches)]); - let members = vec![ - String::from("library/std"), - String::from("library/core"), - String::from("library/alloc"), - String::from("library/sysroot"), - ]; - let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new( - &src_path, - &Some(members), - /*default_members*/ &None, - /*exclude*/ &None, - /*inheritable*/ &None, - /*custom_metadata*/ &None, - )); - let virtual_manifest = crate::core::VirtualManifest::new( - Rc::default(), - Rc::new(toml_edit::ImDocument::parse("".to_owned()).expect("empty is valid TOML")), - Rc::default(), - Rc::default(), - /*replace*/ Vec::new(), - patch, - ws_config, - crate::core::Features::default(), - None, - ); - + let std_ws_manifest_path = src_path.join("library").join("Cargo.toml"); let gctx = ws.gctx(); - // This is a delicate hack. In order for features to resolve correctly, - // the resolver needs to run a specific "current" member of the workspace. - // Thus, in order to set the features for `std`, we need to set `sysroot` - // to be the "current" member. `sysroot` is the root, and all other - // standard library crates are dependencies from there. Since none of the - // other crates need to alter their features, this should be fine, for - // now. Perhaps in the future features will be decoupled from the resolver - // and it will be easier to control feature selection. - let current_manifest = src_path.join("library/sysroot/Cargo.toml"); // TODO: Consider doing something to enforce --locked? Or to prevent the // lock file from being written, such as setting ephemeral. - let mut std_ws = Workspace::new_virtual(src_path, current_manifest, virtual_manifest, gctx)?; + let mut std_ws = Workspace::new(&std_ws_manifest_path, gctx)?; // Don't require optional dependencies in this workspace, aka std's own // `[dev-dependencies]`. No need for us to generate a `Resolve` which has // those included because we'll never use them anyway. diff --git a/tests/testsuite/mock-std/Cargo.toml b/tests/testsuite/mock-std/Cargo.toml deleted file mode 100644 index a69aa4b88fa..00000000000 --- a/tests/testsuite/mock-std/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[workspace] -members = [ - "library/alloc", - "library/core", - "library/proc_macro", - "library/std", - "library/test", -] diff --git a/tests/testsuite/mock-std/library/Cargo.toml b/tests/testsuite/mock-std/library/Cargo.toml new file mode 100644 index 00000000000..f44d7bef23a --- /dev/null +++ b/tests/testsuite/mock-std/library/Cargo.toml @@ -0,0 +1,11 @@ +[workspace] +resolver = "1" +members = [ + "std", + "sysroot", +] + +[patch.crates-io] +rustc-std-workspace-core = { path = 'rustc-std-workspace-core' } +rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' } +rustc-std-workspace-std = { path = 'rustc-std-workspace-std' } From 7902bf8cb02d47d7ba6386ca6191dca2c2a96821 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 5 Aug 2024 23:37:58 -0400 Subject: [PATCH 3/3] refactor: remove unused `Workspace::new_virtual` It was designed for `-Zbuild-std` --- src/cargo/core/workspace.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 0e538c698e1..3a2588e412c 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -244,25 +244,6 @@ impl<'gctx> Workspace<'gctx> { } } - pub fn new_virtual( - root_path: PathBuf, - current_manifest: PathBuf, - manifest: VirtualManifest, - gctx: &'gctx GlobalContext, - ) -> CargoResult> { - let mut ws = Workspace::new_default(current_manifest, gctx); - ws.root_manifest = Some(root_path.join("Cargo.toml")); - ws.target_dir = gctx.target_dir()?; - ws.packages - .packages - .insert(root_path, MaybePackage::Virtual(manifest)); - ws.find_members()?; - ws.set_resolve_behavior()?; - // TODO: validation does not work because it walks up the directory - // tree looking for the root which is a fake file that doesn't exist. - Ok(ws) - } - /// Creates a "temporary workspace" from one package which only contains /// that package. ///