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.46k stars 194 forks source link

Autocomplete/hover does not work properly for code aliases via use #193

Closed axelson closed 1 year ago

axelson commented 4 years ago

For details see this forum post: https://elixirforum.com/t/problem-with-use-macro-in-vscode-elixirls/30450

Which goes along with this repository: https://github.com/JanKneiphof/autocomplete_problem/

I'm able to reproduce the problem locally but I've been having a hard time reproducing it in tests.

axelson commented 4 years ago

Okay, made a little progress on this. It is related to the Elixir versions in play. It is broken if ElixirLS was compiled under 1.7.4 and running as 1.10.0+, but if ElixirLS was compiled under 1.10.0 and running as 1.10.0 it works. Yet another reason that we need #115

axelson commented 4 years ago

Although https://github.com/elixir-lsp/elixir-ls/pull/121 might be a better short-term fix for it since it involves compiling ElixirLS under the same version of Elixir and Erlang that the project runs with.

rompetomp commented 4 years ago

Any progress on this? I've got the same problem

axelson commented 4 years ago

Nothing yet, and help is definitely appreciated. Another workaround would be to have a configuration to use a local install of ElixirLS (which would be based on #104). As a workaround if you know your vscode extension directory you can overwrite the version of the server there with one installed with a higher elixir version (but all your projects will need to have to use the same or higher version of elixir). But you'd use a command like mix elixir_ls.release -o ~/.vscode-oss/extensions/jakebecker.elixir-ls-0.4.0/elixir-ls-release/

Defman21 commented 4 years ago

To my understanding, the solution to this problem is compiling elixir-ls with erlang/elixir the user have? If so, nvim-lsp handles this pretty well. Maybe we should do the same once the extension is installed and elixir version on user's machine is different with the one used to compile elixir-ls bundled with the plugin?

JohnnyCrazy commented 4 years ago

@axelson Can you confirm that the workaround with compiling elixir-ls with the current installed elixir version still works?

I'm currently unable to get this workaround to work for me, although I'm using 1.12.0-dev as version. My log:

Started ElixirLS v0.5.0
Elixir version: "1.12.0-dev (ae97bb6) (compiled with Erlang/OTP 22)"
Erlang version: "22"
ElixirLS compiled with Elixir 1.12.0-dev and erlang 22
MIX_ENV: test
MIX_TARGET: 
...
[Info  - 4:46:16 PM] Compile took 269 milliseconds
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] Updating index...
[Info  - 4:46:16 PM] [ElixirLS Dialyzer] Checking for stale beam files
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] 1 modules need reindexing
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] 0 callbacks added to index
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] 0 types added to index
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] 1 modules added to index
[Info  - 4:46:16 PM] [ElixirLS WorkspaceSymbols] 7 functions added to index
[Info  - 4:46:18 PM] [ElixirLS Dialyzer] Found 631 changed files in 1816 milliseconds
[Info  - 4:46:25 PM] [ElixirLS Dialyzer] Analysis finished in 47688 milliseconds
[Info  - 4:46:32 PM] [ElixirLS Dialyzer] Analyzing 624 modules: [Ecto.Embedded, Postgrex.Range, Ecto.Queryable.Tuple, Enumerable.Floki.HTMLTree, Ecto.Migration.Table, :cow_spdy, Postgrex.Path, DBConnection.Stream, :cowboy_req, Ecto.Application, Postgrex.Point, Inspect.Ecto.Schema.Metadata, Postgrex.Extensions.Array, Jason.Encoder.Ecto.Association.NotLoaded, :floki_mochi_html, Ecto.Query.Builder.Update, Phoenix.Param.Any, Plug.Head, Phoenix.HTML.FormData, Floki.HTMLTree.IDSeeder, Plug.Conn.WrapperError, Phoenix.Presence.Tracker, Ecto.Multi, Plug.Parsers.UnsupportedMediaTypeError, Ecto.Association.BelongsTo, Plug.Conn.Query, Ecto.Query.Builder.Filter, Ecto.Queryable, Phoenix.Socket.V2.JSONSerializer, Jason.Decoder.Unescape, Plug.Conn.Cookies, Phoenix.Channel, :ranch_protocol, Ecto, Postgrex.Polygon, DBConnection.Query, Ecto.QueryError, :cowboy_http, Mix.Tasks.Phx, Phoenix.Router, Inspect.Ecto.Changeset, :cow_http_te, Plug.Exception.Ecto.CastError, Plug.Exception.Ecto.StaleEntryError, Phoenix.HTML.Safe.Phoenix.LiveView.Rendered, :cowboy_websocket, Ecto.Adapters.SQL.Stream, Plug.Exception.Ecto.NoResultsError, Ecto.Adapters.MySQL, DBConnection.OwnershipError, ...]
[Info  - 4:47:01 PM] [ElixirLS Dialyzer] Analysis finished in 36187 milliseconds
[Info  - 4:47:01 PM] Dialyzer analysis is up to date
[Info  - 4:47:01 PM] [ElixirLS Dialyzer] Writing manifest...
[Info  - 4:47:02 PM] [ElixirLS Dialyzer] Done writing manifest in 1063 milliseconds.

Installed via mix elixir_ls.release -o ~/.vscode-server-insiders/extensions/jakebecker.elixir-ls-0.5.0/elixir-ls-release

axelson commented 4 years ago

@JohnnyCrazy Yes this workaround is still applicable. Although I haven't tested on 1.12.0-dev. But also it's important to consider that the current tip of master only supports Elixir 1.10. 1.11 support is added in https://github.com/elixir-lsp/elixir-ls/pull/360 can you retry first with 1.10 and then with 1.11?

JohnnyCrazy commented 4 years ago

EDIT:

1.11 actually works with the PR you mentioned!

axelson commented 4 years ago

Thanks for the report and especially for testing it with #360. We definitely won't be looking at 1.12.0-dev support since it won't be released until 9 months or so from now.

DragosMocrii commented 3 years ago

I have put together all the steps needed to update the language server inside the extension https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/ . I am now finally getting IntelliSense for my Schema files, LiveView modules, and basically everywhere in my Phoenix application that makes use of use often.

Update: The problem I was having was with how macros were resolved by the Language Server. By default, the LS extension that ships with the plugin is built with Elixir 1.8 and Erlang 21. Subsequently, if your project is using a later Elixir version, such as Elixir 1.13.1 and Erlang 23, you may not be getting all the functionality that the LS is supposed to provide.

maximumstock commented 3 years ago

Thanks everyone and @DragosMocrii for your writeup! In order for this to work, do I have to set my project's Elixir version to the same version as well? I just tried above steps a few times but it seems like thats also required. Pardon me if I'm only stating the obvious.

DragosMocrii commented 3 years ago

@maximumstock I believe that your project's Elixir version needs to be higher or equal to the version that the LS plugin has been built with. However, if the project's Elixir version is too far ahead, then the LS may not work correctly (as was the case I described). If you have multiple Elixir versions (ie if you use asdf), the guide I wrote may not work, or may need to be adjusted in some way. I was using asdf before, but couldn't get the LS plugin to compile, so I had to install Elixir globally (one version). See this comment too https://github.com/elixir-lsp/elixir-ls/issues/193#issuecomment-630990426

maximumstock commented 3 years ago

@DragosMocrii Alright, thank you very much for replying. I'll keep an eye on it.

benvp commented 3 years ago

Wouldn't it make sense to provide some information regarding this in the README? I mean, as it's buried here most people might not really get the full functionality elixir-ls provides. I would assume a lot of projects are running OTP > 21 right now.

kasvith commented 3 years ago

I tried compiling elixir-lsp with my local elixir version

Erlang/OTP 22 [erts-10.7] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Elixir 1.11.2 (compiled with Erlang/OTP 21)

But I'm still not getting auto-complete for use macros

defmodule CommunityWeb.Schema.ContentTypes do
  use Absinthe.Schema.Notation

  # object doesnt autocomplete
  object :post do
    field :name, non_null(:string)
  end
end

I had to import it to get autocompletion like follows, which seems wrong

defmodule CommunityWeb.Schema.ContentTypes do
  use Absinthe.Schema.Notation
  import Absinthe.Schema.Notation

  object :post do # object doesnt autocomplete
    field :name, non_null(:string)
  end
end
benvp commented 3 years ago

Can you try the following on your machine? Assuming you are running VSCode and the latest 0.8.0 version of the extension.

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

Then check in the output console for ElixirLS that it states the following when opening a mix project. The last line should match your Erlang/OTP and Elixir version.

Started ElixirLS v0.8.0
Elixir version: "1.12.2 (compiled with Erlang/OTP 24)"
Erlang version: "24"
ElixirLS compiled with Elixir 1.12.2 and erlang 24
kasvith commented 3 years ago

Elixir-LS output

Started ElixirLS v0.8.0
Elixir version: "1.11.2 (compiled with Erlang/OTP 21)"
Erlang version: "22"
ElixirLS compiled with Elixir 1.11.2 and erlang 22
MIX_ENV: test

My elixir version

Erlang/OTP 22 [erts-10.7] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Elixir 1.11.2 (compiled with Erlang/OTP 21)
benvp commented 3 years ago

I think the issue is that your elixir version has compiled with OTP 21 and the ElixirLS version has been compiled using OTP 22. ElixirLS does not support elixir versions which have been compiled with a lower OTP version than the ElixirLS has been compiled against (afaik).

kasvith commented 3 years ago

I tried on my mac with OTP 24(both elixir and elixir-ls). But no luck. Still no autocompletion for use

DragosMocrii commented 3 years ago

@kasvith can you try following these instructions https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/ ? Also, if you installed Elixir/Erlang with asdf, it may not work for you.

kasvith commented 3 years ago

@DragosMocrii thanks for the article, I already tried that(found this in the same thread when I searched before). For some reason I do not get autocomplete for use

kasvith commented 3 years ago

It seems with the same setup phx macros get auto-completed with use

KaranAhlawat commented 2 years ago

As of 22nd January, 2022, this is still a problem. I'm gonna leave what my inference on the situation is to hopefully help others like me.

