JuliaDebug / JuliaInterpreter.jl

Interpreter for Julia code
Other
162 stars 34 forks source link

Change code location information for debugging (Pluto usecase) #582

Open disberd opened 1 year ago

disberd commented 1 year ago

Hi All,

I have a question that would be helpful for adding debugger support for Pluto and I thought this would be the best place to ask.

I have tried hooking a running Pluto notebook with an active VSCode instance for debugging purposes and it was actually easier than expected so I had created a small utility package for that here: https://github.com/disberd/PlutoVSCodeDebugger.jl

The debugging and breakpoint creations works very nicely when one wants to add breakpoint to functions that are not defined directly in the notebook (so functions that are either defined in imported modules or in directly included files)

The reason this is not very straightforward for code defined in the notebook is that Pluto itself modifies the LineNumberNodes of the code within the notebook to also include information about the notebook cell where the code is defined.

The format of this modified LNN is the following filename#==#cell_uuid:line_number_from_cell_start. So it basicaly changes the filename to add the cell uuid (separated by #==#) and modifies the line number by counting lines only from the start of the cell.

This is the reason why adding a breakpoint in the notebook file won't cause the debugger to stop, as the file actually tracked by the debugger is another (nonexistant) one.

For example, entering (e.g. with @enter) inside this function defined in the notebook: image

the debugger in VSCode will open a new non-existent file as follows: image

Pluto relies pretty heavily on this modified LNN to simplify error stacktraces within the notebook so I believe it would be very difficult to change that part of Pluto.

What I would like to know is whether there is a way to modify those LNN just for debugger purposes with some code that maybe exists either in JuliaInterpreter or CodeTracking. This could happen directly within custom @run or @enter macro specifically devised for Pluto which ideally would do something like this:

macro run(command)
# Clean LNNs of code defined inside Pluto by removing cell data from file name
# Execute the normal @run
# Put back the Pluto LNNs in the code defined in the notebook
end

Let me know if you have any idea on whether this is feasible or not without forcing requiring Pluto itself to change its internal LNN representation.

Alberto

timholy commented 1 year ago

Without looking into it myself, I can't really say. Note that CodeTracking does provide some support for functions defined in the REPL, which is conceptually a bit similar.