Skip to content

Commit

Permalink
fix(build-std): always link to std when testing proc-macros (#14850)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
ehuss authored Nov 26, 2024
2 parents 4c39aaf + 4527567 commit 8ba3ec2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ impl<'gctx> Compilation<'gctx> {
// libs from the sysroot that ships with rustc. This may not be
// required (at least I cannot craft a situation where it
// matters), but is here to be safe.
if self.gctx.cli_unstable().build_std.is_none() {
if self.gctx.cli_unstable().build_std.is_none() ||
// Proc macros dynamically link to std, so set it anyway.
pkg.proc_macro()
{
search_path.push(self.sysroot_target_libdir[&kind].clone());
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/build-std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,33 @@ fn remap_path_scope() {
)
.run();
}

#[cargo_test(build_std_real)]
fn test_proc_macro() {
// See rust-lang/cargo#14735
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
edition = "2021"
[lib]
proc-macro = true
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("test --lib")
.env_remove(cargo_util::paths::dylib_path_envvar())
.build_std()
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.0 ([ROOT]/foo)
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] unittests src/lib.rs (target/debug/deps/foo-[HASH])
"#]])
.run();
}

0 comments on commit 8ba3ec2

Please sign in to comment.