@kasvith can you try following these instructions https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/ ? Also, if you installed Elixir/Erlang with asdf, it may not work for you.

  1. If you follow that blog, it'll allow help you build ElixirLS with your version of Elixir and Erlang.
  2. Doing that fixes a lot of issues with the autocomplete, particularly with the use macro. (Tested with Ecto and it's components, seems to work as expected)
  3. Does not work for Absinthe, so I'm assuming there are other libraries out there like this as well.
  4. Using import Absinthe.<module_name> instead of use Absinthe.<module_name> seems to work as expected, although that does not seem to be the preferred method as seen in their documentation.
kasvith commented 2 years ago

Yes Absinthe doesn't work well with ElixirLS unless you use use Absinthe.<module> syntax

lukaszsamson commented 2 years ago

@kasvith @KaranAhlawat you are hitting a limitation in elixir_sense. It has only a limited understanding of macro generated code. In order to have first class support for Absinthe it would require a dedicated plugin. We already have one for Ecto (https://github.com/elixir-lsp/elixir_sense/tree/master/lib/elixir_sense/plugins) and we are open for contributions.

kasvith commented 2 years ago

Thanks for the info...this problem is kind of a nightmare

tusqasi commented 1 year ago

@kasvith can you try following these instructions https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/ ? Also, if you installed Elixir/Erlang with asdf, it may not work for you.

I use asdf and I can confirm it doesn't work. Is there no workaround for asdf for now?

kasvith commented 1 year ago

@kasvith can you try following these instructions https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/ ? Also, if you installed Elixir/Erlang with asdf, it may not work for you.

I use asdf and I can confirm it doesn't work. Is there no workaround for asdf for now?

its not a problem with asdf

tusqasi commented 1 year ago

Ahh. 😅😅.

I didn't understand what read. This makes me think I probably didn't read the article correctly.

I will try again.

ellismarkf commented 1 year ago

I followed @DragosMocrii instructions, and it resolved the issue for me! Now autocomplete/hover works for code aliased with use in VS Code.

I have a separate issue though, which I think it related, and just curious what the expected behavior is. Phoenix Controller actions always take Plug.Conn as the first parameter. After rebuilding elixir-ls with my machine's global Elixir instance, autocomplete/hover works for all the functions from the :controller module. Cool.

Screenshot 2023-03-06 at 12 12 36 PM

But hovering over the conn variable in the action body doesn't do anything, and I don't get any autocomplete for it, even when decorated with the typespec @spec. 😢

Screenshot 2023-03-06 at 12 16 23 PM

The typespec does provide IntelliSense for Plug.Conn when hovering over the @spec:

Screenshot 2023-03-06 at 12 23 47 PM

But in the action body, it seems like conn is just treated as a variable that VS Code doesn't know anything about. This isn't specific to conn; any function parameter seems to lose type information and intellisense:

Screenshot 2023-03-06 at 12 28 52 PM

It seems like, especially with the @spec that VS Code should know what conn or n is in the function body.

Is there some additional configuration that would enable this? Is this supported at all?

benvp commented 1 year ago

I don't know if specs are considered, but you could pattern match on the Plug.Conn struct in the function header to receive auto complete on the struct fields.

create(%Plug.Conn{} = conn, params)

lukaszsamson commented 1 year ago

@ellismarkf There actually 2 (or more) separate issues you mention. First as @benvp speculated specs from the module/behaviours/protocols are not considered in elixir_sense type inference engine. Currently only assignments or match expressions are used so %Plug.Conn{} = conn should work. I wanted to add this at some point and I created https://github.com/elixir-lsp/elixir_sense/issues/203 for tracking. The second issue is that hover provider doesn't currently have support for variables. It's already tracked in https://github.com/elixir-lsp/elixir_sense/issues/164

roryfahy commented 1 year ago

I have put together all the steps needed to update the language server inside the extension dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use . I am now finally getting IntelliSense for my Schema files, LiveView modules, and basically everywhere in my Phoenix application that makes use of use often.

Update: The problem I was having was with how macros were resolved by the Language Server. By default, the LS extension that ships with the plugin is built with Elixir 1.8 and Erlang 21. Subsequently, if your project is using a later Elixir version, such as Elixir 1.13.1 and Erlang 23, you may not be getting all the functionality that the LS is supposed to provide.

Should this in theory now be fixed by the latest release? @lukaszsamson @DragosMocrii

image
Defman21 commented 1 year ago

Using 0.14.2, I now have auto-completion in Ecto migrations (which does use use Ecto.Migration in it), so I guess it is fixed?

lukaszsamson commented 1 year ago

Using 0.14.2, I now have auto-completion in Ecto migrations (which does use use Ecto.Migration in it), so I guess it is fixed?

So far only the vscode extension uses Mix.install and there are still issues that need ironing out

vanderhoop commented 1 year ago

If anyone other VS Code users have upgraded to OTP 26, you'll likely encounter issues with the language server, which (as of today) on master is compiled with OTP 25. You can still follow the instructions from https://dragoshmocrii.com/fix-vscode-elixirls-intellisense-for-code-imported-with-use/, but, as a temporary fix, you'll have to change git clone --depth 1 https://github.com/elixir-lsp/elixir-ls to git clone --depth 1 --branch otp26 https://github.com/elixir-lsp/elixir-ls

lukaszsamson commented 1 year ago

@vanderhoop elixirLS does not support OTP26 as it has critical IO bugs impacting LSP protocol, see https://github.com/elixir-lsp/elixir-ls/issues/886. I'll resume work on official support when OTP releases fixes for those problems.

adonig commented 1 year ago

@vanderhoop I run into this error:

[asd@fwk other]$ git clone --depth 1 --branch otp26 https://github.com/elixir-lsp/elixir-ls
Cloning into 'elixir-ls'...
remote: Enumerating objects: 676, done.
remote: Counting objects: 100% (676/676), done.
remote: Compressing objects: 100% (517/517), done.
remote: Total 676 (delta 96), reused 464 (delta 86), pack-reused 0
Receiving objects: 100% (676/676), 612.83 KiB | 1.30 MiB/s, done.
Resolving deltas: 100% (96/96), done.
[asd@fwk other]$ cd elixir-ls
[asd@fwk elixir-ls]$ mix deps.get
* Getting jason_v (https://github.com/elixir-lsp/jason.git - c81537e2a5e1acacb915cf339fe400357e3c2aaa)
remote: Enumerating objects: 1553, done.        
remote: Counting objects: 100% (229/229), done.        
remote: Compressing objects: 100% (87/87), done.        
remote: Total 1553 (delta 160), reused 148 (delta 142), pack-reused 1324        
* Getting mix_task_archive_deps (https://github.com/elixir-lsp/mix_task_archive_deps.git)
remote: Enumerating objects: 265, done.        
remote: Counting objects: 100% (57/57), done.        
remote: Compressing objects: 100% (27/27), done.        
remote: Total 265 (delta 20), reused 57 (delta 20), pack-reused 208        
* Getting dialyxir_vendored (https://github.com/elixir-lsp/dialyxir.git - 896fa45817c6a1be8ec408577c88ab52c27f6851)
remote: Enumerating objects: 3179, done.        
remote: Counting objects: 100% (367/367), done.        
remote: Compressing objects: 100% (174/174), done.        
remote: Total 3179 (delta 169), reused 341 (delta 158), pack-reused 2812        
fatal: reference is not a tree: 896fa45817c6a1be8ec408577c88ab52c27f6851
** (Mix) Command "git --git-dir=.git checkout --quiet 896fa45817c6a1be8ec408577c88ab52c27f6851" failed

That looks like the error I get in VSCodium when I enable regular ElixirLS for my Phoenix project:

** (Mix.Error) Command "git --git-dir=.git checkout --quiet 896fa45817c6a1be8ec408577c88ab52c27f6851" failed with reason: fatal: reference is not a tree: 896fa45817c6a1be8ec408577c88ab52c27f6851

    (mix 1.14.5) lib/mix.ex:513: Mix.raise/2
    (mix 1.14.5) lib/mix/scm/git.ex:136: Mix.SCM.Git.checkout/2
    (elixir 1.14.5) lib/file.ex:1607: File.cd!/2
    (mix 1.14.5) lib/mix/dep/fetcher.ex:61: Mix.Dep.Fetcher.do_fetch/3
    (mix 1.14.5) lib/mix/dep/converger.ex:213: Mix.Dep.Converger.all/9
    (mix 1.14.5) lib/mix/dep/converger.ex:224: Mix.Dep.Converger.all/9
    (mix 1.14.5) lib/mix/dep/converger.ex:146: Mix.Dep.Converger.all/7
    (mix 1.14.5) lib/mix/dep/converger.ex:95: Mix.Dep.Converger.all/4
[Error - 11:28:58 AM] Server initialization failed.
  Message: Pending response rejected since connection got disposed
  Code: -32097 
[Error - 11:28:58 AM] The ElixirLS - apix server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.
[Error - 11:28:58 AM] ElixirLS - apix client: couldn't create connection to server.
  Message: Pending response rejected since connection got disposed
  Code: -32097 
[Error - 11:28:58 AM] Restarting server failed
  Message: Pending response rejected since connection got disposed
  Code: -32097 
lukaszsamson commented 1 year ago

This should no longer be an issue. ElixirLS 0.15 (and 0.14 for vscode extension) switched release mode to Mix.install. This ensures that it is built with the correct elixir and OTP combination. If anyone is still using .ez archives please consider updating to Mix.install release or manage your ElixirLS builds by yourself