Elixir Development with Visual Studio Code
It is always helpful to have an IDE where you can run and debug your programs. Unfortunately, there is no a decent IDE yet for the Elixir programming language but today we will configure Visual Studio Code to be able to run and debug elixir projects.
First of all, you will need to have Erlang, Elixir and Visual Studio Code installed in your environment.
Create a basic Elixir project with Mix
Mix is a tool that you can use to create, compile, test and manage the dependencies of your Elixir application. It is shipped along with Elixir. If you come from the Java world (like me) Mix is for Elixir like Gradle or Maven is for Java.
Let's create a very simple project using Mix running the following:
$> mix new my_project
That is the simplest command you can run with Mix to create a skeleton of an Elixir project.
Now if you have already installed Visual Studio Code you can open the folder or run the
code command inside the folder
$> code .
You can immediately notice that Visual Studio Code identifies files with extension
.exs ; and tells you that the Marketplace has extensions to use those files.
If you search in the Visual Studio Market place you will find a lot of interesting extensions but today we are going to install the following extensions:
The extension ElixirLS adds a variety of new abilities to Visual Studio Code to handle Elixir projects like autocomplete, go-to-definition, code formatter, quick symbol lookup in files, inline reporting of build warnings and errors, debugger support and more. The most important for our objective today is the debugger support.
Let’s change the module
MyProject to have the following in the file
Also, notice that now you can see the code highlighted.
Now, let's write a simple test for our module like the following in the file
Once that we have our module and test ready we can run our test in our Elixir project using Mix like:
$> mix test
That is cool, but what if we want to run our test in Visual Studio Code?.
The first thing you need to do is to go to the Debug view at the left in your Visual Studio Code.
Notice that you will see “No Configurations” and also a red dot at the engine icon. Click on the engine icon and Visual Studio Code will ask you to select the environment you want to generate a configuration. Select “Mix Tasks” and VSC will generate a new folder
.vscode in your project and a new file
launch.js inside the folder. (Note: this folder shouldn’t go into your git repository :P).
launch.json file tells Debug view which configurations to load to run/debug your project. If you go again into the Debug view of VSC you will notice that there is no more the message “No Configuration” and instead you will see all configurations that the
launch.json file has.
Now, instead of running your Elixir tests in your terminal try selecting the configuration
mix test in the Debug view and clicking on the green arrow. If you open the debug console
View->Debug Consoleyou will see the following:
Try again adding some breakpoints into your code and see the results.
Launch configurations have different attributes that are used in different debuggers and debugging scenarios. I found the following attributes the most useful when developing Elixir apps.
- type. This is the type of the configuration and it depends on the installed debugger. In our case, this will always will have the value “mix_task” when using the ElixirLS debugger.
- name. the name which appears in Debug view.
- preLaunchTask. This attribute is useful to run a “task” before the debug session. Tasks are defined in a file named
.vscode. For example, you can define a task to run
mix deps.getand run that task before a debug session just to be sure that your dependencies are there.
- postDebugTask. Similar to preLaunchTask but this runs after the debug session.
- env. Here you can define environment variables.
"name": "mix phx.server",
You can define different kind of tasks in a file named
tasks.json inside folder
.vscode . In the case of Elixir project, you can define tasks to run different Mix commands.
For example, the following defines a task to run
mix test :
By the way, if you don’t know how to create a
tasks.json file you can run
Terminal->Configure Tasks... to generate a template of the file.
It is not perfect, but definitely, you can start with Visual Studio Code to learn and develop Elixir apps with the help of a debugger and code highlighting. Good luck.
If you know more extensions that can help with Elixir please comment.