From 4bdeb53cb514100e1cfe8267adc9774c443d34ba Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Fri, 7 Jul 2023 09:33:41 +0800 Subject: [PATCH] Bail out an error when using `cargo::` in custom build script Signed-off-by: hi-rustin --- src/cargo/core/compiler/custom_build.rs | 12 +++++++++- tests/testsuite/build_script.rs | 29 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index d1746217497c..85306aaac878 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -690,7 +690,17 @@ impl BuildOutput { continue; } let data = match iter.next() { - Some(val) => val, + Some(val) => { + if val.starts_with(":") { + // Line started with `cargo::`. + bail!("unsupported output in {}: `{}`\n\ + Found a `cargo::key=value` build directive which is reserved for future use.\n\ + Either change the directive to `cargo:key=value` syntax (note the single `:`) or upgrade your version of Rust.\n\ + See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \ + for more information about build script outputs.", whence, line); + } + val + } None => continue, }; diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 4840356c6bae..400d10547c43 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -5140,6 +5140,35 @@ for more information about build script outputs. .run(); } +#[cargo_test] +fn wrong_syntax_with_two_colons() { + let p = project() + .file("src/lib.rs", "") + .file( + "build.rs", + r#" + fn main() { + println!("cargo::foo=bar"); + } + "#, + ) + .build(); + + p.cargo("build") + .with_status(101) + .with_stderr( + "\ +[COMPILING] foo [..] +error: unsupported output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar` +Found a `cargo::key=value` build directive which is reserved for future use. +Either change the directive to `cargo:key=value` syntax (note the single `:`) or upgrade your version of Rust. +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 custom_build_closes_stdin() { // Ensure stdin is closed to prevent deadlock.