Skip to content

Commit

Permalink
cargo:warning+= syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Nov 6, 2022
1 parent db04f5f commit 2def6a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
35 changes: 29 additions & 6 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use std::path::{Path, PathBuf};
use std::str;
use std::sync::{Arc, Mutex};

const CARGO_WARNING: &str = "cargo:warning=";
const CARGO_ERROR: &str = "cargo:error=";
const CARGO_LINE_PREFIX: &str = "cargo:";

/// Contains the parsed output of a custom build script.
#[derive(Clone, Debug, Hash, Default)]
Expand Down Expand Up @@ -401,10 +400,28 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
let mut diagnostics = Vec::new();
let script_result = cmd.exec_with_streaming(
&mut |stdout| {
if let Some(warning) = stdout.strip_prefix(CARGO_WARNING) {
diagnostics.push(Diagnostic::Warning(warning.to_owned()));
} else if let Some(error) = stdout.strip_prefix(CARGO_ERROR) {
diagnostics.push(Diagnostic::Error(error.to_owned()));
let kv = stdout.strip_prefix(CARGO_LINE_PREFIX).and_then(|kv| {
let mut kv = kv.splitn(2, '=');
Some((kv.next()?, kv.next()?))
});
if let Some((key, value)) = kv {
match key {
"warning" => diagnostics.push(Diagnostic::Warning(value.to_owned())),
"error" => diagnostics.push(Diagnostic::Error(value.to_owned())),
"warning+" => {
if let Some(Diagnostic::Warning(msg)) = diagnostics.last_mut() {
msg.push_str("\n ");
msg.push_str(value);
}
}
"error+" => {
if let Some(Diagnostic::Error(msg)) = diagnostics.last_mut() {
msg.push_str("\n ");
msg.push_str(value);
}
}
_ => {}
}
}
if extra_verbose {
state.stdout(format!("{}{}", prefix, stdout))?;
Expand Down Expand Up @@ -775,6 +792,12 @@ impl BuildOutput {
}
// "error" is not parsed here for backwards compatibility, and because this function is for successful outputs
"warning" => diagnostics.push(Diagnostic::Warning(value.to_string())),
"warning+" => {
if let Some(Diagnostic::Warning(msg)) = diagnostics.last_mut() {
msg.push_str("\n ");
msg.push_str(&value);
}
}
"rerun-if-changed" => rerun_if_changed.push(PathBuf::from(value)),
"rerun-if-env-changed" => rerun_if_env_changed.push(value.to_string()),
_ => metadata.push((key.to_string(), value.to_string())),
Expand Down
11 changes: 10 additions & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ fn custom_build_script_failed_custom_error() {
"build.rs",
r#"fn main() {
println!("cargo:error=failed");
println!("cargo:error+=because oops");
println!("cargo:warning=multi");
println!("cargo:warning+=line");
println!("cargo:warning+=warning");
println!("cargo:error=error2");
println!("cargo:warning+=this one has nothing to append to");
std::process::exit(101);
}"#,
)
Expand All @@ -76,8 +81,12 @@ fn custom_build_script_failed_custom_error() {
"\
[COMPILING] foo v0.5.0 ([CWD])
[ERROR] failed
because oops
[WARNING] multi
line
warning
[ERROR] error2
[ERROR] could not build `foo` due to 2 previous errors
[ERROR] could not build `foo` due to 2 previous errors; 1 warning emitted
[NOTE] for more details, run again with '--verbose'",
)
.run();
Expand Down

0 comments on commit 2def6a9

Please sign in to comment.