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 does not respect `.formatter.exs` when piping code into it #7573

Closed bitboxer closed 6 years ago

bitboxer commented 6 years ago

Environment

Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [kernel-poll:false]

Elixir 1.6.3 (compiled with OTP 19)
Ubuntu 16.04.4 LTS

Current behavior

When piping code into the formatter like this:

cat path/to/file.ex | mix format -

it does not respect the local .formatter.exs. It also ignores the --dot-formatter setting.

Am I doing something wrong? Is this a bug?

Expected behavior

It should format the code using the local .formatter.exs or at least the one I specify in the params.

NobbZ commented 6 years ago

Ignoring the default .formatter.exs is by design, as a piped in stream of characters is considered to be unrelated to the current (or any) project.

In my opinion it should respect an explicitely passed in config file via --dot-formatter though.

josevalim commented 6 years ago

This is fixed on v1.6.4. --

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

bitboxer commented 6 years ago

Thanks for the quick reply 👍

bitboxer commented 6 years ago

Just found time to test this in my local elixir 1.6.4

Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [kernel-poll:false]

Elixir 1.6.4 (compiled with OTP 19)

Sadly I can't confirm that this works.

I did this:

cat /path/to/file.exs |  mix format --dot-formatter=/path/to/.formatter.exs -

And it did non respect the config I gave to it. Using

mix format --dot-formatter=/path/to/.formatter.exs /path/to/file.exs

it worked.

josevalim commented 6 years ago

@bitboxer it worked just fine here. Here is what I did:

$ cat foo.ex | mix format --dot-formatter=path/to/elixir/repo/.formatter.exs -
assert_same bar

Notice that it did not add parens around assert_same since it is listed in .formatter.exs.

bitboxer commented 6 years ago

Maybe the issue is that my .formatter.exs looks like this:

[
  inputs: ["mix.exs", "config/*.exs"],
  subdirectories: ["apps/*"]
]

and in the subdirectories I have some additional formatter files. Will try it out with moving the stuff into the main config file tonight.

josevalim commented 6 years ago

The input file - will never traverse the subdirectories since it doesn't have any path in it. It always consider the root one. And since you don't have anything at the root then it will only o the default formatting.

bitboxer commented 6 years ago

Other languages have a parameter for the formatter to set the root path if you pipe a file into the formatter. But it's understandable if you think this might be too much overhead for elixir. I can work around that with saving the file in a specific directory and deleting it afterwards.