Skip to content

Commit 3f0c788

Browse files
committed
Show error on JSON parse error and nonzero exit.
1 parent eeb3e16 commit 3f0c788

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

Diff for: src/cargo/util/process_builder.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt;
55
use std::path::Path;
66
use std::process::{Command, Output, Stdio};
77

8+
use failure::Fail;
89
use jobserver::Client;
910
use shell_escape::escape;
1011

@@ -271,19 +272,20 @@ impl ProcessBuilder {
271272

272273
{
273274
let to_print = if capture_output { Some(&output) } else { None };
274-
if !output.status.success() {
275-
return Err(process_error(
276-
&format!("process didn't exit successfully: {}", self),
277-
Some(output.status),
278-
to_print,
279-
).into());
280-
} else if let Some(e) = callback_error {
275+
if let Some(e) = callback_error {
281276
let cx = process_error(
282277
&format!("failed to parse process output: {}", self),
283278
Some(output.status),
284279
to_print,
285280
);
286-
return Err(e.context(cx).into());
281+
return Err(cx.context(e).into());
282+
} else if !output.status.success() {
283+
return Err(process_error(
284+
&format!("process didn't exit successfully: {}", self),
285+
Some(output.status),
286+
to_print,
287+
)
288+
.into());
287289
}
288290
}
289291

Diff for: tests/testsuite/build.rs

+68
Original file line numberDiff line numberDiff line change
@@ -4435,3 +4435,71 @@ Caused by:
44354435
.with_status(101)
44364436
.run();
44374437
}
4438+
4439+
#[test]
4440+
fn json_parse_fail() {
4441+
// Ensure when json parsing fails, and rustc exits with non-zero exit
4442+
// code, that a useful error message is displayed.
4443+
let foo = project()
4444+
.file(
4445+
"Cargo.toml",
4446+
r#"
4447+
[package]
4448+
name = "foo"
4449+
version = "0.1.0"
4450+
[dependencies]
4451+
pm = { path = "pm" }
4452+
"#,
4453+
)
4454+
.file(
4455+
"src/lib.rs",
4456+
r#"
4457+
#[macro_use]
4458+
extern crate pm;
4459+
4460+
#[derive(Foo)]
4461+
pub struct S;
4462+
"#,
4463+
)
4464+
.file(
4465+
"pm/Cargo.toml",
4466+
r#"
4467+
[package]
4468+
name = "pm"
4469+
version = "0.1.0"
4470+
[lib]
4471+
proc-macro = true
4472+
"#,
4473+
)
4474+
.file(
4475+
"pm/src/lib.rs",
4476+
r#"
4477+
extern crate proc_macro;
4478+
use proc_macro::TokenStream;
4479+
4480+
#[proc_macro_derive(Foo)]
4481+
pub fn derive(_input: TokenStream) -> TokenStream {
4482+
eprintln!("{{evil proc macro}}");
4483+
panic!("something went wrong");
4484+
}
4485+
"#,
4486+
)
4487+
.build();
4488+
4489+
foo.cargo("build --message-format=json")
4490+
.with_stderr(
4491+
"\
4492+
[COMPILING] pm [..]
4493+
[COMPILING] foo [..]
4494+
[ERROR] Could not compile `foo`.
4495+
4496+
Caused by:
4497+
compiler produced invalid json: `{evil proc macro}`
4498+
4499+
Caused by:
4500+
failed to parse process output: `rustc [..]
4501+
",
4502+
)
4503+
.with_status(101)
4504+
.run();
4505+
}

0 commit comments

Comments
 (0)