Skip to content

Commit

Permalink
Auto merge of rust-lang#7550 - ehuss:fix-color, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix `cargo fix` not showing colors.

`cargo fix` runs `rustc` a final time after it has finished to:
- Show what happened if the fix failed.
- Show errors with `--broken-code`.
- Show any remaining warnings after a successful fix.

This last run was no longer showing colored diagnostics due to the stabilization of cache-messages. Cargo now unconditionally uses colored JSON messages, and then conditionally strips them after the fact.  "cargo as rustc wrapper" was stripping the JSON flags which allowed Cargo to handle colors.

This fix works by putting the json flags back in for this final pass.
  • Loading branch information
bors committed Oct 28, 2019
2 parents 8ac9868 + 91a59e6 commit a2f4906
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
// - If the fix succeeded, show any remaining warnings.
let mut cmd = Command::new(&rustc);
args.apply(&mut cmd);
for arg in args.format_args {
// Add any json/error format arguments that Cargo wants. This allows
// things like colored output to work correctly.
cmd.arg(arg);
}
exit_with(cmd.status().context("failed to spawn rustc")?);
}

Expand Down Expand Up @@ -587,6 +592,7 @@ struct FixArgs {
other: Vec<OsString>,
rustc: Option<PathBuf>,
clippy_args: Vec<String>,
format_args: Vec<String>,
}

enum PrepareFor {
Expand Down Expand Up @@ -627,6 +633,7 @@ impl FixArgs {
if s.starts_with("--error-format=") || s.starts_with("--json=") {
// Cargo may add error-format in some cases, but `cargo
// fix` wants to add its own.
ret.format_args.push(s.to_string());
continue;
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,3 +1289,22 @@ fn fix_with_clippy() {
"
);
}

#[cargo_test]
fn fix_color_message() {
// Check that color appears in diagnostics.
let p = project()
.file("src/lib.rs", "std::compile_error!{\"color test\"}")
.build();

p.cargo("fix --allow-no-vcs --color=always")
.with_stderr_contains("[..]\x1b[[..]")
.with_status(101)
.run();

p.cargo("fix --allow-no-vcs --color=never")
.with_stderr_contains("error: color test")
.with_stderr_does_not_contain("[..]\x1b[[..]")
.with_status(101)
.run();
}

0 comments on commit a2f4906

Please sign in to comment.