vscode-elixir-ls icon indicating copy to clipboard operation
vscode-elixir-ls copied to clipboard

Formatting on save does not work with heex files.

Open tlietz opened this issue 2 years ago • 13 comments

The auto formatter does not work on heex files, even after setting up the configuration in my project correctly and adding the elixir-ls plugin as the default formatter in settings.json. If I run a mix format in the terminal, the heex files are formatted.

# .formatter.exs
[
  import_deps: [:ecto, :phoenix],
  plugins: [Phoenix.LiveView.HTMLFormatter],
  inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
  subdirectories: ["priv/*/migrations"]
]
# settings.json
 "[phoenix-heex]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "JakeBecker.elixir-ls"
    },

Is this expected? Is it something to implement on the vscode-elixir-ls level?

Environment

  • Elixir & Erlang versions (elixir --version): Erlang/OTP 24, Elixir 1.13.4
  • VSCode ElixirLS version: 0.9.0
  • Operating System Version: macOS Monterey 12.1 (M1)

tlietz avatar Apr 07 '22 23:04 tlietz

I have the same issue, error is

[Error - 7:51:17 AM] Request textDocument/formatting failed.
  Message: Unable to format due to syntax error
  Code: -32603

dvic avatar Apr 08 '22 05:04 dvic

Just confirmed: latest master version of this repo + latest master of elixir-ls suffers from same issue.

dvic avatar Apr 08 '22 06:04 dvic

Same for me

  Message: Unable to format due to syntax error
  Code: -32603 

I installed latest elixir 1.13.4 and liveview. Automatic formatting in vscode works great for ~H sigil, but fails on .heex files. I tried formatting from the command line directly the .heex files and that work ok for me.

Not sure if related, but I noticed in the elixir ls output that it says is using version 0.8.2

Started ElixirLS v0.8.2
Elixir version: "1.13.4 (compiled with Erlang/OTP 22)"
Erlang version: "24"
ElixirLS compiled with Elixir 1.10.4 and erlang 22

But there is not release with that number in the changelog as it jumps from 0.8.1 to 0.9.0 so maybe that is just the latest release

holandes22 avatar Apr 08 '22 06:04 holandes22

Same for me

  Message: Unable to format due to syntax error
  Code: -32603 

I installed latest elixir 1.13.4 and liveview. Automatic formatting in vscode works great for ~H sigil, but fails on .heex files. I tried formatting from the command line directly the .heex files and that work ok for me.

Not sure if related, but I noticed in the elixir ls output that it says is using version 0.8.2

Started ElixirLS v0.8.2
Elixir version: "1.13.4 (compiled with Erlang/OTP 22)"
Erlang version: "24"
ElixirLS compiled with Elixir 1.10.4 and erlang 22

But there is not release with that number in the changelog as it jumps from 0.8.1 to 0.9.0 so maybe that is just the latest release

I see the same version mismatch, but I'm not sure if it matters, even with the custom build (everything bumped to master), where I do see the correct version

Started ElixirLS v0.9.0
Elixir version: "1.13.3 (compiled with Erlang/OTP 24)"
Erlang version: "24"
ElixirLS compiled with Elixir 1.13.4 and erlang 24

it doesn't work.

dvic avatar Apr 08 '22 06:04 dvic

Yeah, I tried that as well (compiling from master). Error remains

holandes22 avatar Apr 08 '22 06:04 holandes22

I tried reproducing the error while running the formatter in the command line. I installed with asdf a different elixir version (1.12.0) and then I get a similar syntax error when formatting a .heex file

mix format failed for file: edit.html.heex
** (SyntaxError) edit.html.heex:1:1: syntax error before: '<'
    (elixir 1.12.0) lib/code.ex:978: Code.format_string!/2
    (mix 1.12.0) lib/mix/tasks/format.ex:418: Mix.Tasks.Format.format_file/2
    (elixir 1.12.0) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (elixir 1.12.0) lib/task/supervised.ex:35: Task.Supervised.reply/5
    (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

so maybe is related to the plugin using an older Elixir version, but what I don't get, if that is the case, how it does work on ~H sigils

holandes22 avatar Apr 08 '22 06:04 holandes22

As a workaround, you can install the runonsave extension for VSCode and configure that to run mix format: https://elixirforum.com/t/heex-formatter-a-formatter-for-heex-templates/45649/18?u=nicd

Sadly then "save without formatting" won't work. So it would be nice to get this working with the extension directly.

Nicd avatar Apr 09 '22 08:04 Nicd

I wanted to offer an alternative solution that worked for me. I installed another VSCode extension called Elixir Mix Formatter and set this to be my default formatter for elixir and heex files which seems to work as expected.

cchauche avatar May 25 '22 18:05 cchauche

make sure you have the latest version of Elixir, just run asdf install or brew uprgrade that worked for me

AmineFrioua avatar May 30 '22 15:05 AmineFrioua

https://github.com/elixir-lsp/elixir-ls/pull/690 just got merged... 🥳

dvic avatar Jun 21 '22 19:06 dvic

elixir-lsp/elixir-ls#690 just got merged... 🥳

Yeah got this working with the VS Code extension by pulling master on elixir-ls and building the extension vscode-elixir-ls (also on master) locally against Elixir 1.13.4 and Erlang 25.0.2. No Elixir Mix Formatter needed any longer; just have the one extension, vscode-elixir-ls, handling everything now.

Edit: to be clear now formats all of .ex, .exs, .heex utilizing mix format plugins in .formatter.exs (in my case Phoenix.LiveView.HTMLFormatter) via this extension built from source.

ryanjafari avatar Jun 22 '22 18:06 ryanjafari

Just in case anybody wonders how to compile the current master, try this little snippet:

(Remember to change the version to the current VSCode extension version. Currently v0.11.0)

git clone --depth 1 --branch master https://github.com/elixir-lsp/elixir-ls.git ~/elixir_ls
cd ~/elixir_ls
mix deps.get
mix elixir_ls.release -o ~/.vscode/extensions/jakebecker.elixir-ls-0.11.0/elixir-ls-release/

benvp avatar Jun 24 '22 10:06 benvp

For everyone tracking this, note elixir-ls was updated with version 0.11.0 of the extension, according to elixir-ls/CHANGELOG.md.

trkoch avatar Aug 19 '22 10:08 trkoch

Fixed in v0.11

lukaszsamson avatar Sep 27 '22 20:09 lukaszsamson