Skip to content

Commit

Permalink
Show error on JSON parse error and nonzero exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Nov 9, 2018
1 parent eeb3e16 commit 3f0c788
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/cargo/util/process_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt;
use std::path::Path;
use std::process::{Command, Output, Stdio};

use failure::Fail;
use jobserver::Client;
use shell_escape::escape;

Expand Down Expand Up @@ -271,19 +272,20 @@ impl ProcessBuilder {

{
let to_print = if capture_output { Some(&output) } else { None };
if !output.status.success() {
return Err(process_error(
&format!("process didn't exit successfully: {}", self),
Some(output.status),
to_print,
).into());
} else if let Some(e) = callback_error {
if let Some(e) = callback_error {
let cx = process_error(
&format!("failed to parse process output: {}", self),
Some(output.status),
to_print,
);
return Err(e.context(cx).into());
return Err(cx.context(e).into());
} else if !output.status.success() {
return Err(process_error(
&format!("process didn't exit successfully: {}", self),
Some(output.status),
to_print,
)
.into());
}
}

Expand Down
68 changes: 68 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4435,3 +4435,71 @@ Caused by:
.with_status(101)
.run();
}

#[test]
fn json_parse_fail() {
// Ensure when json parsing fails, and rustc exits with non-zero exit
// code, that a useful error message is displayed.
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
pm = { path = "pm" }
"#,
)
.file(
"src/lib.rs",
r#"
#[macro_use]
extern crate pm;
#[derive(Foo)]
pub struct S;
"#,
)
.file(
"pm/Cargo.toml",
r#"
[package]
name = "pm"
version = "0.1.0"
[lib]
proc-macro = true
"#,
)
.file(
"pm/src/lib.rs",
r#"
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_derive(Foo)]
pub fn derive(_input: TokenStream) -> TokenStream {
eprintln!("{{evil proc macro}}");
panic!("something went wrong");
}
"#,
)
.build();

foo.cargo("build --message-format=json")
.with_stderr(
"\
[COMPILING] pm [..]
[COMPILING] foo [..]
[ERROR] Could not compile `foo`.
Caused by:
compiler produced invalid json: `{evil proc macro}`
Caused by:
failed to parse process output: `rustc [..]
",
)
.with_status(101)
.run();
}

0 comments on commit 3f0c788

Please sign in to comment.