Skip to content

Commit

Permalink
Rollup merge of #59128 - oli-obk:colorful_json, r=mark-i-m,eddyb
Browse files Browse the repository at this point in the history
Emit ansi color codes in the `rendered` field of json diagnostics

cc @ljedrz

Implemented for rust-lang/rust#56595 (comment) (x.py clippy)
  • Loading branch information
Centril authored Apr 17, 2019
2 parents a4b8cc1 + 17719dc commit ec3a4e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 45 deletions.
7 changes: 4 additions & 3 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,20 @@ struct DiagnosticCode {
explanation: Option<String>,
}

pub fn extract_rendered(output: &str, proc_res: &ProcRes) -> String {
pub fn extract_rendered(output: &str) -> String {
output
.lines()
.filter_map(|line| {
if line.starts_with('{') {
match serde_json::from_str::<Diagnostic>(line) {
Ok(diagnostic) => diagnostic.rendered,
Err(error) => {
proc_res.fatal(Some(&format!(
print!(
"failed to decode compiler output as json: \
`{}`\nline: {}\noutput: {}",
error, line, output
)));
);
panic!()
}
}
} else {
Expand Down
46 changes: 4 additions & 42 deletions src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,50 +2090,10 @@ impl<'test> TestCx<'test> {
}

fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
self.try_print_open_handles();
self.error(err);
proc_res.fatal(None);
}

// This function is a poor man's attempt to debug rust-lang/rust#38620, if
// that's closed then this should be deleted
//
// This is a very "opportunistic" debugging attempt, so we ignore all
// errors here.
fn try_print_open_handles(&self) {
if !cfg!(windows) {
return;
}
if self.config.mode != Incremental {
return;
}

let filename = match self.testpaths.file.file_stem() {
Some(path) => path,
None => return,
};

let mut cmd = Command::new("handle.exe");
cmd.arg("-a").arg("-u");
cmd.arg(filename);
cmd.arg("-nobanner");
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
let output = match cmd.spawn().and_then(read2_abbreviated) {
Ok(output) => output,
Err(_) => return,
};
println!("---------------------------------------------------");
println!("ran extra command to debug rust-lang/rust#38620: ");
println!("{:?}", cmd);
println!("result: {}", output.status);
println!("--- stdout ----------------------------------------");
println!("{}", String::from_utf8_lossy(&output.stdout));
println!("--- stderr ----------------------------------------");
println!("{}", String::from_utf8_lossy(&output.stderr));
println!("---------------------------------------------------");
}

// codegen tests (using FileCheck)

fn compile_test_and_save_ir(&self) -> ProcRes {
Expand Down Expand Up @@ -2844,7 +2804,7 @@ impl<'test> TestCx<'test> {
let stderr = if explicit {
proc_res.stderr.clone()
} else {
json::extract_rendered(&proc_res.stderr, &proc_res)
json::extract_rendered(&proc_res.stderr)
};

let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
Expand Down Expand Up @@ -3464,7 +3424,9 @@ impl ProcRes {
{}\n\
------------------------------------------\n\
\n",
self.status, self.cmdline, self.stdout, self.stderr
self.status, self.cmdline,
json::extract_rendered(&self.stdout),
json::extract_rendered(&self.stderr),
);
// Use resume_unwind instead of panic!() to prevent a panic message + backtrace from
// compiletest, which is unnecessary noise.
Expand Down

0 comments on commit ec3a4e4

Please sign in to comment.