elixir-lsp / elixir-ls

A frontend-independent IDE "smartness" server for Elixir. Implements the "Language Server Protocol" standard and provides debugger support via the "Debug Adapter Protocol"
https://elixir-lsp.github.io/elixir-ls/
Apache License 2.0
1.5k stars 196 forks source link

v0.22.1 can not logger format liveview projects #1110

Open NoBrainSkull opened 4 months ago

NoBrainSkull commented 4 months ago

Environment

Elixir 1.17.2 (compiled with Erlang/OTP 27)


and

Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [jit:ns]

Elixir 1.16.3 (compiled with Erlang/OTP 26)


* Elixir Language Server version: v0.22.1
* Operating system: Archlinux
* Editor or IDE name (e.g. Emacs/VSCode):  Neovim
* Editor Plugin/LSP Client name and version: nvim-lsp

### Current behavior

Calling the LSP format function will fail with the following : 

(Mix.Error) Formatter plugin Phoenix.LiveView.HTMLFormatter cannot be found



[For some reason, the `&Code.ensure_loaded?/1` call in the `&load_plugins` function returns false for the `LiveView.HTMLFormatter` in this version
](https://github.com/elixir-lsp/elixir-ls/blob/master/apps/language_server/lib/language_server/mix_tasks/format.ex#L301)

### Expected behavior

Reverting `elixir-ls` to v0.22.0 fixes the problem entirely, with no observed impact from Elixir/OTP version.
lukaszsamson commented 4 months ago

I cannot reproduce that. Formatting a live_view project works on my machine. Note that there were no changes that could affect formatting between v0.22.0 and v0.22.1. Do you have a project that reproduces it reliably?

NoBrainSkull commented 4 months ago

Sure, please find the link below.

I must add that for 1 second I thought that reverting to v0.22.0 and upgrading again to v0.22.1 solved the issue but removing .elixir_ls, deps/ and build made it appear again (after reinstalling, deps and compiling).

Here is the full output I'm having :

LSP[elixirls] Unable to find formatter for /home/no_brain_skull/src/ex17ls/.formatter.exs: "** 
        (Mix.Error) Formatter plugin Phoenix.LiveView.HTMLFormatter cannot be found
        (mix 1.15.0) lib/mix.ex:577: Mix.raise/2
    (language_server 0.22.1) lib/language_server/mix_tasks/format.ex:302: anonymous fn/2 in Mix.Tasks.ElixirLSFormat.load_plugins/1
    (elixir 1.15.0) lib/enum.ex:2510: Enum.\"-reduce/3-lists^foldl/2-0-\"/3
    (language_server 0.22.1) lib/language_server/mix_tasks/format.ex:299: Mix.Tasks.ElixirLSFormat.load_plugins/1
    (language_server 0.22.1) lib/language_server/mix_tasks/format.ex:362: Mix.Tasks.ElixirLSFormat.formatter_for_file/2
    (language_server 0.22.1) lib/language_server/source_file.ex:277: ElixirLS.LanguageServer.SourceFile.formatter_for/3
    (language_server 0.22.1) lib/language_server/providers/formatting.ex:13: ElixirLS.LanguageServer.Providers.Formatting.format/4
    (language_server 0.22.1) lib/language_server/server.ex:1280: anonymous fn/3 in ElixirLS.LanguageServer.Server.handle_request_async/2"

And here is the reproduction repo. It's a freshly generated liveview app with nothing special.

lukaszsamson commented 4 months ago

@NoBrainSkull Formatting works for me with your repo. Tested on macOS VSCode v0.22.1, elixir 1.17.2, OTP 26

NoBrainSkull commented 4 months ago

Well I guess the problem is on my end then... For the records, I'm using neovim and installed elixir-ls through Mason.

I do have the problem but I'm fine staying on the v0.22.0 for now. I suggest closing this issue and maybe reopening if someone else has a similar problem ?

Thank you for your time !

jamilabreu commented 4 months ago

I'm also getting the same error at times, and it randomly goes away

kabturek commented 3 months ago

I'm also getting an error on neovim (Lazy). Removing the plugin fixes the issue.

   Error  12:00:55 lsp.message LSP Message (elixirls) Unable to find formatter for /Users/kabturek/projects/insight/.formatter.exs: "
** (Mix.Error) Formatter plugin Phoenix.LiveView.HTMLFormatter cannot be found\n    
(mix 1.17.2) lib/mix.ex:588: Mix.raise/2\n    
(language_server 0.22.1) lib/language_server/mix_tasks/format.ex:302: anonymous fn/2 in Mix.Tasks.ElixirLSFormat.load_plugins/1\n    
(elixir 1.17.2) lib/enum.ex:2531: Enum.\"-reduce/3-lists^foldl/2-0-\"/3\n    
(language_server 0.22.1) lib/language_server/mix_tasks/format.ex:299: Mix.Tasks.ElixirLSFormat.load_plugins/1\n    
(language_server 0.22.1) lib/language_server/mix_tasks/format.ex:362: Mix.Tasks.ElixirLSFormat.formatter_for_file/2\n    
(language_server 0.22.1) lib/language_server/source_file.ex:277: ElixirLS.LanguageServer.SourceFile.formatter_for/3\n    
(language_server 0.22.1) lib/language_server/providers/formatting.ex:13: ElixirLS.LanguageServer.Providers.Formatting.format/4\n    
(language_server 0.22.1) lib/language_server/server.ex:1280: anonymous fn/3 in ElixirLS.LanguageServer.Server.handle_request_async/2\n"
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Elixir 1.17.2 (compiled with Erlang/OTP 27)

Elixir Language Server version: v0.22.1 Operating system: Mac Editor or IDE name (e.g. Emacs/VSCode): Neovim Editor Plugin/LSP Client name and version: nvim-lspconfig/mason (default LazyVim config)

aar2dee2 commented 3 months ago

getting the same error elixir v1.17.2 live_view v1.0.0-rc.6 macOS

Also getting it for projects with older versions of LiveView. Error disappears if I uninstall the extension.

adamu commented 3 months ago

I was also experiencing this with coc.nvim after upgrading to Elixir 1.17.2.

Funnily enough, just leaving the editor open for a few minutes resolved the issue. I guess it was compiling, but each interaction was interrupting the process. I confirmed that Elixir.Phoenix.LiveView.HTMLFormatter.beam was missing from .elixir_ls/build/dev_test/lib/phoenix_live_view/ebin (actually the phoenix_live_view directory didn't exist), but it appeared after waiting a bit.

edit: Confirmed that deleting .elixir-ls/build then opening a .ex file and waiting for .elixir_ls/build/dev_test/lib/phoenix_live_view to show up again resolves the issue for me.

numso commented 3 months ago

I've also been experiencing this issue off and on (in vscode). Sometimes the formatter fixes itself; other times it'll stay broken for days. The best way I've found to fix it when it does remain broken is:

  1. Remove plugins: [TailwindFormatter, Phoenix.LiveView.HTMLFormatter], from my .formatter.exs
  2. Go save a random .heex file in my project
  3. Revert the change made in step 1

Seems like maybe elixir_ls sometimes has an issue compiling those plugins and following those steps forces a re-compile.

ravensiris commented 2 months ago

Had the same issue with styler. I fixed it by copying _build/test/lib/styler/ to .elixir_ls/build/test/lib/.

grahac commented 2 months ago

Just adding in I had this for weeks. Elixir 1.17.2, OTP 27. Tried installing older versions, uninstalling, reinstalling. Nothing worked. Eventually I uninstalled, trashed the .elixir_ls folder, reinstalled an older version and then upgraded and it worked!

RobertoSchneiders commented 2 months ago

I have the same issue with VSCode, ElixirLS 0.23.0, Elixir 1.17.2, and OTP 27.

I tried some of the solutions presented here but none worked for me. However, sometimes just leaving VSCode open resolves the issue as some already pointed out. One weird thing is that I have the VSCode plugin v0.23.1 but the language server is 0.23.0, I'm not sure what is going on there.

Unable to find formatter for some_path/app.html.heex: ** (Mix.Error) Formatter plugin Phoenix.LiveView.HTMLFormatter cannot be found
(mix 1.17.2) lib/mix.ex:588: Mix.raise/2
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:302: anonymous fn/2 in Mix.Tasks.ElixirLSFormat.load_plugins/1
(elixir 1.17.2) lib/enum.ex:2531: Enum.\"-reduce/3-lists^foldl/2-0-\"/3
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:299: Mix.Tasks.ElixirLSFormat.load_plugins/1
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:362: Mix.Tasks.ElixirLSFormat.formatter_for_file/2
(language_server 0.23.0) lib/language_server/source_file.ex:277: ElixirLS.LanguageServer.SourceFile.formatter_for/3
(language_server 0.23.0) lib/language_server/providers/formatting.ex:13: ElixirLS.LanguageServer.Providers.Formatting.format/4
(language_server 0.23.0) lib/language_server/server.ex:1280: anonymous fn/3 in ElixirLS.LanguageServer.Server.handle_request_async/2

Elixir Language Server version: v0.23.0 Operating system: Ubuntu (VSCode Server) Editor or IDE name: VSCode Editor Plugin/LSP Client name and version: vscode-elixir-ls (0.23.1)

lukaszsamson commented 2 months ago

One weird thing is that I have the VSCode plugin v0.23.1 but the language server is 0.23.0

This is correct. Plugin versioning is independent from language server versioning.

goulvenclech commented 2 months ago

Same error :

Unable to find formatter for /Users/goulvenclech/Documents/encheres_immo/lib/encheres_immo_web/components/form/combobox.ex: "** (Mix.Error) Formatter plugin Surface.Formatter.Plugin cannot be found    
(mix 1.17.2) lib/mix.ex:588: Mix.raise/2    
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:302: anonymous fn/2 in Mix.Tasks.ElixirLSFormat.load_plugins/1    
(elixir 1.17.2) lib/enum.ex:2531: Enum.\"-reduce/3-lists^foldl/2-0-\"/3    
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:299: Mix.Tasks.ElixirLSFormat.load_plugins/1    
(language_server 0.23.0) lib/language_server/mix_tasks/format.ex:362: Mix.Tasks.ElixirLSFormat.formatter_for_file/2    
(language_server 0.23.0) lib/language_server/source_file.ex:277: ElixirLS.LanguageServer.SourceFile.formatter_for/3    
(language_server 0.23.0) lib/language_server/providers/formatting.ex:13: ElixirLS.LanguageServer.Providers.Formatting.format/4\n    (language_server 0.23.0) lib/language_server/server.ex:1280: anonymous fn/3 in ElixirLS.LanguageServer.Server.handle_request_async/2"

Erlang/OTP 26 Elixir 1.17.2 ElixirLS plugin v0.23.1 ElixirLS v0.23.0 VScode 1.92.2 (Universal) MacOS 14.5 (23F79)

Fixed after following this post on Elixir Forum :

Just adding in I had this for weeks on latest version of ElixirLS (0.23.1). Elixir 1.17.2, OTP 27. Tried installing older versions, uninstalling, reinstalling. Nothing worked. Eventually I uninstalled, trashed the .elixir_ls folder, reinstalled an older version and then upgraded Elixir LS and it worked!

lukaszsamson commented 1 month ago

I'm going to close this as there's a confirmed workaround

anildigital commented 1 month ago

@lukaszsamson This needs a proper fix.

lukaszsamson commented 1 month ago

@lukaszsamson This needs a proper fix.

Agreed @anildigital please send a PR or a reliable repro.

sabiwara commented 1 month ago

I think I found a reliable repro, it feels like a race condition:

  1. Cloning the repo shared by @NoBrainSkull https://github.com/elixir-lsp/elixir-ls/issues/1110#issuecomment-2239303380
  2. I just tweaked the .tool-versions to use elixir 1.17.2
  3. Install everything until you get a working state
  4. Close vscode
  5. rm -rf .elixir_ls
  6. Open a file in vscode, e.g. lib/ex17ls/mailer.ex, immediately add some spaces and try to save a couple of times
  7. Get the error
  8. Run Elixir: Restart language server => the formatter works again
lukaszsamson commented 1 month ago

@sabiwara The formatter cannot work until the first build finishes. Formatter API can under that conditions initiate compilation which would conflict with the ongoing compilation. You wouldn't normally run mix format while mix compile is under way. To prevent that ElixirLS is using a vendored compiler API that never compiles anything.

sabiwara commented 1 month ago

Maybe the issue is for big projects for which the compilation is slow, and every new save will trigger a new compilation, delaying the formatter being available? Naive question, but isn't a mix deps.compile enough for the formatter? If the deps didn't change it shouldn't care about compiling the project itself, no? (at least for projects not defining custom plugins)

lukaszsamson commented 1 month ago

It's not trivial to separate deps.compile. Deps change (e.g. when you switch branches or edit config). Elixir is pretty bad in deciding what to recompile. There were dozens of nontrivial errors and crashes so it's safest to rebuild on start. There's no easy escape from the initial compile time. I guess the real issue would be if the plugins get unloaded on each subsequent compile. I will need to check that. The last change that could have affected purging and reloading was d17ee89a30c570b8927df829b3be5a509876e777 introduced in v0.21.0. OP reported that v0.22.0 didn't have the problem while v0.22.1 did. There were no changes affecting build between those versions

lukaszsamson commented 1 month ago

Reports on forum thread started on August 22 a few weeks after v0.23 release, but this post states that v0.20 worked OK

anildigital commented 3 weeks ago

Anyone already fixing this somewhere? Any woraround to avoid this error? thanks

mawkler commented 5 days ago

Just commenting here to say that I have the same issue

mrluc commented 5 days ago

I started seeing this today, after a machine crash. 1.17.2 elixir, latest ElixirLS in VSCode.

Cheers