kidp330

kidp330

Debugging a single .exs without mix.exs

I’m trying to run a script with Mix.install inside, and set some breakpoints. I’m running VSCode with the ElixirLS extension. I can run the script just fine if I mix run --no-mix-exs file.exs but not with the debugger, even If I add the flag to taskArgs. Haven’t found this functionality doccumented in the extension docs so either I’m doing something wrong or it’s not implemented?

Marked As Solved

lukaszsamson

lukaszsamson

ElixirLS Core Team

By debugger do you mean dbg

No @sodapopcan, OP means ElixirLS debug adapter (GitHub - elixir-lsp/vscode-elixir-ls: Elixir language support and debugger for VS Code, powered by ElixirLS.)

Haven’t found this functionality doccumented in the extension docs so either I’m doing something wrong or it’s not implemented?

@kidp330 ElixirLS debug adapter currently requires a mix project (I opened Debug adapter does not support no mixfile projects · Issue #1037 · elixir-lsp/elixir-ls · GitHub for tracking). Trying to run in a folder that does not contain mix.exs will raise Mix.NoMixProjectError. You can create a dummy project and run your scripts from there.

However this is tricky in case of .exs files.

  1. OTP debugger which ElixirLS uses underneath requires modules to be interpreted before you can set breakpoints in them. So you need to put your code in module functions
  2. .exs files are compiled in memory. To interpret them you need to include them in requireFiles so they are purged, recompiled and their beam files saved to disk so the OTP debugger can interpret them
  3. .exs script starts executing when you compile it not giving the debugger required time to interpret and set breakpoints. It’s not possible to purge a module when a process executes it’s code. The reasult is that interpretation happens when the script has already returned. You can overcome this with invoking your code in a separate process started from the script
  4. mix run exits when the script return. By default debugger will end the session when mix task finishes. You need to set exitAfterTaskReturns to false so the debug session will continue

I was able to create a proof of concept. The debugger breaks when the breakpoint in Abc.debug_me is hit from the task I start. I needed to add some delay though. Otherwise the code finished before the breakpoint was set.

poc.exs

defmodule Abc do
  def debug_me() do
    a = [1, 2, 3]
    b = Enum.map(a, & &1 + 1)
    b
  end
end

a = [1, 2, 3]
b = Enum.map(a, & &1 + 1)
IO.puts("done #{inspect(b)}, #{Abc.debug_me()}")

Task.start(fn ->
  Process.sleep(4000)
  IO.puts("done from task #{inspect(b)}, #{Abc.debug_me()}")
end)

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "mix_task",
      "name": "mix run",
      "request": "launch",
      "task": "run",
      "taskArgs": [
        "--no-mix-exs",
        "poc.exs"
      ],
      "startApps": false,
      "projectDir": "${workspaceRoot}",
      "exitAfterTaskReturns": false,
      "requireFiles": [
        "poc.exs"
      ]
    }
  ]
}

The other possibility is breaking on Kernel.dbg macro. This is easier to achieve. In this case do not put the script into requireFiles as recompilation and interpreting will deadlock the debugger

Also Liked

lukaszsamson

lukaszsamson

ElixirLS Core Team

ElixirLS Debug adapter will support --no-mix-exs in 0.19 release

sodapopcan

sodapopcan

I don’t have a direct solution but figured I’d respond since you haven’t gotten any bites yet.

By debugger do you mean dbg? If so then I’m not sure this is possible since in order for it to be available, you need to add :runtime_tools to :extra_applications in a mix file. I’m sorry I don’t know the answer but maybe that’s a good enough clue?

…and welcome to Elixir Forum! :slight_smile:

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement