From 33e6b331c820e443c7089af964a489bce28201a8 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 21 Nov 2024 23:57:47 -0500 Subject: [PATCH 1/2] test(build-std): shows that proc macro unittest fails This starts failing since https://github.com/rust-lang/rust/pull/131188 --- tests/build-std/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index a106b38b367..07c6919fb77 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -360,3 +360,41 @@ 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]) +dyld[[..]]: Library not loaded: @rpath/libstd-[HASH].dylib + Referenced from: <[..]> [ROOT]/foo/target/debug/deps/foo-[HASH] + Reason: tried: '[ROOT]/foo/target/debug/deps/libstd-[HASH].dylib' (no such file), '[ROOT]/foo/target/debug/libstd-[HASH].dylib' (no such file), '/usr/local/lib/libstd-[HASH].dylib' (no such file), '/usr/lib/libstd-[HASH].dylib' (no such file, not in dyld cache) +[ERROR] test failed, to rerun pass `--lib` + +Caused by: + process didn't exit successfully: `[ROOT]/foo/target/debug/deps/foo-[HASH]` ([..]) + +"#]]) + .with_status(101) + .run(); +} From 4527567c9f83a8985a96a75577fd46c9bfa786d6 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 22 Nov 2024 00:01:14 -0500 Subject: [PATCH 2/2] fix(build-std): always link to std when testing/running proc-macros --- src/cargo/core/compiler/compilation.rs | 5 ++++- tests/build-std/main.rs | 8 -------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index c31ee9ee4eb..7531f0fa61b 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -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()); } } diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 07c6919fb77..9906d0a890f 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -386,15 +386,7 @@ fn test_proc_macro() { [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]) -dyld[[..]]: Library not loaded: @rpath/libstd-[HASH].dylib - Referenced from: <[..]> [ROOT]/foo/target/debug/deps/foo-[HASH] - Reason: tried: '[ROOT]/foo/target/debug/deps/libstd-[HASH].dylib' (no such file), '[ROOT]/foo/target/debug/libstd-[HASH].dylib' (no such file), '/usr/local/lib/libstd-[HASH].dylib' (no such file), '/usr/lib/libstd-[HASH].dylib' (no such file, not in dyld cache) -[ERROR] test failed, to rerun pass `--lib` - -Caused by: - process didn't exit successfully: `[ROOT]/foo/target/debug/deps/foo-[HASH]` ([..]) "#]]) - .with_status(101) .run(); }