Skip to content

Commit 4b49826

Browse files
committed
Added a similar suggestion when using cargo::metadata=.
1 parent 4aac847 commit 4b49826

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -722,24 +722,28 @@ impl BuildOutput {
722722
const DOCS_LINK_SUGGESTION: &str = "See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
723723
for more information about build script outputs.";
724724

725+
fn has_reserved_prefix(flag: &str) -> bool {
726+
RESERVED_PREFIXES
727+
.iter()
728+
.any(|reserved_prefix| flag.starts_with(reserved_prefix))
729+
}
730+
725731
fn check_minimum_supported_rust_version_for_new_syntax(
726732
pkg_descr: &str,
727733
msrv: &Option<RustVersion>,
728-
key: &str,
734+
flag: &str,
729735
) -> CargoResult<()> {
730736
if let Some(msrv) = msrv {
731737
let new_syntax_added_in = RustVersion::from_str("1.77.0")?;
732738
if !new_syntax_added_in.is_compatible_with(msrv.as_partial()) {
733-
let prefix = format!("{key}=");
734-
735-
let old_syntax_suggestion = RESERVED_PREFIXES
736-
.contains(&&*prefix)
737-
.then(|| {
738-
format!(
739-
"Consider using the old `cargo:` syntax in front of `{prefix}`.\n"
740-
)
741-
})
742-
.unwrap_or_default();
739+
let old_syntax_suggestion = if has_reserved_prefix(flag) {
740+
format!("Consider using the old `cargo:` syntax in front of `{flag}`.\n")
741+
} else if flag.starts_with("metadata=") {
742+
let old_format_flag = flag.strip_prefix("metadata=").unwrap();
743+
format!("Consider using the old `cargo:{old_format_flag}` syntax instead of `cargo::{flag}` (note the single colon).\n")
744+
} else {
745+
String::new()
746+
};
743747

744748
bail!(
745749
"the `cargo::` syntax for build script output instructions was added in \
@@ -806,17 +810,13 @@ impl BuildOutput {
806810
};
807811
let mut old_syntax = false;
808812
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
813+
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv, data)?;
809814
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
810-
let (key, value) = parse_directive(whence.as_str(), line, data, old_syntax)?;
811-
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv, key)?;
812-
(key, value)
815+
parse_directive(whence.as_str(), line, data, old_syntax)?
813816
} else if let Some(data) = line.strip_prefix("cargo:") {
814817
old_syntax = true;
815818
// For instance, `cargo:rustc-flags=foo`.
816-
if RESERVED_PREFIXES
817-
.iter()
818-
.any(|prefix| data.starts_with(prefix))
819-
{
819+
if has_reserved_prefix(data) {
820820
parse_directive(whence.as_str(), line, data, old_syntax)?
821821
} else {
822822
// For instance, `cargo:foo=bar`.

tests/testsuite/build_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5541,7 +5541,7 @@ fn test_new_syntax_with_old_msrv_and_reserved_prefix() {
55415541
[COMPILING] foo [..]
55425542
error: the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
55435543
but the minimum supported Rust version of `foo v0.5.0 ([ROOT]/foo)` is 1.60.0.
5544-
Consider using the old `cargo:` syntax in front of `rustc-check-cfg=`.
5544+
Consider using the old `cargo:` syntax in front of `rustc-check-cfg=cfg(foo)`.
55455545
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
55465546
for more information about build script outputs.
55475547
",

0 commit comments

Comments
 (0)