elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Mix breaks character encoding in Powershell on Windows Terminal

Open Ilyushya opened this issue 2 years ago • 27 comments

Elixir and Erlang/OTP versions

Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Elixir 1.16.0 (compiled with Erlang/OTP 26)

Operating system

Windows 10

Current behavior

I'm building an in-game chat filtering app and I experience odd behaviour in my program. When I use IO.gets() in iex, everything works properly, but when I launch my code by mix run and accept messages there, it looks broken.

PS C:\Users\Ilyushya\Documents\pfilter> mix
Compiling 1 file (.ex)
Generated pfilter app
>>gh
--->gh

>>привет
--->�ਢ��

>>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
a
Terminate batch job (Y/N)? Y

mix.exs

defmodule Pfilter.MixProject do
  use Mix.Project

  def project do
    [
      app: :pfilter,
      version: "0.1.0",
      elixir: "~> 1.16",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger],
      mod: {Pfilter, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

My app code

defmodule Pfilter do
  use Application

  def accept_messages do
    msg = IO.gets(">>")
    IO.puts("--->" <> msg)
    accept_messages()
  end

  @impl true
  def start(_start_type, _start_args) do
    accept_messages()
  end

end

I noticed this some time ago, but thought it was a project issue. However, it is a common abnormality.

Expected behavior

I expect receiving normal message content (until I filter it 😈)

# starting manually
PS C:\Users\Ilyushya\Documents\pfilter> iex.bat -S mix
Compiling 1 file (.ex)
Generated pfilter app
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.16.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Pfilter.accept_messages
>>ge
--->ge

>>привет
--->привет

>>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
a
Terminate batch job (Y/N)? Y

Ilyushya avatar Jan 30 '24 13:01 Ilyushya

What does this code return when you execute with and without iex?

  @impl true
  def start(_start_type, _start_args) do
    IO.inspect :io.getopts()
    accept_messages()
  end

josevalim avatar Jan 30 '24 13:01 josevalim

Also, can you reproduce the bug by storing this in a file name pfilter.ex:

defmodule Pfilter do
  def run do
    msg = IO.gets(">>")
    IO.puts("--->" <> msg)
    accept_messages()
  end
end

Pfilter.run

And then doing elixir pfilter.ex?

josevalim avatar Jan 30 '24 13:01 josevalim

What does this code return when you execute with and without iex?

  @impl true
  def start(_start_type, _start_args) do
    IO.inspect :io.getopts()
    accept_messages()
  end
mix
Generated pfilter app
[
  expand_fun: &:edlin_expand.expand/2,
  echo: false,
  binary: true,
  encoding: :unicode,
  terminal: true
]
>>hi
--->hi

>>привет
--->�ਢ��

>>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
a
Terminate batch job (Y/N)? Y

Ilyushya avatar Jan 30 '24 13:01 Ilyushya

@Intelliractive do not place the file I suggested above in lib. Instead, put it outside of lib and run it without mix. Run it with elixir pfilter.ex.

josevalim avatar Jan 30 '24 13:01 josevalim

Unfortunately, it doesn't work either.

elixir .\pfilter.ex
>>как дела?
--->��� ����?

>>how are things?
--->how are things?

>>

Ilyushya avatar Jan 30 '24 13:01 Ilyushya

@Intelliractive can you save the code below to a file named foo.erl:

-module(foo).
-compile(export_all).

start() ->
  io:setopts([{binary, true}]),
  io:format("~p~n", [io:getopts()]),
  loop().

loop() ->
  Res = io:get_line(">> "),
  io:put_chars(["---> ", Res, $\n]),
  loop().

And then:

$ erlc foo.erl
$ erl -noshell -s foo

Now type the same characters. Does it reproduce the issue?

josevalim avatar Jan 30 '24 18:01 josevalim

This does reproduce the issue. It turned out it was an Erlang issue again. Thank you

Ilyushya avatar Jan 31 '24 02:01 Ilyushya

@Intelliractive do you want to report it upstream or should I? :)

josevalim avatar Jan 31 '24 08:01 josevalim

Good morning mr José! I have discovered that only Powershell behaves in that odd manner.

cmd

C:\Users\Ilyushya\Documents\yerkyn>dotnet build
MSBuild version 17.5.1+f6fdcf537 for .NET
  Определение проектов для восстановления...
  Все проекты обновлены для восстановления.
  yerkyn -> C:\Users\Ilyushya\Documents\yerkyn\bin\Debug\net7.0\yerkyn.dll

Сборка успешно завершена.
    Предупреждений: 0
    Ошибок: 0

Прошло времени 00:00:01.66

powershell

