mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
fix(build-std): always link to std when testing proc-macros (#14850)
### What does this PR try to resolve?
Fixes #14735
https://github.com/rust-lang/rust/pull/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](b9dbfcb4ac
)
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
This commit is contained in:
commit
8ba3ec2895
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user