From 91a59e60893b34325c751e28c1b81da63265c09a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 28 Oct 2019 12:00:09 -0700 Subject: [PATCH] Fix `cargo fix` not showing colors. --- src/cargo/ops/fix.rs | 7 +++++++ tests/testsuite/fix.rs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 415229eca6d..0daaf096d34 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -281,6 +281,11 @@ pub fn fix_maybe_exec_rustc() -> CargoResult { // - 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")?); } @@ -587,6 +592,7 @@ struct FixArgs { other: Vec, rustc: Option, clippy_args: Vec, + format_args: Vec, } enum PrepareFor { @@ -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; } } diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 04399bd4d61..1c31865c10b 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -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(); +}