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

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

Closed
axelson opened this issue Apr 10, 2020 · 39 comments
Closed

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

axelson opened this issue Apr 10, 2020 · 39 comments
Labels
bug Something isn't working

Comments

@axelson
Copy link
Member

axelson commented Apr 10, 2020

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 axelson added the bug Something isn't working label Apr 10, 2020
@axelson
Copy link
Member Author

axelson commented Apr 10, 2020

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

axelson commented Apr 16, 2020

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

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

@axelson axelson added the help wanted Extra attention is needed label May 19, 2020
@axelson
Copy link
Member Author

axelson commented May 19, 2020

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

Defman21 commented Sep 6, 2020

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

@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
Copy link
Member Author

axelson commented Sep 16, 2020

@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 #360 can you retry first with 1.10 and then with 1.11?

@JohnnyCrazy
Copy link

JohnnyCrazy commented Sep 16, 2020

EDIT:

1.11 actually works with the PR you mentioned!

@axelson
Copy link
Member Author

axelson commented Sep 19, 2020

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

DragosMocrii commented Nov 19, 2020

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

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

@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 #193 (comment)

@maximumstock
Copy link

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

@benvp
Copy link

benvp commented May 14, 2021

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

kasvith commented Aug 26, 2021

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

benvp commented Aug 26, 2021

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

kasvith commented Aug 27, 2021

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

benvp commented Aug 27, 2021

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

kasvith commented Aug 27, 2021

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

@DragosMocrii
Copy link

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

kasvith commented Aug 27, 2021

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

kasvith commented Aug 27, 2021

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

@KaranAhlawat
Copy link

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

kasvith commented Jan 23, 2022

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

@lukaszsamson
Copy link
Collaborator

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

kasvith commented May 26, 2022

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

@tusqasi
Copy link

tusqasi commented Oct 19, 2022

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

kasvith commented Oct 19, 2022

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

tusqasi commented Oct 21, 2022

Ahh. 😅😅.

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

I will try again.

@ellismarkf
Copy link

ellismarkf commented Mar 6, 2023

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

benvp commented Mar 7, 2023

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

@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 elixir-lsp/elixir_sense#203 for tracking.
The second issue is that hover provider doesn't currently have support for variables. It's already tracked in elixir-lsp/elixir_sense#164

@roryfahy
Copy link

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

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

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

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

@vanderhoop elixirLS does not support OTP26 as it has critical IO bugs impacting LSP protocol, see #886. I'll resume work on official support when OTP releases fixes for those problems.

@adonig
Copy link

adonig commented May 27, 2023

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

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

@lukaszsamson lukaszsamson removed the help wanted Extra attention is needed label Jun 20, 2023
@lukaszsamson lukaszsamson unpinned this issue Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests