Skip to content

Commit

Permalink
Bail out an error when using cargo:: in custom build script
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
hi-rustin committed Jul 15, 2023
1 parent 45782b6 commit 98afb59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,20 @@ impl BuildOutput {
continue;
}
let data = match iter.next() {
Some(val) => val,
Some(val) => {
if val.starts_with(":") {
// Line started with `cargo::`.
bail!("invalid output in {}: `{}`\n\
Expected a line with `cargo:key=value`, \
but found `cargo::` instead.\n\
`cargo::` is reserved for future use or some way to indicate that there is a version mismatch \
between the build script and cargo, whether its because no MSRV was specified \
or someone was reading too new of documentation. \
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,
};

Expand Down
31 changes: 31 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,37 @@ 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: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo::foo=bar`
Expected a line with `cargo:key=value`, but found `cargo::` instead.
`cargo::` is reserved for future use or some way to indicate that there is a version mismatch \
between the build script and cargo, whether its because no MSRV was specified \
or someone was reading too new of documentation. \
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.
Expand Down

0 comments on commit 98afb59

Please sign in to comment.