how to configure VSCode to autoformat on save?

Edit: I solved this.

This post is how I solved this problem. It requires installing the extension “run on save” and setting it to run mix format.

I still have ElixirLS installed, the above solution doesn’t interfere with it.

My original post is below.

I’d like to format my code on save (using mix format) in VSCode, I saw that ElixirLS should have this functionality so I set it to do so by putting this in my settings.json:

"[elixir]": {
    "editor.defaultFormatter": "JakeBecker.elixir-ls"
    "editor.formatOnSave": true,

However, when I open another file in a project, it says the file “is not included in .formatter.exs” (it’s ElixirLS that shows that message) despite the fact that it has an exs file extension. For some other files format on save works, such as if I open the project_test.exs file change the formating and save it, it gets autoformated correctly.

The .formatter.exs reads:

# Used by "mix format"
  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]

Which should catch this file. So I’m at a loss why the file is not being caught. For me it’s important to autoformat my files on save, for consistent formating.

Any ideas? I am on Windows 10.

Steps to reproduce:

1. Install ElixirLS and set it to the default formater for Elixir files as per above.
2. mix new project
3. In the project directory, add an exs file
4. Open it in vscode.
5. When saved it shows this error.

Any ideas?

1 Like

Elixir LS is supposed to respect the .formatter.exs rules by now. But it does not always work for me either.

Since I run my tests all the time I just added mix format in front of my test command :smiley: