Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,13 @@ fn add_dep_arg<'a, 'b: 'a>(
continue;
}
map.insert(&dep.unit, build_runner.files().deps_dir(&dep.unit));

// Proc macros are statically linked, so when including a proc-macro dependency we can skip
// adding it's dependencies. Note that we still do add them when we are compiling the
// proc-macro itself.
if dep.unit.target.proc_macro() {
continue;
}
add_dep_arg(map, build_runner, &dep.unit);
}
}
Expand Down
106 changes: 106 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6736,3 +6736,109 @@ qux (cdylib) on search path: true
"#]])
.run();
}

#[cargo_test(
nightly,
reason = "Depends on https://github.com/rust-lang/rust/pull/155439/changes/61f3e086acc1c187bb262ab43cac71f44018c397"
)]
fn should_not_include_proc_macro_deps_paths_in_rustc_args() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.0"
edition = "2021"
authors = []
resolver = "2"

[dependencies]
my-proc-macro = { path = "my-proc-macro" }
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"my-dylib/Cargo.toml",
r#"
[package]
name = "my-dylib"
version = "0.1.0"
edition = "2021"
authors = []

[lib]
crate-type = ["dylib"]

@weihanglo weihanglo Jul 18, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dylib here is important but easy to miss, can we add some comment around crate-type explaining it? Or we can rename bar to my-dylib and baz to my-proc-macro.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant explaining why we are testing against dylib not rlibs or others.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its important for dylibs but also applies to rlibs as well.

In hindsight, I probably should have been testing both.
I updated the test include both rlib and dylib deps to the proc-macro. Also updated the crate names to make it more clear.

lmk what you think :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you.

"#,
)
.file(
"my-dylib/src/lib.rs",
r#"pub fn value_from_dylib() -> i32 { 100 }"#,
)
.file(
"my-rlib/Cargo.toml",
r#"
[package]
name = "my-rlib"
version = "0.1.0"
edition = "2021"
authors = []
"#,
)
.file(
"my-rlib/src/lib.rs",
r#"pub fn value_from_rlib() -> i32 { 200 }"#,
)
.file(
"my-proc-macro/Cargo.toml",
r#"
[package]
name = "my-proc-macro"
version = "0.1.0"
edition = "2021"
authors = []

[lib]
proc-macro = true

[dependencies]
my-dylib = { path = "../my-dylib" }
my-rlib = { path = "../my-rlib" }
"#,
)
.file(
"my-proc-macro/src/lib.rs",
r#"
use proc_macro::TokenStream;
use my_dylib::value_from_dylib;
use my_rlib::value_from_rlib;

#[proc_macro]
pub fn make_bar(_item: TokenStream) -> TokenStream {
let val = value_from_dylib() + value_from_rlib();
format!("fn bar() -> u32 {{ {val} }}").parse().unwrap()
}
"#,
)
.build();

p.cargo("-Zbuild-dir-new-layout -v build")
.masquerade_as_nightly_cargo(&["new build-dir layout"])
.enable_mac_dsym()
// Verify that the proc-macro dependencies (my-rlib and my-dylib) are not added to the rustc
// invocation for the `foo` crate as `-L` args.
.with_stderr_data(str![[r#"
[LOCKING] 3 packages to latest compatible versions
[COMPILING] my-dylib v0.1.0 ([ROOT]/foo/my-dylib)
[RUNNING] `rustc --crate-name my_dylib [..]`
[COMPILING] my-rlib v0.1.0 ([ROOT]/foo/my-rlib)
[RUNNING] `rustc --crate-name my_rlib [..]`
[COMPILING] my-proc-macro v0.1.0 ([ROOT]/foo/my-proc-macro)
[RUNNING] `rustc --crate-name my_proc_macro [..]`
[COMPILING] foo v0.0.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo [..] --out-dir [ROOT]/foo/target/debug/build/foo/[HASH]/out -L dependency=[ROOT]/foo/target/debug/build/my-proc-macro/[HASH]/out --extern my_proc_macro=[ROOT]/foo/target/debug/build/my-proc-macro/[HASH]/out/[..] --verbose`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]].unordered())
.run();
}