Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting on save does not work with heex files. #242

Closed
tlietz opened this issue Apr 7, 2022 · 14 comments
Closed

Formatting on save does not work with heex files. #242

tlietz opened this issue Apr 7, 2022 · 14 comments

Comments

@tlietz
Copy link

tlietz commented Apr 7, 2022

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)
@dvic
Copy link

dvic commented Apr 8, 2022

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
Copy link

dvic commented Apr 8, 2022

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

@holandes22
Copy link

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

@dvic
Copy link

dvic commented Apr 8, 2022

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.

@holandes22
Copy link

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

@holandes22
Copy link

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

@Nicd
Copy link

Nicd commented Apr 9, 2022

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.

@cchauche
Copy link

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.

@AmineFrioua
Copy link

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

@dvic
Copy link

dvic commented Jun 21, 2022

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

@ryanjafari
Copy link

ryanjafari commented Jun 22, 2022

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.

@benvp
Copy link

benvp commented Jun 24, 2022

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/

@trkoch
Copy link

trkoch commented Aug 19, 2022

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

@lukaszsamson
Copy link
Collaborator

Fixed in v0.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants