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 e0baebd51..547b84147 100644
13 --- a/src/cargo/core/compiler/standard_lib.rs
14 +++ b/src/cargo/core/compiler/standard_lib.rs
15 @@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
17 use std::collections::{HashMap, HashSet};
20 use std::path::PathBuf;
22 use super::BuildConfig;
23 @@ -74,27 +75,45 @@ pub fn resolve_std<'cfg>(
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 patch = HashMap::from([(crates_io_url, patches)]);
77 - String::from("library/std"),
78 - String::from("library/core"),
79 - String::from("library/alloc"),
80 - String::from("library/test"),
82 let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
85 diff --git a/tests/testsuite/mock-std/library/test/Cargo.toml b/tests/testsuite/mock-std/library/test/Cargo.toml
86 index 299db7bfd..eb81bbf5e 100644
87 --- a/tests/testsuite/mock-std/library/test/Cargo.toml
88 +++ b/tests/testsuite/mock-std/library/test/Cargo.toml
89 @@ -10,6 +10,7 @@ std = { path = "../std" }
90 panic_unwind = { path = "../panic_unwind" }
91 compiler_builtins = { path = "../compiler_builtins" }
92 registry-dep-using-std = { version = "*", features = ['mockbuild'] }
93 +registry-dep-only-used-by-test = { version = "*" }
97 diff --git a/tests/testsuite/mock-std/library/test/src/lib.rs b/tests/testsuite/mock-std/library/test/src/lib.rs
98 index a112855f5..224b89bb2 100644
99 --- a/tests/testsuite/mock-std/library/test/src/lib.rs
100 +++ b/tests/testsuite/mock-std/library/test/src/lib.rs
101 @@ -7,4 +7,5 @@ extern crate test;
104 pub fn custom_api() {
105 + registry_dep_only_used_by_test::wow_testing_is_so_easy();
107 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
109 index 000000000..31ba65a98
111 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/Cargo.toml
114 +name = "registry-dep-only-used-by-test"
116 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
122 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
124 index 000000000..a68d2aeef
126 +++ b/tests/testsuite/mock-std/vendor/registry-dep-only-used-by-test/src/lib.rs
128 +pub fn wow_testing_is_so_easy() {
130 \ No newline at end of file
131 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
133 index 000000000..f7e4ab232
135 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/Cargo.toml
138 +name = "registry-dep-using-alloc"
140 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
144 +rustc-std-workspace-alloc = { version = "*", optional = true }
145 +rustc-std-workspace-core = { version = "*", optional = true }
148 +mockbuild = ["rustc-std-workspace-alloc", "rustc-std-workspace-core"]
149 \ No newline at end of file
150 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
152 index 000000000..b9ab30339
154 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-alloc/src/lib.rs
156 +#[cfg(feature = "mockbuild")]
157 +pub fn custom_api() {
160 +#[cfg(not(feature = "mockbuild"))]
161 +pub fn non_sysroot_api() {
162 + core::custom_api();
163 + alloc::custom_api();
165 \ No newline at end of file
166 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
168 index 000000000..befb83a63
170 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/Cargo.toml
173 +name = "registry-dep-using-core"
175 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
179 +rustc-std-workspace-core = { version = "*", optional = true }
182 +mockbuild = ["rustc-std-workspace-core"]
183 \ No newline at end of file
184 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
186 index 000000000..f9dbac0f4
188 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-core/src/lib.rs
190 +#[cfg(feature = "mockbuild")]
191 +pub fn custom_api() {
194 +#[cfg(not(feature = "mockbuild"))]
195 +pub fn non_sysroot_api() {
196 + core::custom_api();
198 \ No newline at end of file
199 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
201 index 000000000..71ef0a42f
203 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/Cargo.toml
206 +name = "registry-dep-using-std"
208 +authors = ["Alex Crichton <alex@alexcrichton.com>"]
212 +rustc-std-workspace-std = { version = "*", optional = true }
215 +mockbuild = ["rustc-std-workspace-std"]
216 \ No newline at end of file
217 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
219 index 000000000..f3af39178
221 +++ b/tests/testsuite/mock-std/vendor/registry-dep-using-std/src/lib.rs
223 +#[cfg(feature = "mockbuild")]
224 +pub fn custom_api() {
227 +#[cfg(not(feature = "mockbuild"))]
228 +pub fn non_sysroot_api() {
231 \ No newline at end of file
232 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
234 index 000000000..4465a08a8
236 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-alloc/Cargo.toml
238 +this file shouldn't be read
239 \ No newline at end of file
240 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
242 index 000000000..4465a08a8
244 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-core/Cargo.toml
246 +this file shouldn't be read
247 \ No newline at end of file
248 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
250 index 000000000..4465a08a8
252 +++ b/tests/testsuite/mock-std/vendor/rustc-std-workspace-std/Cargo.toml
254 +this file shouldn't be read
255 \ No newline at end of file
256 diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs
257 index d3be303ea..486a9b4e0 100644
258 --- a/tests/testsuite/standard_lib.rs
259 +++ b/tests/testsuite/standard_lib.rs
260 @@ -15,71 +15,18 @@ struct Setup {
263 fn setup() -> Setup {
264 - // Our mock sysroot requires a few packages from crates.io, so make sure
265 - // they're "published" to crates.io. Also edit their code a bit to make sure
266 - // that they have access to our custom crates with custom apis.
267 + // Register a version of one of the std dependencies that doesn't compile.
268 + // This ensures that the mock-std's vendor is actually being used.
269 Package::new("registry-dep-using-core", "1.0.0")
275 - #[cfg(feature = \"mockbuild\")]
276 - pub fn custom_api() {
279 - #[cfg(not(feature = \"mockbuild\"))]
280 - pub fn non_sysroot_api() {
281 - core::custom_api();
283 + don't compile me bro!!
286 .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
287 .feature("mockbuild", &["rustc-std-workspace-core"])
289 - Package::new("registry-dep-using-alloc", "1.0.0")
295 - extern crate alloc;
297 - #[cfg(feature = \"mockbuild\")]
298 - pub fn custom_api() {
301 - #[cfg(not(feature = \"mockbuild\"))]
302 - pub fn non_sysroot_api() {
303 - core::custom_api();
304 - alloc::custom_api();
308 - .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
309 - .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
312 - &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
315 - Package::new("registry-dep-using-std", "1.0.0")
319 - #[cfg(feature = \"mockbuild\")]
320 - pub fn custom_api() {
323 - #[cfg(not(feature = \"mockbuild\"))]
324 - pub fn non_sysroot_api() {
329 - .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
330 - .feature("mockbuild", &["rustc-std-workspace-std"])
333 let p = ProjectBuilder::new(paths::root().join("rustc-wrapper"))
335 @@ -335,6 +282,81 @@ fn depend_same_as_std() {
339 + // Our mock sysroot requires a few packages from crates.io, so make sure
340 + // they're "published" to crates.io. Also edit their code a bit to make sure
341 + // that they have access to our custom crates with custom apis.
342 + Package::new("registry-dep-using-core", "1.0.0")
348 + #[cfg(feature = \"mockbuild\")]
349 + pub fn custom_api() {
352 + #[cfg(not(feature = \"mockbuild\"))]
353 + pub fn non_sysroot_api() {
354 + core::custom_api();
358 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
359 + .feature("mockbuild", &["rustc-std-workspace-core"])
361 + Package::new("registry-dep-using-alloc", "1.0.0")
367 + extern crate alloc;
369 + #[cfg(feature = \"mockbuild\")]
370 + pub fn custom_api() {
373 + #[cfg(not(feature = \"mockbuild\"))]
374 + pub fn non_sysroot_api() {
375 + core::custom_api();
376 + alloc::custom_api();
380 + .add_dep(Dependency::new("rustc-std-workspace-core", "*").optional(true))
381 + .add_dep(Dependency::new("rustc-std-workspace-alloc", "*").optional(true))
384 + &["rustc-std-workspace-core", "rustc-std-workspace-alloc"],
387 + Package::new("registry-dep-using-std", "1.0.0")
391 + #[cfg(feature = \"mockbuild\")]
392 + pub fn custom_api() {
395 + #[cfg(not(feature = \"mockbuild\"))]
396 + pub fn non_sysroot_api() {
401 + .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true))
402 + .feature("mockbuild", &["rustc-std-workspace-std"])
404 + Package::new("registry-dep-only-used-by-test", "1.0.0")
408 + pub fn wow_testing_is_so_easy() {