Skip to content

Commit 8f032b3

Browse files
committed
Handle "invalid JSON" from the compiler
Just opportunistically consider lines which start with `{` as valid JSON, otherwise forward everything else to normal stdout/stderr.
1 parent 95e2404 commit 8f032b3

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,17 @@ fn on_stderr_line(
10991099
return Ok(());
11001100
}
11011101

1102-
let compiler_message: Box<serde_json::value::RawValue> = serde_json::from_str(line)
1103-
.map_err(|_| internal(&format!("compiler produced invalid json: `{}`", line)))?;
1102+
let compiler_message: Box<serde_json::value::RawValue> = match serde_json::from_str(line) {
1103+
Ok(msg) => msg,
1104+
1105+
// If the compiler produced a line that started with `{` but it wasn't
1106+
// valid JSON, maybe it wasn't JSON in the first place! Forward it along
1107+
// to stderr.
1108+
Err(_) => {
1109+
state.stderr(line);
1110+
return Ok(());
1111+
}
1112+
};
11041113

11051114
// In some modes of compilation Cargo switches the compiler to JSON mode
11061115
// but the user didn't request that so we still want to print pretty rustc

tests/testsuite/build.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,3 +4632,61 @@ fn pipelining_works() {
46324632
")
46334633
.run();
46344634
}
4635+
4636+
#[test]
4637+
fn forward_rustc_output() {
4638+
let foo = project()
4639+
.file(
4640+
"Cargo.toml",
4641+
r#"
4642+
[package]
4643+
name = "foo"
4644+
version = "0.1.0"
4645+
edition = '2018'
4646+
[dependencies]
4647+
bar = { path = "bar" }
4648+
"#,
4649+
)
4650+
.file("src/lib.rs", "bar::foo!();")
4651+
.file(
4652+
"bar/Cargo.toml",
4653+
r#"
4654+
[package]
4655+
name = "bar"
4656+
version = "0.1.0"
4657+
[lib]
4658+
proc-macro = true
4659+
"#,
4660+
)
4661+
.file(
4662+
"bar/src/lib.rs",
4663+
r#"
4664+
extern crate proc_macro;
4665+
use proc_macro::*;
4666+
4667+
#[proc_macro]
4668+
pub fn foo(input: TokenStream) -> TokenStream {
4669+
println!("a");
4670+
println!("b");
4671+
println!("{{}}");
4672+
eprintln!("c");
4673+
eprintln!("d");
4674+
eprintln!("{{a"); // "malformed json"
4675+
input
4676+
}
4677+
"#,
4678+
)
4679+
.build();
4680+
4681+
foo.cargo("build")
4682+
.with_stdout("a\nb\n{}")
4683+
.with_stderr("\
4684+
[COMPILING] [..]
4685+
[COMPILING] [..]
4686+
c
4687+
d
4688+
{a
4689+
[FINISHED] [..]
4690+
")
4691+
.run();
4692+
}

0 commit comments

Comments
 (0)