Skip to content

Show error on JSON parse error and nonzero exit. #6290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}