Skip to content

Commit

Permalink
Old syntax suggestion.
Browse files Browse the repository at this point in the history
  • Loading branch information
torhovland committed May 7, 2024
1 parent d72d0b2 commit 9686982
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,26 @@ impl BuildOutput {
fn check_minimum_supported_rust_version_for_new_syntax(
pkg_descr: &str,
msrv: &Option<RustVersion>,
key: &str,
) -> CargoResult<()> {
if let Some(msrv) = msrv {
let new_syntax_added_in = RustVersion::from_str("1.77.0")?;
if !new_syntax_added_in.is_compatible_with(msrv.as_partial()) {
let prefix = format!("{key}=");

let old_syntax_suggestion = RESERVED_PREFIXES
.contains(&&*prefix)
.then(|| {
format!(
"Consider using the old `cargo:` syntax in front of `{prefix}`.\n"
)
})
.unwrap_or_default();

bail!(
"the `cargo::` syntax for build script output instructions was added in \
Rust 1.77.0, but the minimum supported Rust version of `{pkg_descr}` is {msrv}.\n\
{old_syntax_suggestion}\
{DOCS_LINK_SUGGESTION}"
);
}
Expand Down Expand Up @@ -793,9 +806,10 @@ impl BuildOutput {
};
let mut old_syntax = false;
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv)?;
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
parse_directive(whence.as_str(), line, data, old_syntax)?
let (key, value) = parse_directive(whence.as_str(), line, data, old_syntax)?;
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv, key)?;
(key, value)
} else if let Some(data) = line.strip_prefix("cargo:") {
old_syntax = true;
// For instance, `cargo:rustc-flags=foo`.
Expand Down
41 changes: 41 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5508,6 +5508,47 @@ for more information about build script outputs.
.run();
}

#[cargo_test]
fn test_new_syntax_with_old_msrv_and_reserved_prefix() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.5.0"
edition = "2015"
authors = []
build = "build.rs"
rust-version = "1.60.0"
"#,
)
.file("src/lib.rs", "")
.file(
"build.rs",
r#"
fn main() {
println!("cargo::rustc-check-cfg=cfg(foo)");
}
"#,
)
.build();

p.cargo("build")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo [..]
error: the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
but the minimum supported Rust version of `foo v0.5.0 ([ROOT]/foo)` is 1.60.0.
Consider using the old `cargo:` syntax in front of `rustc-check-cfg=`.
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
for more information about build script outputs.
",
)
.run();
}

#[cargo_test]
fn test_new_syntax_with_compatible_partial_msrv() {
let p = project()
Expand Down

0 comments on commit 9686982

Please sign in to comment.