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

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

Open axelson opened this issue 4 years ago • 26 comments

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 avatar Apr 10 '20 01:04 axelson

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 avatar Apr 10 '20 18:04 axelson

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.

axelson avatar Apr 16 '20 17:04 axelson

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

rompetomp avatar May 19 '20 10:05 rompetomp

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/

axelson avatar May 19 '20 18:05 axelson

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?

Defman21 avatar Sep 06 '20 20:09 Defman21

@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

JohnnyCrazy avatar Sep 16 '20 14:09 JohnnyCrazy

@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?

axelson avatar Sep 16 '20 17:09 axelson

  • 1.10 worked perfectly :)
  • 1.11 worked as well, needed to compile with the mentioned PR ( #360 )

EDIT:

1.11 actually works with the PR you mentioned!

JohnnyCrazy avatar Sep 16 '20 17:09 JohnnyCrazy

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.

axelson avatar Sep 19 '20 19:09 axelson

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.

DragosMocrii avatar Nov 19 '20 03:11 DragosMocrii

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.

maximumstock avatar Feb 11 '21 09:02 maximumstock

@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

DragosMocrii avatar Feb 11 '21 17:02 DragosMocrii

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

maximumstock avatar Feb 11 '21 17:02 maximumstock

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.

benvp avatar May 14 '21 10:05 benvp

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

kasvith avatar Aug 26 '21 06:08 kasvith

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

benvp avatar Aug 26 '21 07:08 benvp

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)

kasvith avatar Aug 27 '21 04:08 kasvith

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).

benvp avatar Aug 27 '21 09:08 benvp

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

kasvith avatar Aug 27 '21 18:08 kasvith

@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.

DragosMocrii avatar Aug 27 '21 18:08 DragosMocrii

@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 avatar Aug 27 '21 18:08 kasvith

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

kasvith avatar Aug 27 '21 19:08 kasvith

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.

KaranAhlawat avatar Jan 22 '22 18:01 KaranAhlawat

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

kasvith avatar Jan 23 '22 07:01 kasvith

@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.

lukaszsamson avatar Jan 23 '22 07:01 lukaszsamson

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

kasvith avatar May 26 '22 04:05 kasvith

@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?

tusqasi avatar Oct 19 '22 10:10 tusqasi

@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

kasvith avatar Oct 19 '22 11:10 kasvith

Ahh. 😅😅.

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

I will try again.

tusqasi avatar Oct 21 '22 01:10 tusqasi

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?

ellismarkf avatar Mar 06 '23 18:03 ellismarkf