PS C:\Users\Ilyushya\Documents\yerkyn> dotnet build
MSBuild version 17.5.1+f6fdcf537 for .NET
  Определение проектов для восстановления...
  Все проекты обновлены для восстановления.
  yerkyn -> C:\Users\Ilyushya\Documents\yerkyn\bin\Debug\net7.0\yerkyn.dll

���ઠ �ᯥ譮 �����襭�.
    �।�०�����: 0
    �訡��: 0

��諮 �६��� 00:00:05.45

Ilyushya avatar Jan 31 '24 10:01 Ilyushya

@Intelliractive do you want to report it upstream or should I? :)

There are already many issues open on topic of odd console behaviour, I guess😅

Ilyushya avatar Jan 31 '24 10:01 Ilyushya

@Intelliractive can you please try this version?

-module(foo).
-compile(export_all).

start() ->
  io:setopts([{binary, true}]),
  io:format("~p~n", [io:getopts()]),
  loop().

loop() ->
  Res = io:get_line(">> "),
  erlang:display(<<0, Res/binary>>),
  io:put_chars(["---> ", Res, $\n]),
  loop().

I just realized we are not sure if it is printing wrong or reading it wrong.

josevalim avatar Jan 31 '24 11:01 josevalim

Remember that if you read from stdio in binary since OTP 26 you need to pass -kernel standard_io_encoding latin1, otherwise the encoding defaults to utf8

lukaszsamson avatar Jan 31 '24 12:01 lukaszsamson

@Intelliractive ping :)

josevalim avatar Feb 09 '24 07:02 josevalim

... pong? xD I've done some more research and figured out that of cmd and powershell stand-alone and in Windows Terminal (four in total), only cmd on WT behaves in an adequate way. I guess this is more an Erlang issue, should we close it here?

Ilyushya avatar Feb 09 '24 14:02 Ilyushya

@Intelliractive can you try this version of the Erlang program? https://github.com/elixir-lang/elixir/issues/13301#issuecomment-1918885813

Also, can you try this on the version of the installer from this PR? https://github.com/erlang/otp/pull/8103 It is a recent Erlang version, with some debugging, which will tell us if the bug has been fixed already or not. :)

josevalim avatar Feb 09 '24 16:02 josevalim

Hello @josevalim ! The new program works fine! Even in the VS Code terminal!

3> foo:start().
[{expand_fun,fun edlin_expand:expand/2},
 {echo,true},
 {binary,true},
 {encoding,unicode},
 {terminal,true}]
>> hello!<<0,104,101,108,108,111,33,10>>
>> hello!
---> hello!

>> привет!<<0,208,191,209,128,208,184,208,178,208,181,209,130,33,10>>
>> привет!
---> привет!

>> How are things?😀<<0,72,111,119,32,97,114,101,32,116,104,105,110,103,115,63,240,159,152,128,10>>
>> How are things?😀
---> How are things?😀

>> Как дела?😀<<0,208,154,208,176,208,186,32,208,180,208,181,208,187,208,176,63,240,159,152,128,10>>
>> Как дела?😀
---> Как дела?😀

>>

Ilyushya avatar Feb 11 '24 05:02 Ilyushya

It works properly on that new Erlang (27 release candidate) as well

foo:start().
[{expand_fun,fun edlin_expand:expand/2},
 {echo,true},
 {binary,true},
 {encoding,unicode},
 {terminal,true}]
>> привет
>> привет
---> привет

>> 😎Всё в порядке!
<<0,240,159,152,142,208,146,209,129,209,145,32,208,178,32,208,191,208,190,209,128,209,143,208,180,208,186,208,181,33,10>>> 😎Всё в порядке!
---> 😎Всё в порядке!

>> 😎Everything works fine!
>> 😎Everything works fine!
---> 😎Everything works fine!

>>

Ilyushya avatar Feb 11 '24 05:02 Ilyushya

And does the earlier program work on Erlang 27?

josevalim avatar Feb 11 '24 10:02 josevalim

It works properly on that new Erlang (27 release candidate) as well

foo:start().
[{expand_fun,fun edlin_expand:expand/2},
 {echo,true},
 {binary,true},
 {encoding,unicode},
 {terminal,true}]
>> привет
>> привет
---> привет

>> 😎Всё в порядке!
<<0,240,159,152,142,208,146,209,129,209,145,32,208,178,32,208,191,208,190,209,128,209,143,208,180,208,186,208,181,33,10>>> 😎Всё в порядке!
---> 😎Всё в порядке!

>> 😎Everything works fine!
>> 😎Everything works fine!
---> 😎Everything works fine!

>>

Here I demonstrate it running on the release you offered me to test, which is Erlang 27 in-development

Ilyushya avatar Feb 11 '24 10:02 Ilyushya

Sorry, to be clear, but that's the last program I shared, right? Does the first program I shared also works?

josevalim avatar Feb 11 '24 10:02 josevalim