elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.3k stars 3.35k forks source link

Formatter doesn't work under windows #7625

Closed fklement closed 6 years ago

fklement commented 6 years ago

Environment

Current behavior

In our project the .formatter.exs looks like this:

[
  inputs: [
    "lib/**/*.{ex,exs}",
    "mix.exs",
    "test/support/**/*.{ex,exs}"
  ],
  locals_without_parens: [
    # Kernel
    inspect: 1,
    inspect: 2,

    # Phoenix
    plug: 1,
    plug: 2,
    action_fallback: 1,
    render: 2,
    render: 3,
    render: 4,
    redirect: 2,
    resources: 2,
    resources: 3,
    resources: 4,
    get: 4,
    post: 4,
    delete: 4,
    forward: 4,
    get: 3,
    post: 3,
    delete: 3,
    forward: 3,
    get: 2,
    post: 2,
    delete: 2,
    forward: 2,

    # Ecto Schema
    field: 2,
    field: 3,
    belongs_to: 2,
    belongs_to: 3,
    has_one: 2,
    has_one: 3,
    has_many: 2,
    has_many: 3,
    embeds_one: 2,
    embeds_one: 3,
    embeds_many: 2,
    embeds_many: 3,
    many_to_many: 2,
    many_to_many: 3,
    add: 3,

    # Ecto Query
    from: 2
  ]
]

The problem is, that the list get's correctly returned but it does not affect the formatted code when i hit save.

Expected behavior

For example: embeds_one :rates, Type.Rates becomes embeds_one(:rates, Types.Rates) which should not be.

My colleague is using MacOS and here works everyting fine.

josevalim commented 6 years ago

Does it work if you run “mix format” from your project root? I.e. from the same directory where .formatter.exs is placed? --

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D

fklement commented 6 years ago

I tried. But nothing happens at all.

josevalim commented 6 years ago

It doesn’t happen because the code is already formatted? If you add “foo 1, 2” to a file that is listed, does it add parens? --

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D

josevalim commented 6 years ago

If that still does not work, can you please provide an application that reproduces the issue? We know for certain that it works on windows so we need more information to effectively reproduce this. Thank you! --

José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D

josevalim commented 6 years ago

Ping!

josevalim commented 6 years ago

I will go ahead and close this for now. We will gladly reopen once we have a mechanism to reproduce the issue.

princemaple commented 6 years ago

I have some problem that might be related

~\my_proj [master ↑3 +8 ~8 -0 !] [Aug-24 16:58:04]
> mix format .\lib\app\flow\update_stock_check.ex
** (Mix) Could not find a file to format. The files/patterns given to command line did not point to any existing file. Got: [".\\lib\\app\\flow\\update_stock_check.ex"]
~\my_proj [master ↑3 +8 ~8 -0 !] [Aug-24 16:58:29]
> mix format ./lib/app/flow/update_stock_check.ex
~\my_proj [master ↑3 +8 ~8 -0 !] [Aug-24 16:58:50]
> elixir --version
Erlang/OTP 21 [erts-10.0.1] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Elixir 1.7.1 (compiled with Erlang/OTP 19)

Notice how the windows \ separated path does not work, even though File.exists ".\\lib\\app\\flow\\update_stock_check.ex" returns true After changing them to / it worked.

princemaple commented 6 years ago

Looks like it's caused by Path.wildcard returning empty list for the windows path

iex(2)> Path.wildcard(".\\lib\\app\\flow\\update_stock_check.ex")
[]

https://github.com/elixir-lang/elixir/blob/master/lib/mix/lib/mix/tasks/format.ex#L334

josevalim commented 6 years ago

@princemaple good find. What is your OTP version? It may be worth checking if OTP 20 and OTP 21 behave the same. Also, are you running on the Windows terminal? Or cygwin or the Ubuntu thingy?

princemaple commented 6 years ago

Thanks for the quick reply, @josevalim . I'm on PowerShell & OTP21, it's installed via Chocolatey. I don't have an easy way to install an OTP20 version... I normally dev in docker so it's not a problem for me. I found out about this because I use a build system in Sublime to call formatter, which calls the Elixir on Windows, not the Elixir in docker.

princemaple commented 6 years ago

https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/path.ex#L650 @josevalim Looks like this is documented:

Directory separators must always be written as /, even on Windows.

at http://erlang.org/doc/man/filelib.html

princemaple commented 6 years ago

@josevalim I guess Path.wildcard behaves as expected, according to the above. And mix format probably needs to do some unification before passing paths to Path.wildcard..?