elixir_sense icon indicating copy to clipboard operation
elixir_sense copied to clipboard

definition not able to locate elixir sources on some ocasions

Open justindotpub opened this issue 5 years ago • 15 comments

Should ElixirSense.definition/3 be able to handle modules that are provided with Elixir, like GenServer? See https://github.com/balduncle/playground/blob/master/test/playground_test.exs for 3 tests that are currently failing due to the paths to the source files for built in modules not existing on my computer and instead being associated with the machine Elixir was built on. Note that the Enum test comes straight from the docs on hex.

Thanks.

justindotpub avatar Aug 25 '19 15:08 justindotpub

In case it is helpful, that repo has Rexbug printing out a trace to help quickly see each function's arguments and return value.

justindotpub avatar Aug 25 '19 15:08 justindotpub

I see now that if I install elixir from source (e.g. with asdf install elixir ref:HEAD) the tests pass. Would it make sense to 1) call this out in the docs, and 2) consider returning something else that more clearly states why the definition wasn't found?

justindotpub avatar Aug 25 '19 18:08 justindotpub

@balduncle yes, we can only "find definition" or "go to definition" if Elixir was installed from source. Not sure if there's another way to do this. Maybe we could accept an option like src_path and then users could just download the source and set the option. Ideally, I think the editor could even ask the user if he wants the source to be downloaded and automatically set the option whenever the editor needs to find definitions. Since this will demand work on the editor plugins side, we would need confirmation from those projects maintainers if they are willing to do those changes.

  1. call this out in the docs

We should definitely have that explicit in the docs, however, remember that the end-user doesn't even know that he should read ElixirSense's docs when something doesn't work as expected, he just uses the editor that uses a plugin that uses ElixirSense under the hood.

  1. consider returning something else that more clearly states why the definition wasn't found?

I'm open to suggestions here ;)

msaraiva avatar Sep 05 '19 18:09 msaraiva

Yeah, I like the idea of having the editors download the source. It would alleviate tickets like https://github.com/JakeBecker/vscode-elixir-ls/issues/123. I'm willing to try to tackle this for vscode-elixir-ls, for what it's worth.

Fair point on the docs being more valuable for end users if present in a library that calls into ElixirSense.

justindotpub avatar Sep 07 '19 12:09 justindotpub

@msaraiva any update on this? I tried to install from source asdf install elixir ref:HEAD but Go To Definition is still not working

khanakia avatar Nov 07 '21 05:11 khanakia

@khanakia can yo debug it with?

iex(3)> Enum.module_info[:compile][:source]
'/Users/me/elixir/lib/elixir/lib/enum.ex'

on my machine the sources are in the directory that is returned (I don't use asdf, I build elixir myself)

head -n 1 /Users/me/elixir/lib/elixir/lib/enum.ex 
defprotocol Enumerable do

lukaszsamson avatar Nov 07 '21 07:11 lukaszsamson

@lukaszsamson

iex(2)> Enum.module_info[:compile][:source]
'/home/build/elixir/lib/elixir/lib/enum.ex'
iex(3)> 

The above path does not exists. But i verified this path exists /Users/khanakia/.asdf/installs/elixir/1.12.3-otp-24/lib/elixir/lib/enum.ex

My binaries are located in this folder

/Users/khanakia/.asdf/shims/iex
/Users/khanakia/.asdf/shims/elixir

How can I change the path to point the elixir to a new path?

khanakia avatar Nov 07 '21 09:11 khanakia

@lukaszsamson So paths are correct now after I compiled with source. This is the new path it's showing now

iex(1)> Enum.module_info[:compile][:source]
'/Users/khanakia/D1/elixir/lib/elixir/lib/enum.ex'
iex(2)> 

But Go to definition still not works as you can see in the attached screenshot Screenshot 2021-11-07 at 2 08 09 PM

khanakia avatar Nov 07 '21 09:11 khanakia

@lukaszsamson Also i just reinstalled the elixir-ls vscode extension and I can see now the extension is not loaded at all and the reason extension looks for a binary under asdf path but as I compiled the binaries manually so they exist in the different path now.

As https://github.com/elixir-lsp/vscode-elixir-ls required the elixir to be installed using the asdf path.

Refer to this issue. https://github.com/elixir-lsp/vscode-elixir-ls/issues/208

Show how did you make it work with the custom build then as vscode extension requires asdf LOL ?

khanakia avatar Nov 07 '21 09:11 khanakia

I have fixed the issue. Here are the steps

  1. Download the source code zip from here https://github.com/elixir-lang/elixir/releases/tag/v1.12.3
  2. Unzip the file and rename it to elixir
  3. cd elixir
  4. make clean test
  5. Edit .bashrc or .zshrc file and it should be like this
. $HOME/.asdf/asdf.sh
export PATH=$PATH:/Users/khanakia/.asdf/shims
export PATH="$PATH:/Users/khanakia/D1/elixir/bin"
  1. Make sure you do not have elixir installed using asdf
  2. Run these commands
asdf global elixir path:/Users/khanakia/D1/elixir
asdf plugin add elixir
  1. Verify the asdf plugins by running asdf current and this will output
elixir          path:/Users/khanakia/D1/elixir /Users/khanakia/.tool-versions
erlang          24.1.4          /Users/khanakia/.tool-versions
  1. Confirm elixir -v
Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.12.3 (compiled with Erlang/OTP 24)
  1. Now you can open the vscode with elixir-ls plugin installed and it will fetch the core function definition e.g. IO.puts("demo") I can simply click the puts and it will open the IO code file

khanakia avatar Nov 07 '21 14:11 khanakia

Must I install elixir via asdf to use this? Im an Intel Mac user, I installed elixir via brew. And i have the same problems that i cant use the Go to Definition function with the VS Code extension

Sky0307 avatar Jan 06 '22 03:01 Sky0307

I have fixed the issue. Here are the steps

  1. Download the source code zip from here https://github.com/elixir-lang/elixir/releases/tag/v1.12.3
  2. Unzip the file and rename it to elixir
  3. cd elixir
  4. make clean test
  5. Edit .bashrc or .zshrc file and it should be like this
. $HOME/.asdf/asdf.sh
export PATH=$PATH:/Users/khanakia/.asdf/shims
export PATH="$PATH:/Users/khanakia/D1/elixir/bin"
  1. Make sure you do not have elixir installed using asdf
  2. Run these commands
asdf global elixir path:/Users/khanakia/D1/elixir
asdf plugin add elixir
  1. Verify the asdf plugins by running asdf current and this will output
elixir          path:/Users/khanakia/D1/elixir /Users/khanakia/.tool-versions
erlang          24.1.4          /Users/khanakia/.tool-versions
  1. Confirm elixir -v
Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.12.3 (compiled with Erlang/OTP 24)
  1. Now you can open the vscode with elixir-ls plugin installed and it will fetch the core function definition e.g. IO.puts("demo") I can simply click the puts and it will open the IO code file

i tried these steps, however im stucked at step 4, i got the following error after running the command make clean test

rm -rf ebin rm -rf lib//ebin rm -rf lib/elixir/src/elixir_parser.erl rm -rf lib//_build/ rm -rf lib/*/tmp/ rm -rf lib/elixir/test/ebin/ rm -rf lib/mix/test/fixtures/deps_on_git_repo/ rm -rf lib/mix/test/fixtures/git_rebar/ rm -rf lib/mix/test/fixtures/git_repo/ rm -rf lib/mix/test/fixtures/git_sparse_repo/ rm -f erl_crash.dump rm -f man/elixir.1 rm -f man/elixir.1.bak rm -f man/iex.1 rm -f man/iex.1.bak make: erlc: No such file or directory make: *** [lib/elixir/src/elixir_parser.erl] Error 1

Sky0307 avatar Jan 06 '22 03:01 Sky0307

Can i ask the if go to definition function will only on default function such IO.inspect etc?

Currently i can see the definition of IO.inspect, IO.puts etc.

But, I have a helper.ex file and when i import it into another file(example, users.ex), i can't see the definition of the functions that is in the helper.ex from the users.ex file, is this normal?

Sky0307 avatar Jan 06 '22 04:01 Sky0307

@Sky0307 the steps i gave once you perform VS Code Go To Definition will work for everything.

Regarding helper.ex you cannot inspect because you did not compile the file using elixirc. So there are 2 ways:

  1. Compile the helper.ex file within the same directory and then you will see the VS Code Definition
  2. Use mix to create a project. That the most common approach everybody uses.

khanakia avatar Jan 06 '22 07:01 khanakia

@khanakia Thanks for your help! I actually created my project with mix. Anyway, I managed to get the extension working! Here's a few things I did, hope it will help others who facing the same issue.

I am an Intel Mac user, I installed both elixir and asdf with brew.

Then, I did the following:

  1. edit .zshrc file

. /usr/local/opt/asdf/libexec/asdf.sh export PATH=$PATH:/Users/skylee/.asdf/shims export PATH="$PATH:/usr/local/Cellar/elixir/1.13.1"

  1. run the command

asdf global elixir path:/usr/local/Cellar/elixir/1.13.1/ asdf plugin add elixir

  1. asdf current gives me the following output

elixir path:/usr/local/Cellar/elixir/1.13.1/ /Users/skylee/.tool-versions erlang path:/usr/local/Cellar/erlang/24.2/ /Users/skylee/.tool-versions

On top of this, I compiled my project with elixir -S mix phx.server

Sky0307 avatar Jan 07 '22 06:01 Sky0307

Hi there,

has anyone figured out how to get the language server to find sources on Windows? I see:

Elixir sources not found (checking in d:/home/runner/work/elixir/elixir

I installed Elixir using the standard installer which puts it in the program files directory (I assume with no source). I'd be happy to manually download the source and configure a pointer to it if that's a possibility.

(Note that d:/home/ is not a valid directory on my machine. I'm not sure where it's getting that path from).

(Additionally, asdf isn't available for Windows and building Elixir from source on Windows appears to be non-trivial - although I'd love to be proved wrong).

samaaron avatar Dec 18 '22 12:12 samaaron