Skip to content

do not pass cdylib link args to test #15317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
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
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
}

for (lt, arg) in &output.linker_args {
if lt.applies_to(&unit.target) {
if lt.applies_to(&unit.target, unit.mode) {
args.push("-C".into());
args.push(format!("link-arg={}", arg).into());
}
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::core::compiler::build_runner::UnitHash;
use crate::core::compiler::fingerprint::DirtyReason;
use crate::core::compiler::job_queue::JobState;
use crate::core::{profiles::ProfileRoot, PackageId, Target};
use crate::util::command_prelude::CompileMode;
use crate::util::errors::CargoResult;
use crate::util::internal;
use crate::util::machine_message::{self, Message};
Expand Down Expand Up @@ -196,10 +197,11 @@ pub enum LinkArgTarget {

impl LinkArgTarget {
/// Checks if this link type applies to a given [`Target`].
pub fn applies_to(&self, target: &Target) -> bool {
pub fn applies_to(&self, target: &Target, mode: CompileMode) -> bool {
let is_test = mode.is_any_test();
match self {
LinkArgTarget::All => true,
LinkArgTarget::Cdylib => target.is_cdylib(),
LinkArgTarget::Cdylib => !is_test && target.is_cdylib(),
LinkArgTarget::Bin => target.is_bin(),
LinkArgTarget::SingleBin(name) => target.is_bin() && target.name() == name,
LinkArgTarget::Test => target.is_test(),
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ fn rustc(
pass_l_flag,
&target,
current_id,
mode,
)?;
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
}
Expand Down Expand Up @@ -494,6 +495,7 @@ fn rustc(
pass_l_flag: bool,
target: &Target,
current_id: PackageId,
mode: CompileMode,
) -> CargoResult<()> {
for key in build_scripts.to_link.iter() {
let output = build_script_outputs.get(key.1).ok_or_else(|| {
Expand All @@ -520,7 +522,9 @@ fn rustc(
// clause should have been kept in the `if` block above. For
// now, continue allowing it for cdylib only.
// See https://github.com/rust-lang/cargo/issues/9562
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkArgTarget::Cdylib) {
if lt.applies_to(target, mode)
&& (key.0 == current_id || *lt == LinkArgTarget::Cdylib)
{
rustc.arg("-C").arg(format!("link-arg={}", arg));
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,40 @@ fn cdylib_both_forms() {
"#]])
.run();
}

// https://github.com/rust-lang/cargo/issues/12663
#[cargo_test]
fn cdylib_extra_link_args_should_not_apply_to_unit_tests() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.5.0"
edition = "2015"

[lib]
crate-type = ["lib", "cdylib"]
"#,
)
.file(
"src/lib.rs",
r#"
#[test]
fn noop() {}
"#,
)
.file(
"build.rs",
r#"
fn main() {
// This would fail if cargo passed `-lhack` to building the test because `hack` doesn't exist.
println!("cargo::rustc-link-arg-cdylib=-lhack");
}
"#,
)
.build();

p.cargo("test --lib").run();
}