diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index a1fa78f6032..ed13a9d3ed8 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -599,29 +599,32 @@ impl BuildOutput { } "rustc-link-arg-bin" => { if extra_link_arg { - let parts = value.splitn(2, "=").collect::>(); - if parts.len() == 2 { - let bin_name = parts[0].to_string(); - if !targets - .iter() - .any(|target| target.is_bin() && target.name() == bin_name) - { - bail!( - "invalid instruction `cargo:{}` from {}\n\ - The package {} does not have a bin target with the name `{}`.", - key, - whence, - pkg_descr, - bin_name - ); - } - linker_args.push((LinkType::SingleBin(bin_name), parts[1].to_string())); - } else { - warnings.push(format!( - "cargo:{} has invalid syntax: expected `cargo:{}=BIN=ARG`", - key, key - )); + let mut parts = value.splitn(2, '='); + let bin_name = parts.next().unwrap().to_string(); + let arg = parts.next().ok_or_else(|| { + anyhow::format_err!( + "invalid instruction `cargo:{}={}` from {}\n\ + The instruction should have the form cargo:{}=BIN=ARG", + key, + value, + whence, + key + ) + })?; + if !targets + .iter() + .any(|target| target.is_bin() && target.name() == bin_name) + { + bail!( + "invalid instruction `cargo:{}` from {}\n\ + The package {} does not have a bin target with the name `{}`.", + key, + whence, + pkg_descr, + bin_name + ); } + linker_args.push((LinkType::SingleBin(bin_name), arg.to_string())); } else { warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key)); } diff --git a/tests/testsuite/build_script_extra_link_arg.rs b/tests/testsuite/build_script_extra_link_arg.rs index c64e4101c76..6e8384b8a27 100644 --- a/tests/testsuite/build_script_extra_link_arg.rs +++ b/tests/testsuite/build_script_extra_link_arg.rs @@ -162,6 +162,23 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target. [COMPILING] foo [..] error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)` The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`. +", + ) + .run(); + + p.change_file( + "build.rs", + r#"fn main() { println!("cargo:rustc-link-arg-bin=abc"); }"#, + ); + + p.cargo("check -Zextra-link-arg") + .masquerade_as_nightly_cargo() + .with_status(101) + .with_stderr( + "\ +[COMPILING] foo [..] +error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)` +The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG ", ) .run();