Skip to content

Commit 2ef5a58

Browse files
committed
util/process_wrapper: improve resilience to non-JSON output
1 parent 175bf94 commit 2ef5a58

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

util/process_wrapper/main.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,37 @@ fn json_warning(line: &str) -> JsonValue {
9292
}
9393

9494
fn process_line(
95-
mut line: String,
95+
line: String,
9696
quit_on_rmeta: bool,
9797
format: ErrorFormat,
9898
metadata_emitted: &mut bool,
9999
) -> Result<LineOutput, String> {
100-
// LLVM can emit lines that look like the following, and these will be interspersed
101-
// with the regular JSON output. Arguably, rustc should be fixed not to emit lines
102-
// like these (or to convert them to JSON), but for now we convert them to JSON
103-
// ourselves.
104-
if line.contains("is not a recognized feature for this target (ignoring feature)")
105-
|| line.starts_with(" WARN ")
106-
{
107-
if let Ok(json_str) = json_warning(&line).stringify() {
108-
line = json_str;
100+
let mut process = |l: String| {
101+
if quit_on_rmeta {
102+
rustc::stop_on_rmeta_completion(l, format, metadata_emitted)
109103
} else {
110-
return Ok(LineOutput::Skip);
104+
rustc::process_json(l, format)
111105
}
112-
}
113-
if quit_on_rmeta {
114-
rustc::stop_on_rmeta_completion(line, format, metadata_emitted)
106+
};
107+
108+
let is_likely_json = line.trim_start().starts_with('{');
109+
110+
let result = if is_likely_json {
111+
process(line.clone())
115112
} else {
116-
rustc::process_json(line, format)
113+
Err("not likely json".to_string())
114+
};
115+
116+
match result {
117+
Ok(output) => Ok(output),
118+
Err(_) => {
119+
if let Ok(mut json_str) = json_warning(&line).stringify() {
120+
json_str.push('\n');
121+
process(json_str)
122+
} else {
123+
Ok(LineOutput::Skip)
124+
}
125+
}
117126
}
118127
}
119128

@@ -299,6 +308,8 @@ mod test {
299308
for text in [
300309
"'+zaamo' is not a recognized feature for this target (ignoring feature)",
301310
" WARN rustc_errors::emitter Invalid span...",
311+
"this is random noise",
312+
"{ this looks like json but is not }",
302313
] {
303314
let LineOutput::Message(msg) = process_line(
304315
text.to_string(),
@@ -371,4 +382,26 @@ mod test {
371382
assert!(metadata_emitted);
372383
Ok(())
373384
}
385+
386+
#[test]
387+
fn test_process_line_noise_rendered() -> Result<(), String> {
388+
let mut metadata_emitted = false;
389+
for text in [
390+
"this is random noise",
391+
"{ this looks like json but is not }",
392+
r#"noise with "quotes" and \backslashes"#,
393+
] {
394+
let LineOutput::Message(msg) = process_line(
395+
text.to_string(),
396+
/*quit_on_rmeta=*/ false,
397+
ErrorFormat::Rendered,
398+
&mut metadata_emitted,
399+
)?
400+
else {
401+
return Err("Expected a LineOutput::Message".to_string());
402+
};
403+
assert_eq!(msg, text);
404+
}
405+
Ok(())
406+
}
374407
}

0 commit comments

Comments
 (0)