Skip to content

Instantly share code, notes, and snippets.

@Tomboyo
Last active October 28, 2024 14:11
Show Gist options
  • Save Tomboyo/ef8db1ed6beb2a88a8d5fb1d7ff3d76b to your computer and use it in GitHub Desktop.
Save Tomboyo/ef8db1ed6beb2a88a8d5fb1d7ff3d76b to your computer and use it in GitHub Desktop.
Interactive Elixir Debugging with VSCode and ElixirLS

In this Gist we will configure VSCode for Elixir debugging. Once done, we will be able to debug any function with any arguments by opening the VSCode Run (a.k.a Debug) view, selecting the "mix run" debug configuration, and entering any invocation such as Example.run(:my_args) into a command palette prompt. This will let us step through the code as it executes and use breakpoints like normal.

This Gist expects you have the ElixirLS: Elixir Support and Debugger extension installed. This Gist is based on version 0.6.2.

1. Create a launch configuration

Create a .vscode/launch.json file at the root of your project if one does not exist already. Modify the file to contain inputs and configurations like the following:

{
    "version": "0.2.0",
    "inputs": [
        // This input allows us to prompt the VSCode user for arguments when we run a debug configuration.
        {
            "id": "runArgs",
            "type": "promptString",
            "description": "Enter arguments for `mix run -e`"
        }
    ],
    "configurations": [
        // This configuration runs `mix run -e ...` with arguments supplied by the user.
        {
            "type": "mix_task",
            "name": "mix run",
            "request": "launch",
            "task": "run",
            // Prompt the VSCode user for arguments with `"${input:runArgs}` and pass those along to `mix run -e ...`
            "taskArgs": [
                "-e", "${input:runArgs}"
            ],
            "startApps": true,
            "projectDir": "${workspaceRoot}",
        }
    ]
}

2. That is it!

That launch configuration will let you run any function with any arguments. Define some breakpoints in the function of interest, then use the mix run debug configuration from the VSCode Run window to exercise it.

Enjoy!

@dimpdash
Copy link

This only partially works for me. When I add a breakpoint and enter a command I get an error:

"debugged process #PID<0.179.0> exited with reason no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started"

However when starting the debugging breakpoints work.

The log points in vscode seem to still work though.

I've tried windows and wsl and both have the same issue.

Tested on this sample repo here
https://github.com/BrooklinJazz/debugging_elixir

@user20230119
Copy link

user20230119 commented Aug 30, 2023

https://medium.com/geekculture/debugging-phoenix-and-elixir-applications-in-visual-studio-code-5f1b63a6713b says:

If you experience the same issue, make sure you have an application.ex file in /lib.

defmodule DebugApp do
  use Application
  def start(_, _) do
    YourModule.your_function()
    {:ok, self()}
  end
end

Then reference the file in mix.exs.

  def application do
    [
      extra_applications: [:logger],
      mod: {DebugApp, []}
    ]
  end

@Mane087
Copy link

Mane087 commented Aug 27, 2024

Other option is this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "mix_task",
            "request": "launch",
            "name": "Mix Task: iex -S mix phx.server",
            "task": "iex -S mix phx.server",
            "projectDir": "${workspaceFolder}",
            "requireFiles": [
                "lib/**/*.ex",
                "test/**/*.exs"
            ]
        }
    ]
}

this configuration use iex for debugging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment