Skip to content

VS Code Broken for Elixir

You tried using VS Code for Elixir development. You tried out the ElixirLS extension for code insight and code completion. However, the ElixirLS extension kept crashing and never worked for you.

ElixirLS server crashed 5 times
VS Code error display when ElixirLS crashes

This post can help you diagnose the issue and fix it. Let’s get you back on the productive path with your Elixir development.

What’s wrong?

VS Code has an “Output” panel that gives additional insight to what different extensions are doing.

From the VS Code menu, View > Output.

On the Output panel is a dropdown, select “ElixirLS”.

This displays what’s going on with ElixirLS. Any problems will be displayed here. When you open this panel, the output will be scrolled to the bottom. To see issues with ElixirLS starting up, scroll to the top.

The next sections are some of the common problems people may encounter.

It spikes my CPU and my fans go crazy

I touch on this in my previous post about using ElixirLS in VS Code.

The explanation is that ElixirLS runs dialyzer in the background on first run. Dialyzer is a library that creates a PLT (Persistent Lookup Table). The PLT is used to give code completion for the version of Elixir you have installed and for your project. Additionally, it displays the dialyzer warnings in the IDE on the line with the problem.

So yes, on first run it uses Dialyzer to build the PLT. Depending on your machine and project, this can take ~15 minutes. If you let it complete, it won’t keep doing it.

Additionally, be aware that when you change your version or Elixir or Erlang it will need to rebuild the full PLT. So you will encounter this process again.

Elixir command not found

Does your output look something like this?

/Users/dood/.vscode/extensions/jakebecker.elixir-ls-0.2.25/elixir-ls-release/language_server.sh: line 18: elixir: command not found
[Info  - 1:13:04 PM] Connection to server got closed. Server will restart.
/Users/dood/.vscode/extensions/jakebecker.elixir-ls-0.2.25/elixir-ls-release/language_server.sh: line 18: elixir: command not found
[Info  - 1:13:04 PM] Connection to server got closed. Server will restart.
/Users/dood/.vscode/extensions/jakebecker.elixir-ls-0.2.25/elixir-ls-release/language_server.sh: line 18: elixir: command not found
[Info  - 1:13:04 PM] Connection to server got closed. Server will restart.
/Users/dood/.vscode/extensions/jakebecker.elixir-ls-0.2.25/elixir-ls-release/language_server.sh: line 18: elixir: command not found
[Info  - 1:13:04 PM] Connection to server got closed. Server will restart.
/Users/dood/.vscode/extensions/jakebecker.elixir-ls-0.2.25/elixir-ls-release/language_server.sh: line 18: elixir: command not found
[Error - 1:13:04 PM] Connection to server got closed. Server will not be restarted.

The key here is elixir: command not found. Asdf (or however you have have Elixir installed) isn’t setup correctly. ElixirLS can’t find the elixir command. For MacOS and Linux users, follow the asdf installation process to fix this.

If using asdf, you may need to “reshim” the elixir command.

asdf reshim elixir <your-version>

Use the asdf command by itself for a list of other commands available.

Erlang not installed

Did you use asdf to install Elixir? Did you know you also need to install Erlang? VS Code’s ElixirLS output shows this problem when you have Elixir installed with no Erlang.

asdf: No version set for command erl
you might want to add one of the following in your .tool-versions file:

erlang 21.0.7
erlang 21.1.3
erlang 22.0.7
erlang 19.3.6.9
erlang 22.1.7
[Info  - 6:08:00 AM] Connection to server got closed. Server will restart.

The solution is to install Erlang using asdf too. Make sure you install a compatible version!

Follow the asdf installation process to fix this.

Version Mismatch

At the top of the ElixirLS output, it displays the versions detected.

Started ElixirLS
Elixir version: "1.7.4 (compiled with Erlang/OTP 19)"
Erlang version: "22"
Compiling with Mix env test
[Info  - 7:41:33 AM] Compile took 224 milliseconds
...

Notice that the Elixir 1.7.4 “(compiled with Erlang/OTP 19)” does not match the Erlang version “22”. The OTP number refers to the Erlang number.

Follow the asdf installation process to fix this.

ElixirLS stopped working

Everything was working great until one day it stopped working. Code completion is broken and when you look at the ElixirLS output, it complains about something that doesn’t make much sense.

What happened?

A number of things may have happened. A likely one is you added a new dependency or upgraded an existing one.

Have you ever upgraded a project dependency? At some point you will. After updating, sometimes your app gives strange compilation errors. The fix is to run mix clean. This clears out the compilation artifacts in your project’s _build directory. This forces a clean recompile for the project and everything works smoothly again! … Except VS Code and ElixirLS is broken. Running mix clean doesn’t clean out the behind-the-scenes ElixirLS build artifacts it uses to give code completion and insight.

How do I fix it?

Inside the .elixirls/ directory is a build directory. This is just like your project’s_build directory. Delete .elixirls/build/ and restart VS Code and it should be working again! If you watch the ElixirLS output, you’ll see it recompile your project. So wait for that to finish before deciding if ElixirLS works again.

Note: You can delete the entire .elixirls/ directory. Doing this also deletes the built dialyzer PLT files. When those are deleted ElixirLS rebuilds them… and that can take ~20 minutes and your fans kick in. As a short-cut to productivity, try only deleting the .elixirls/build/ directory.

Cleaning up after a change

Assuming you changed something with your installed versions, then it is best to clean your project’s build artifacts. Otherwise you’re likely to get strange compilation errors. This is a good idea to do whenever you change your active Elixir or Erlang versions.

Use the following command.

mix do clean, compile

Also note that the ElixirLS extension creates build artifacts inside the .elixirls/ directory in your project. This contains compiled code for your project that ElixirLS uses to give code insight. To avoid potential issues, also delete that directory to get a clean start.

Use the ElixirLS Fork

A fork of ElixirLS exists. Searching in the VS Code extensions for “elixirls” shows it as “ElixirLS Fork: Elixir Support and Debugger” by “elixir-lsp”. The fork merges in community contributions that were not being merged by the official project.

The ElixirLS Fork extension to use.

Specifically, active development is taking place on a library called elixir_sense that “provides context-aware information for code completion, documentation, go/jump to definition, signature info and more”. The improvements in elixir_sense are not finding their way into the official ElixirLS extension. The ElixirLS Fork is staying up to date with the lower elixir_sense library.

Using the fork may not improve extension stability, but it will improve the Elixir information available in VS Code.

If you switched to the ElixirLS Fork, make sure you do not leave both installed and activated.

Closing

Hopefully this helped you resolve roadblocks on your way to a productive Elixir development experience. Let me know if you encountered other issues that can be documented here as well.

Are you learning Elixir? Check out my courses to help you make the mental shifts faster and easier!

Leave a Comment

You must be logged in to post a comment.