How to get color output from `mix compile` invoked via `System.cmd`?

I have a mix task that runs invokes mix test with various environment variables (MIX_ENV=xxx). The line for the invocation essentially looks like this:

System.cmd("mix", ["test", "--color"], into: IO.stream(:stdio, :line))

That gives me colorized output for the tests.

However, if any compiling takes place before running the tests, the output is not colorized. So I might get warnings but they won’t be yellow.

This can also be reproduced by replacing the invocation of mix test directly with mix compile.

How can I get the output fully colorized?

I had to solve for this at one point and I can’t readily find the reference I cited, but this appears to be working for me:

System.cmd("elixir", ["--erl", "-elixir ansi_enabled true", "-S", "mix", "test"], into: IO.stream(:stdio, :line))

In my case I was using it as part of mix cmd like so:

mix cmd --app ... elixir --erl \"-elixir ansi_enabled true\" -S mix ...
3 Likes

Saweet! That did the trick. Thanks a bunch!