Output of `Mix.shell.cmd` doesn't return any colors

I’ve got a mix alias like this:
"test.watch": [fn _ -> Mix.shell().cmd("watchexec -e ex,exs,heex 'mix test --stale'") end],

it works fine, but doesn’t show any colors. Can this be fixed?

1 Like

Have you tried using the --color flag for mix test? IIRC we try to be smart about it and we don’t use colors if we’re not sure that they’d display nicely, but sometimes we get it wront, and --color should Mix to use colors if I remember it right.

3 Likes

Yeah, it’s the --color flag.

A bit unrelated to your question, but I use this custom mix task at work in a big shared repo. This way, I can add it to my global gitignore and nobody else has to know anything about it.

defmodule Mix.Tasks.Test.Watch do
  use Mix.Task

  @preferred_cli_env :test

  def run(args) do
    Mix.shell().cmd("echo 'Mix Test Watch v1.3'")

    Mix.shell().cmd("fswatch  --latency=0.01 --one-per-batch apps/*/lib apps/*/test |
      (mix test --color #{Enum.join(args, " ")} --listen-on-stdin)")
  end
end
3 Likes

I found this thread while googling for my own problem, which is identical, but the mix task is different.

I’m trying to run dialyzer as part of a mix alias via Mix.shell().cmd("mix dialyzer") and I’m not getting any colors either. But the mix dialyzer task doesn’t accept a --color flag, so the solution from this thread doesn’t work for me.

Is there anything else I could try?

It’s Elixir 1.15.7 and OTP 24 in case that’s relevant.