Skip to content

Commit

Permalink
Auto merge of #6290 - ehuss:json-parse-error, r=alexcrichton
Browse files Browse the repository at this point in the history
Show error on JSON parse error and nonzero exit.

If JSON parsing failed, and rustc exits nonzero, the error was lost.
This would have made it easier to diagnose #5992.
  • Loading branch information
bors committed Nov 9, 2018
2 parents 1292a6d + 3f0c788 commit 6582378
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 6582378

Please sign in to comment.