This is not particularly about my current problem, but more like in general. Sometimes I have a problem that only happens in production configuration, and I'd like to debug it there. What is the best way to approach that in Elixir? Production runs without a graphical environment (docker).
In dev I can use IEX.pry, but since mix is unavailable in production, that does not seem to be an option.
For Erlang https://stackoverflow.com/a/21413344/1561489 mentions dbg and redbug, but even if they can be used, I would need help on applying them to Elixir code.
Setting up the debugger for phoenix or elixir is done automatically for you by Visual Studio Code. When you open a phoenix or elixir project in Visual Studio Code, you should have a Run and Debugger option in your menu. If you don't see it, you can run a Show Run and Debug command with ctrl-shift-D .
First, start a local node running iex on your dev machine using iex -S mix
. If you don't want the application that's running locally to cause breakpoints to be activated, you need to disable the app from starting locally. To do this, you can simply comment out the application
function in mix.exs
or run iex -S mix run --no-start
.
Next, you need to connect to the remote node running on docker from iex on your dev node using Node.connect(:"remote@hostname")
. In order to do this, you have to make sure both the epmd and the node ports on the remote machine are reachable from your local node.
Finally, once your nodes are connected, from the local iex, run :debugger.start()
which opens the debugger with the GUI. Now in the local iex, run :int.ni(<Module you want to debug>)
and it will make the module visible to the debugger and you can go ahead and add breakpoints and start debugging.
You can find a tutorial with steps and screenshots here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With