Skip to content

Commit 8ba3ec2

Browse files
authored
fix(build-std): always link to std when testing proc-macros (#14850)
### What does this PR try to resolve? Fixes #14735 rust-lang/rust#131188 removes libstd.so from sysroot so `cargo test -Zbuild-std` no longer links to it. That results in a "Library not loaded: @rpath/libstd-[HASH].dylib" when testing a proc macro. This is a pretty niche use case, though it can be easily reproduced by running `cargo test -Zbuild-std` in any proc-macro package. Like in [serde-rs/serde](https://github.com/serde-rs/serde/tree/b9dbfcb4ac3b7a663d9efc6eb1387c62302a6fb4) running it would fail. This patch adds a special case that if it is `cargo run/test` against a proc-macro, fill in std dynamic library search path for it. ### How should we test and review this PR? ``` CARGO_RUN_BUILD_STD_TESTS=1 cargo +nightly t --test build-std test_proc_macro ``` or ``` git clone https://github.com/serde-rs/serde cd serde git switch -d b9dbfcb4ac3b7a663d9efc6eb1387c62302a6fb4 cargo +nightly t --test build-std ``` ### Additional information
2 parents 4c39aaf + 4527567 commit 8ba3ec2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/cargo/core/compiler/compilation.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ impl<'gctx> Compilation<'gctx> {
315315
// libs from the sysroot that ships with rustc. This may not be
316316
// required (at least I cannot craft a situation where it
317317
// matters), but is here to be safe.
318-
if self.gctx.cli_unstable().build_std.is_none() {
318+
if self.gctx.cli_unstable().build_std.is_none() ||
319+
// Proc macros dynamically link to std, so set it anyway.
320+
pkg.proc_macro()
321+
{
319322
search_path.push(self.sysroot_target_libdir[&kind].clone());
320323
}
321324
}

tests/build-std/main.rs

+30
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,33 @@ fn remap_path_scope() {
360360
)
361361
.run();
362362
}
363+
364+
#[cargo_test(build_std_real)]
365+
fn test_proc_macro() {
366+
// See rust-lang/cargo#14735
367+
let p = project()
368+
.file(
369+
"Cargo.toml",
370+
r#"
371+
[package]
372+
name = "foo"
373+
edition = "2021"
374+
375+
[lib]
376+
proc-macro = true
377+
"#,
378+
)
379+
.file("src/lib.rs", "")
380+
.build();
381+
382+
p.cargo("test --lib")
383+
.env_remove(cargo_util::paths::dylib_path_envvar())
384+
.build_std()
385+
.with_stderr_data(str![[r#"
386+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
387+
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
388+
[RUNNING] unittests src/lib.rs (target/debug/deps/foo-[HASH])
389+
390+
"#]])
391+
.run();
392+
}

0 commit comments

Comments
 (0)