Elixir formatter in neovim (using coc.nvim) sometimes adds parenthesis around macros

Ah, OK. I think you’ve mentioned that before, actually.

I’ve considered making the jump over to LunarVim just to get a fresh start… I’ve got an old Vimscript config that I migrated over to NeoVim, but it’s starting to get kinda janky with a bunch of Lua mixed in.

1 Like

I entertained the idea to move to VIM several times in the last 10-ish years but the script was a turnoff. When nvim with Lua got out it was then I started actually doing it.

1 Like

Alright, sorry for the noise - as a newcomer I wasn’t aware there’s a dedicated mix new --umbrella (yay!) to set everything up properly. Coming from JS world where I had to set up monorepos manually, but here it’s turn-key. Now formatting seems to work fine, without any custom NeoVim or LS configuration.

(I should note that I still get the error from above if I try to save an Elixir file without giving ElixirLS a few seconds to start up, if I close and reopen Neovim. But that’s probably expected)

Thanks again for all your help!

2 Likes

And now I have the opposite problem: I want the formatter to automatically add parentheses to all function calls in the priv/repo/migrations/ directory but after trying for 5 minutes I gave up.

Got annoyed and decided to pursue. I really want parentheses in my ecto and ecto_sql code.

TL;DR: I had this file in my project: priv/repo/migrations/.formatter.exs. I also have the .formatter.exs in the root of the project. I removed the first file and removed :ecto and :ecto_sql from import_deps in my root .formatter.exs file, then did rm -rf _build && mix do deps.get, compile and et voila, both mix format and ElixirLS started adding parentheses in my migrations code.


Long version:

priv/repo/migrations/.formatter.exs had these contents:

[
   import_deps: [:ecto_sql],
   inputs: ["*.exs"]
]

.formatter.exs (at the root of the project) had these contents:

[
  import_deps: [:ecto, :ecto_sql, :phoenix],
  subdirectories: ["priv/*/migrations"],
  plugins: [Phoenix.LiveView.HTMLFormatter],
  inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/**/*.exs"]
]

When I tried desperately to manually mix format my migrations to put parentheses in (I dislike the space syntax and prefer the parentheses :person_shrugging:), I got this error (I have replaced the absolute path on my storage with $STORAGE_PATH):

Both .formatter.exs and $PROJECT_PATH/priv/repo/migrations/.formatter.exs specify the file $PROJECT/priv/repo/migrations/20240229041210_add_oban_jobs_table.exs in their :inputs option. To resolve the conflict, the configuration in .formatter.exs will be ignored. Please change the list of :inputs in one of the formatter files so only one of them matches $PROJECT_PATH/priv/repo/migrations/20240229041210_add_oban_jobs_table.exs

So I:

  1. Removed priv/repo/migrations/.formatter.exs;
  2. Edited the root .formatter.exs file like so (removed ecto and ecto_sql from import_deps):
[
-  import_deps: [:ecto, :ecto_sql, :phoenix],
+  import_deps: [:phoenix],
  subdirectories: ["priv/*/migrations"],
  plugins: [Phoenix.LiveView.HTMLFormatter],
  inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/**/*.exs"]
]
  1. Removed the _build directory (rm -rf _build) and then ran mix do deps.get, compile. Note that without this step formatting still didn’t work like I wanted it to.

mix format worked immediately after – as in, it added parentheses where there were none.

ElixirLS worked immediately after as well.

Mystery solved. No idea what possessed me to make this secondary .formatter.exs file inside the priv/repo/migrations/ directory.

1 Like

No idea what possessed me to make this secondary .formatter.exs file inside the priv/repo/migrations/ directory.

The generator puts it there when creating the project:

1 Like

Thanks for this write-up, @dimitarvp. I had a very similar issue, for which rm priv/repo/migrations/.formatter.exs was the solution.

FWIW, my error was:

[ERROR] Formatter 'mix' error: ** (Mix) Unknown dependency :ecto_sql given to :import_deps in the formatter configuration. Make sure the dependency is listed in your mix.exs for environment :dev and you have run "mix deps.get"
1 Like

One of my steps was removing that file as well.

Welcome to the forum. :058:

1 Like