Skip to content

Commit 98afb59

Browse files
committed
Bail out an error when using cargo:: in custom build script
Signed-off-by: hi-rustin <[email protected]>
1 parent 45782b6 commit 98afb59

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,20 @@ impl BuildOutput {
690690
continue;
691691
}
692692
let data = match iter.next() {
693-
Some(val) => val,
693+
Some(val) => {
694+
if val.starts_with(":") {
695+
// Line started with `cargo::`.
696+
bail!("invalid output in {}: `{}`\n\
697+
Expected a line with `cargo:key=value`, \
698+
but found `cargo::` instead.\n\
699+
`cargo::` is reserved for future use or some way to indicate that there is a version mismatch \
700+
between the build script and cargo, whether its because no MSRV was specified \
701+
or someone was reading too new of documentation. \
702+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
703+
for more information about build script outputs.", whence, line);
704+
}
705+
val
706+
}
694707
None => continue,
695708
};
696709

tests/testsuite/build_script.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,6 +5140,37 @@ for more information about build script outputs.
51405140
.run();
51415141
}
51425142

5143+
#[cargo_test]
5144+
fn wrong_syntax_with_two_colons() {
5145+
let p = project()
5146+
.file("src/lib.rs", "")
5147+
.file(
5148+
"build.rs",
5149+
r#"
5150+
fn main() {
5151+
println!("cargo::foo=bar");
5152+
}
5153+
"#,
5154+
)
5155+
.build();
5156+
5157+
p.cargo("build")
5158+
.with_status(101)
5159+
.with_stderr(
5160+
"\
5161+
[COMPILING] foo [..]
5162+
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar`
5163+
Expected a line with `cargo:key=value`, but found `cargo::` instead.
5164+
`cargo::` is reserved for future use or some way to indicate that there is a version mismatch \
5165+
between the build script and cargo, whether its because no MSRV was specified \
5166+
or someone was reading too new of documentation. \
5167+
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
5168+
for more information about build script outputs.
5169+
",
5170+
)
5171+
.run();
5172+
}
5173+
51435174
#[cargo_test]
51445175
fn custom_build_closes_stdin() {
51455176
// Ensure stdin is closed to prevent deadlock.

0 commit comments

Comments
 (0)