tomblind / local-lua-debugger-vscode

Local Lua Debugger for VSCode
MIT License
106 stars 26 forks source link

Breakpoints without source maps failing... #40

Closed thejustinwalsh closed 2 years ago

thejustinwalsh commented 3 years ago

re: https://github.com/ts-defold/defold-lldebugger/tree/extension

When debugging in Defold and setting breakpoints in .lua, or .script files, the debugger skips right past them. Interestingly enough, when using sourcempas and putting breakpoints in *.ts files, the debugger respects the breakpoints.

Defold has various names for it's lua script files, .gui_script, .script, as well as using *.lua for module files.

This was all being tested on macOS. Let me know how else I can help, and if the project is working for you at all.

Attached is a fully configured and built project, that you should be able to execute directly from vscode (without even installing defold ;))

Basic 3D project.zip

tomblind commented 3 years ago

Thanks for posting this. It looks like in defold, the debug facilities see scripts as existing at absolute paths within the project hierarchy (so /main/main.script instead of main/main.script or ./main/main.script) and this confuses the debugger. I'd like to try a few things, but it looks like the debugger is pre-compiled into the project as a luac file. Would it be possible to dynamically load the debugger script somehow?

thejustinwalsh commented 3 years ago

Would it be possible to dynamically load the debugger script somehow?

You could potentially use the same cached module hack I use to get Defold to load the lldebugger script dynamically, or create a native extension that overrides, or replaces it.

It looks like in defold, the debug facilities see scripts as existing at absolute paths within the project hierarchy (so /main/main.script instead of main/main.script or ./main/main.script) and this confuses the debugger.

That makes sense as the scripts are actually contained in an archive at runtime and Defold overrides the loader to read the scripts from that archive file where the paths would be rooted absolute.

Another interesting piece is when you include extensions / libraries, the source doesn't even exist on the file system directly, it is in another archive and loaded directly from that archive, so if you where to try and step into a library script to debug, I don't even know what would happen.

thejustinwalsh commented 3 years ago

Would it be reasonable to allow for a callback function to be registered when you load the lldebugger module in lua that hints / assists in remapping the paths and something like that. I understand that Defold is a pretty custom integration of lua and maybe a mechanism like that would help other projects that load scripts in even wilder ways work?

Like a path mapping middleware from in memory archive to where that thing might live on disk essentially.

I'll get spun up on debugging vscode extensions so I can be more useful as well in the interim.

tomblind commented 3 years ago

You could potentially use the same cached module hack I use to get Defold to load the lldebugger script dynamically

This is what I was hoping. But main.script is also precompiled so I can't just modify that. Can I recompile those myself with lua5.1, or do I need to use defold?

Another interesting piece is when you include extensions / libraries, the source doesn't even exist on the file system directly, it is in another archive and loaded directly from that archive, so if you where to try and step into a library script to debug, I don't even know what would happen.

Without source the debugger will either skip those calls or step through them while showing a greyed-out callstack. I'm not sure which (this is mostly controlled by vscode).

Would it be reasonable to allow for a callback function to be registered when you load the lldebugger module in lua that hints / assists in remapping the paths and something like that.

This is certainly a possible feature that could be added, but it shouldn't be necessary in this case. The scriptRoots option was made to solve this exact issue and the fact it's not working for this situation is a bug.

thejustinwalsh commented 3 years ago

This is what I was hoping. But main.script is also precompiled so I can't just modify that. Can I recompile those myself with lua5.1, or do I need to use defold?

Oh, I see, yeah that was compiled by Defold, uses all kinds of custom lua functions / user data exposed by Defold. The game.project file is what you would open in Defold and then just select Build. If this if going too deep I would be happy to try out anything you want me to test, log, etc as well.

Without source the debugger will either skip those calls or step through them while showing a greyed-out callstack. I'm not sure which (this is mostly controlled by vscode).

Seems reasonable.

This is certainly a possible feature that could be added, but it shouldn't be necessary in this case. The scriptRoots option was made to solve this exact issue and the fact it's not working for this situation is a bug.

True. Scripts roots does indeed seem like the right move for pathing, over a plugin.

tomblind commented 2 years ago

I've committed a fix for the breakpoints and scriptRoots not working (at least it works on the provided example).

Note that scriptRoots must be set for vscode to find the files properly (ex. scriptRoots: ["."]), otherwise your breakpoints will hit but vscode won't actually open the file and goto the line.

thejustinwalsh commented 2 years ago

Ahhhhh, scriptRoots: ["."]. Thanks again for all your hard work!