Skip to content

Commit a2f4906

Browse files
committed
Auto merge of #7550 - ehuss:fix-color, r=alexcrichton
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.
2 parents 8ac9868 + 91a59e6 commit a2f4906

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/cargo/ops/fix.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
281281
// - If the fix succeeded, show any remaining warnings.
282282
let mut cmd = Command::new(&rustc);
283283
args.apply(&mut cmd);
284+
for arg in args.format_args {
285+
// Add any json/error format arguments that Cargo wants. This allows
286+
// things like colored output to work correctly.
287+
cmd.arg(arg);
288+
}
284289
exit_with(cmd.status().context("failed to spawn rustc")?);
285290
}
286291

@@ -587,6 +592,7 @@ struct FixArgs {
587592
other: Vec<OsString>,
588593
rustc: Option<PathBuf>,
589594
clippy_args: Vec<String>,
595+
format_args: Vec<String>,
590596
}
591597

592598
enum PrepareFor {
@@ -627,6 +633,7 @@ impl FixArgs {
627633
if s.starts_with("--error-format=") || s.starts_with("--json=") {
628634
// Cargo may add error-format in some cases, but `cargo
629635
// fix` wants to add its own.
636+
ret.format_args.push(s.to_string());
630637
continue;
631638
}
632639
}

tests/testsuite/fix.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,3 +1289,22 @@ fn fix_with_clippy() {
12891289
"
12901290
);
12911291
}
1292+
1293+
#[cargo_test]
1294+
fn fix_color_message() {
1295+
// Check that color appears in diagnostics.
1296+
let p = project()
1297+
.file("src/lib.rs", "std::compile_error!{\"color test\"}")
1298+
.build();
1299+
1300+
p.cargo("fix --allow-no-vcs --color=always")
1301+
.with_stderr_contains("[..]\x1b[[..]")
1302+
.with_status(101)
1303+
.run();
1304+
1305+
p.cargo("fix --allow-no-vcs --color=never")
1306+
.with_stderr_contains("error: color test")
1307+
.with_stderr_does_not_contain("[..]\x1b[[..]")
1308+
.with_status(101)
1309+
.run();
1310+
}

0 commit comments

Comments
 (0)