Skip to content

Commit 836e537

Browse files
committed
Make cargo:rustc-link-arg-bin without the = an error.
1 parent f676b49 commit 836e537

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -599,29 +599,32 @@ impl BuildOutput {
599599
}
600600
"rustc-link-arg-bin" => {
601601
if extra_link_arg {
602-
let parts = value.splitn(2, "=").collect::<Vec<_>>();
603-
if parts.len() == 2 {
604-
let bin_name = parts[0].to_string();
605-
if !targets
606-
.iter()
607-
.any(|target| target.is_bin() && target.name() == bin_name)
608-
{
609-
bail!(
610-
"invalid instruction `cargo:{}` from {}\n\
611-
The package {} does not have a bin target with the name `{}`.",
612-
key,
613-
whence,
614-
pkg_descr,
615-
bin_name
616-
);
617-
}
618-
linker_args.push((LinkType::SingleBin(bin_name), parts[1].to_string()));
619-
} else {
620-
warnings.push(format!(
621-
"cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`",
622-
key, key
623-
));
602+
let mut parts = value.splitn(2, '=');
603+
let bin_name = parts.next().unwrap().to_string();
604+
let arg = parts.next().ok_or_else(|| {
605+
anyhow::format_err!(
606+
"invalid instruction `cargo:{}={}` from {}\n\
607+
The instruction should have the form cargo:{}=BIN=ARG",
608+
key,
609+
value,
610+
whence,
611+
key
612+
)
613+
})?;
614+
if !targets
615+
.iter()
616+
.any(|target| target.is_bin() && target.name() == bin_name)
617+
{
618+
bail!(
619+
"invalid instruction `cargo:{}` from {}\n\
620+
The package {} does not have a bin target with the name `{}`.",
621+
key,
622+
whence,
623+
pkg_descr,
624+
bin_name
625+
);
624626
}
627+
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
625628
} else {
626629
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
627630
}

tests/testsuite/build_script_extra_link_arg.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
162162
[COMPILING] foo [..]
163163
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
164164
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
165+
",
166+
)
167+
.run();
168+
169+
p.change_file(
170+
"build.rs",
171+
r#"fn main() { println!("cargo:rustc-link-arg-bin=abc"); }"#,
172+
);
173+
174+
p.cargo("check -Zextra-link-arg")
175+
.masquerade_as_nightly_cargo()
176+
.with_status(101)
177+
.with_stderr(
178+
"\
179+
[COMPILING] foo [..]
180+
error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
181+
The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
165182
",
166183
)
167184
.run();

0 commit comments

Comments
 (0)