1 Teaches Cargo to source all std dependencies from vendored sources in the rust-src
2 component, making -Zbuild-std compatible with vendored builds.
4 This was originally landed in https://github.com/rust-lang/cargo/pull/8834
5 but was backed out for causing breakage in other situations. It works fine
6 for Firefox's usecase, though.
8 Most of these changes just add/edit tests for the functionality. Only the
9 change to src/cargo/core/compiler/standard_lib.rs is important.
11 diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs
12 index 0b14df805..7bc5151a4 100644
13 --- a/src/cargo/core/compiler/standard_lib.rs
14 +++ b/src/cargo/core/compiler/standard_lib.rs
15 @@ -10,6 +10,7 @@ use crate::ops::{self, Packages};
16 use crate::util::errors::CargoResult;
17 use std::collections::{HashMap, HashSet};
20 use std::path::PathBuf;
22 /// Parse the `-Zbuild-std` flag.
23 @@ -38,28 +39,46 @@ pub fn resolve_std<'cfg>(
25 ) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
26 let src_path = detect_sysroot_src_path(target_data)?;
28 - "rustc-std-workspace-core",
29 - "rustc-std-workspace-alloc",
30 - "rustc-std-workspace-std",
32 - let patches = to_patch
35 - let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
36 - let dep = Dependency::parse(name, None, source_path)?;
38 + // Special std packages should be pulled from `library/` and should be
39 + // prefixed with `rustc-std-workspace-` in certain places.
40 + let libs_prefix = "library/";
41 + let special_std_prefix = "rustc-std-workspace-";
42 + let libs_path = src_path.join(libs_prefix);
44 + // Crates in rust-src to build. libtest is in some sense the "root" package
45 + // of std, as nothing else depends on it, so it must be explicitly added.
46 + let mut members = vec![format!("{}test", libs_prefix)];
48 + // If rust-src contains a "vendor" directory, then patch in all the crates it contains.
49 + let vendor_path = src_path.join("vendor");
50 + let vendor_dir = fs::read_dir(vendor_path)?;
51 + let patches = vendor_dir
58 + .map_err(|_| anyhow::anyhow!("package name wasn't utf8"))?;
60 + // Remap the rustc-std-workspace crates to the actual rust-src libraries
61 + let path = if let Some(real_name) = name.strip_prefix(special_std_prefix) {
62 + // Record this crate as something to build in the workspace
63 + members.push(format!("{}{}", libs_prefix, real_name));
64 + libs_path.join(&name)
68 + let source_path = SourceId::for_path(&path)?;
69 + let dep = Dependency::parse(&name, None, source_path)?;
72 .collect::<CargoResult<Vec<_>>>()?;
74 let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
75 let mut patch = HashMap::new();
76 patch.insert(crates_io_url, patches);
78 - String::from("library/std"),
79 - String::from("library/core"),
80 - String::from("library/alloc"),
81 - String::from("library/test"),
83 let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
86 diff --git a/tests/testsuite/mock-std/library/test/Cargo.toml b/tests/testsuite/mock-std/library/test/Cargo.toml
87 index 299db7bfd..eb81bbf5e 100644
88 --- a/tests/testsuite/mock-std/library/test/Cargo.toml
89 +++ b/tests/testsuite/mock-std/library/test/Cargo.toml
90 @@ -10,6 +10,7 @@ std = { path = "../std" }
91 panic_unwind = { path = "../panic_unwind" }
92 compiler_builtins = { path = "../compiler_builtins" }
93 registry-dep-using-std = { version = "*", features = ['mockbuild'] }
94 +registry-dep-only-used-by-test = { version = "*" }
98 diff --git a/tests/testsuite/mock-std/library/test/src/lib.rs b/tests/testsuite/mock-std/library/test/src/lib.rs
99 index a112855f5..224b89bb2 100644
100 --- a/tests/testsuite/mock-std/library/test/src/lib.rs
101 +++ b/tests/testsuite/mock-std/library/test/src/lib.rs
102 @@ -7,4 +7,5 @@ extern crate test;
105 pub fn custom_api() {
106 + registry_dep_only_used_by_test::wow_testing_is_so_easy();
108 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
110 index 000000000..31ba65a98
112 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
115 +name = "registry-dep-only-used-by-test"
117 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
123 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
125 index 000000000..a68d2aeef
127 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
129 +pub fn wow_testing_is_so_easy() {
131 \ No newline at end of file
132 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
134 index 000000000..f7e4ab232
136 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
139 +name = "registry-dep-using-alloc"
141 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
145 +rustc-std-workspace-alloc = { version = "*", optional = true }
146 +rustc-std-workspace-core = { version = "*", optional = true }
149 +mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"]
150 \ No newline at end of file
151 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
153 index 000000000..b9ab30339
155 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
157 +#[cfg(feature = "mockbuild")]
158 +pub fn custom_api() {
161 +#[cfg(not(feature = "mockbuild"))]
162 +pub fn non_sysroot_api() {
163 + core::custom_api();
164 + alloc::custom_api();
166 \ No newline at end of file
167 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
169 index 000000000..befb83a63
171 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
174 +name = "registry-dep-using-core"
176 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
180 +rustc-std-workspace-core = { version = "*", optional = true }
183 +mockbuild = ["rustc-std-workspace-core"]
184 \ No newline at end of file
185 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
187 index 000000000..f9dbac0f4
189 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
191 +#[cfg(feature = "mockbuild")]
192 +pub fn custom_api() {
195 +#[cfg(not(feature = "mockbuild"))]
196 +pub fn non_sysroot_api() {
197 + core::custom_api();
199 \ No newline at end of file
200 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
202 index 000000000..71ef0a42f
204 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
207 +name = "registry-dep-using-std"
209 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
213 +rustc-std-workspace-std = { version = "*", optional = true }
216 +mockbuild = ["rustc-std-workspace-std"]
217 \ No newline at end of file
218 diff --git a/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
220 index 000000000..f3af39178
222 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
224 +#[cfg(feature = "mockbuild")]
225 +pub fn custom_api() {
228 +#[cfg(not(feature = "mockbuild"))]
229 +pub fn non_sysroot_api() {
232 \ No newline at end of file
233 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
235 index 000000000..4465a08a8
237 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
239 +this file shouldn't be read
240 \ No newline at end of file
241 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
243 index 000000000..4465a08a8
245 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
247 +this file shouldn't be read
248 \ No newline at end of file
249 diff --git a/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
251 index 000000000..4465a08a8
253 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
255 +this file shouldn't be read
256 \ No newline at end of file
257 diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs
258 index b0d42b38d..3bbc49db7 100644
259 --- a/tests/testsuite/standard_lib.rs
260 +++ b/tests/testsuite/standard_lib.rs
261 @@ -27,71 +27,18 @@ fn setup() -> Option<Setup> {
265 - // Our mock sysroot requires a few packages from crates.io, so make sure
266 - // they're "published" to crates.io. Also edit their code a bit to make sure
267 - // that they have access to our custom crates with custom apis.
268 + // Register a version of one of the std dependencies that doesn't compile.
269 + // This ensures that the mock-std's vendor is actually being used.
270 Package::new("registry-dep-using-core", "1.0.0")
276 - #[cfg(feature = \"mockbuild\")]
277 - pub fn custom_api() {
280 - #[cfg(not(feature = \"mockbuild\"))]
281 - pub fn non_sysroot_api() {
282 - core::custom_api();
284 + don't compile me bro!!
287 .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
288 .feature("mockbuild", &["rustc-std-workspace-core"])
290 - Package::new("registry-dep-using-alloc", "1.0.0")
296 - extern crate alloc;
298 - #[cfg(feature = \"mockbuild\")]
299 - pub fn custom_api() {
302 - #[cfg(not(feature = \"mockbuild\"))]
303 - pub fn non_sysroot_api() {
304 - core::custom_api();
305 - alloc::custom_api();
309 - .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
310 - .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
313 - &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
316 - Package::new("registry-dep-using-std", "1.0.0")
320 - #[cfg(feature = \"mockbuild\")]
321 - pub fn custom_api() {
324 - #[cfg(not(feature = \"mockbuild\"))]
325 - pub fn non_sysroot_api() {
330 - .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
331 - .feature("mockbuild", &["rustc-std-workspace-std"])
334 let p = ProjectBuilder::new(paths::root().join("rustc-wrapper"))
336 @@ -327,6 +274,81 @@ fn depend_same_as_std() {
340 + // Our mock sysroot requires a few packages from crates.io, so make sure
341 + // they're "published" to crates.io. Also edit their code a bit to make sure
342 + // that they have access to our custom crates with custom apis.
343 + Package::new("registry-dep-using-core", "1.0.0")
349 + #[cfg(feature = \"mockbuild\")]
350 + pub fn custom_api() {
353 + #[cfg(not(feature = \"mockbuild\"))]
354 + pub fn non_sysroot_api() {
355 + core::custom_api();
359 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
360 + .feature("mockbuild", &["rustc-std-workspace-core"])
362 + Package::new("registry-dep-using-alloc", "1.0.0")
368 + extern crate alloc;
370 + #[cfg(feature = \"mockbuild\")]
371 + pub fn custom_api() {
374 + #[cfg(not(feature = \"mockbuild\"))]
375 + pub fn non_sysroot_api() {
376 + core::custom_api();
377 + alloc::custom_api();
381 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
382 + .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
385 + &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
388 + Package::new("registry-dep-using-std", "1.0.0")
392 + #[cfg(feature = \"mockbuild\")]
393 + pub fn custom_api() {
396 + #[cfg(not(feature = \"mockbuild\"))]
397 + pub fn non_sysroot_api() {
402 + .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
403 + .feature("mockbuild", &["rustc-std-workspace-std"])
405 + Package::new("registry-dep-only-used-by-test", "1.0.0")
409 + pub fn wow_testing_is_so_easy() {