Skip to content

Commit 1fec089

Browse files
committed
Auto merge of #13874 - torhovland:old-syntax-suggestion, r=weihanglo
Old syntax suggestion Fixes #13868. The build error in the issue will now include a suggestion: ``` Compiling zerocopy v0.8.0-alpha.9 error: the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, but the minimum supported Rust version of `zerocopy v0.8.0-alpha.9` is 1.56.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. ``` The suggestion is only included for reserved prefixes. A test has been added.
2 parents 91a6b8f + 3ea3638 commit 1fec089

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,16 +722,35 @@ 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>,
734+
flag: &str,
728735
) -> CargoResult<()> {
729736
if let Some(msrv) = msrv {
730737
let new_syntax_added_in = RustVersion::from_str("1.77.0")?;
731738
if !new_syntax_added_in.is_compatible_with(msrv.as_partial()) {
739+
let old_syntax_suggestion = if has_reserved_prefix(flag) {
740+
format!(
741+
"Switch to the old `cargo:{flag}` syntax (note the single colon).\n"
742+
)
743+
} else if flag.starts_with("metadata=") {
744+
let old_format_flag = flag.strip_prefix("metadata=").unwrap();
745+
format!("Switch to the old `cargo:{old_format_flag}` syntax instead of `cargo::{flag}` (note the single colon).\n")
746+
} else {
747+
String::new()
748+
};
749+
732750
bail!(
733751
"the `cargo::` syntax for build script output instructions was added in \
734752
Rust 1.77.0, but the minimum supported Rust version of `{pkg_descr}` is {msrv}.\n\
753+
{old_syntax_suggestion}\
735754
{DOCS_LINK_SUGGESTION}"
736755
);
737756
}
@@ -793,16 +812,13 @@ impl BuildOutput {
793812
};
794813
let mut old_syntax = false;
795814
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
796-
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv)?;
815+
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv, data)?;
797816
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
798817
parse_directive(whence.as_str(), line, data, old_syntax)?
799818
} else if let Some(data) = line.strip_prefix("cargo:") {
800819
old_syntax = true;
801820
// For instance, `cargo:rustc-flags=foo`.
802-
if RESERVED_PREFIXES
803-
.iter()
804-
.any(|prefix| data.starts_with(prefix))
805-
{
821+
if has_reserved_prefix(data) {
806822
parse_directive(whence.as_str(), line, data, old_syntax)?
807823
} else {
808824
// For instance, `cargo:foo=bar`.

tests/testsuite/build_script.rs

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ fn custom_build_invalid_host_config_feature_flag() {
625625
.with_status(101)
626626
.with_stderr_contains(
627627
"\
628-
error: the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set
628+
[ERROR] the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set
629629
",
630630
)
631631
.run();
@@ -1038,7 +1038,7 @@ fn links_duplicates() {
10381038

10391039
p.cargo("build").with_status(101)
10401040
.with_stderr("\
1041-
error: failed to select a version for `a-sys`.
1041+
[ERROR] failed to select a version for `a-sys`.
10421042
... required by package `foo v0.5.0 ([..])`
10431043
versions that meet the requirements `*` are: 0.5.0
10441044
@@ -1163,7 +1163,7 @@ fn links_duplicates_deep_dependency() {
11631163

11641164
p.cargo("build").with_status(101)
11651165
.with_stderr("\
1166-
error: failed to select a version for `a-sys`.
1166+
[ERROR] failed to select a version for `a-sys`.
11671167
... required by package `a v0.5.0 ([..])`
11681168
... which satisfies path dependency `a` of package `foo v0.5.0 ([..])`
11691169
versions that meet the requirements `*` are: 0.5.0
@@ -4508,7 +4508,7 @@ fn links_duplicates_with_cycle() {
45084508

45094509
p.cargo("build").with_status(101)
45104510
.with_stderr("\
4511-
error: failed to select a version for `a`.
4511+
[ERROR] failed to select a version for `a`.
45124512
... required by package `foo v0.5.0 ([..])`
45134513
versions that meet the requirements `*` are: 0.5.0
45144514
@@ -5315,7 +5315,7 @@ fn wrong_output() {
53155315
.with_stderr(
53165316
"\
53175317
[COMPILING] foo [..]
5318-
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::example`
5318+
[ERROR] invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::example`
53195319
Expected a line with `cargo::KEY=VALUE` with an `=` character, but none was found.
53205320
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
53215321
for more information about build script outputs.
@@ -5405,7 +5405,7 @@ fn test_invalid_old_syntax() {
54055405
.with_stderr(
54065406
"\
54075407
[COMPILING] foo [..]
5408-
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo:foo`
5408+
[ERROR] invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo:foo`
54095409
Expected a line with `cargo:KEY=VALUE` with an `=` character, but none was found.
54105410
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
54115411
for more information about build script outputs.
@@ -5434,7 +5434,7 @@ fn test_invalid_new_syntax() {
54345434
.with_stderr(
54355435
"\
54365436
[COMPILING] foo [..]
5437-
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::metadata=foo`
5437+
[ERROR] invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::metadata=foo`
54385438
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, but none was found.
54395439
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
54405440
for more information about build script outputs.
@@ -5459,7 +5459,7 @@ for more information about build script outputs.
54595459
.with_stderr(
54605460
"\
54615461
[COMPILING] foo [..]
5462-
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar`
5462+
[ERROR] invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar`
54635463
Unknown key: `foo`.
54645464
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
54655465
for more information about build script outputs.
@@ -5499,7 +5499,89 @@ fn test_new_syntax_with_old_msrv() {
54995499
.with_stderr_contains(
55005500
"\
55015501
[COMPILING] foo [..]
5502-
error: the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
5502+
[ERROR] the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
5503+
but the minimum supported Rust version of `foo v0.5.0 ([ROOT]/foo)` is 1.60.0.
5504+
Switch to the old `cargo:foo=bar` syntax instead of `cargo::metadata=foo=bar` (note the single colon).
5505+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5506+
for more information about build script outputs.
5507+
",
5508+
)
5509+
.run();
5510+
}
5511+
5512+
#[cargo_test]
5513+
fn test_new_syntax_with_old_msrv_and_reserved_prefix() {
5514+
let p = project()
5515+
.file(
5516+
"Cargo.toml",
5517+
r#"
5518+
[package]
5519+
name = "foo"
5520+
version = "0.5.0"
5521+
edition = "2015"
5522+
authors = []
5523+
build = "build.rs"
5524+
rust-version = "1.60.0"
5525+
"#,
5526+
)
5527+
.file("src/lib.rs", "")
5528+
.file(
5529+
"build.rs",
5530+
r#"
5531+
fn main() {
5532+
println!("cargo::rustc-check-cfg=cfg(foo)");
5533+
}
5534+
"#,
5535+
)
5536+
.build();
5537+
5538+
p.cargo("build")
5539+
.with_status(101)
5540+
.with_stderr_contains(
5541+
"\
5542+
[COMPILING] foo [..]
5543+
[ERROR] the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
5544+
but the minimum supported Rust version of `foo v0.5.0 ([ROOT]/foo)` is 1.60.0.
5545+
Switch to the old `cargo:rustc-check-cfg=cfg(foo)` syntax (note the single colon).
5546+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5547+
for more information about build script outputs.
5548+
",
5549+
)
5550+
.run();
5551+
}
5552+
5553+
#[cargo_test]
5554+
fn test_new_syntax_with_old_msrv_and_unknown_prefix() {
5555+
let p = project()
5556+
.file(
5557+
"Cargo.toml",
5558+
r#"
5559+
[package]
5560+
name = "foo"
5561+
version = "0.5.0"
5562+
edition = "2015"
5563+
authors = []
5564+
build = "build.rs"
5565+
rust-version = "1.60.0"
5566+
"#,
5567+
)
5568+
.file("src/lib.rs", "")
5569+
.file(
5570+
"build.rs",
5571+
r#"
5572+
fn main() {
5573+
println!("cargo::foo=bar");
5574+
}
5575+
"#,
5576+
)
5577+
.build();
5578+
5579+
p.cargo("build")
5580+
.with_status(101)
5581+
.with_stderr_contains(
5582+
"\
5583+
[COMPILING] foo [..]
5584+
[ERROR] the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \
55035585
but the minimum supported Rust version of `foo v0.5.0 ([ROOT]/foo)` is 1.60.0.
55045586
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
55055587
for more information about build script outputs.

0 commit comments

Comments
 (0)