Skip to content

Mix cmd output has wrong encoding on Linux (since #14027) #14101

@mediremi

Description

@mediremi

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]

Elixir 1.18.0 (compiled with Erlang/OTP 27)

Operating system

Linux

Current behavior

Using mix cmd to run programs that print out non-ASCII characters results in incorrectly encoded output. This behaviour is since #14027.

To reproduce, run mix cmd echo ✔. The output will be â instead of .

Expected behavior

This bug is due to Mix.Shell.IO.cmd/2 using IO.binwrite instead of IO.write even on non-Windows systems.

iex(1)> Mix.Shell.cmd("echo ✔", [], &IO.write/1)
✔
0
iex(2)> Mix.Shell.cmd("echo ✔", [], &IO.binwrite/1)
â
0

I think the following change should fix this (if this looks good happy to make a PR):

def cmd(command, opts \\ []) do
  print_app? = Keyword.get(opts, :print_app, true)

  Mix.Shell.cmd(command, opts, fn data ->
    if print_app?, do: print_app()
    case :os.type() do
      {:win32, _} ->
        # Due to encoding of shell command on Windows,
        # let's write the data as is
        IO.binwrite(data)
      
      _ -> IO.write(data)
    end
  end)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions