tomblind / local-lua-debugger-vscode

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

Relative file paths #11

Closed Straskal closed 4 years ago

Straskal commented 4 years ago

Hi there!

I'm having issues regarding relative file paths for assets (images) and require paths (other lua files).

I have my launch.json configured to use the lua53.exe interpreter and main.lua as my entry file.

Folder structure:

main.lua is not able to find imageLoader.lua via require()

I bundled the two files together to avoid require(), but then my program is unable to find the image.

Any help or work arounds would be greatly appreciated!

Thank you.

astrochili commented 4 years ago

I have the same problem on macOS, can't require local lua modules.

I'm looking forward to the solution, because this is the only debugger that really works 🥳. Nothing else works on my mac 😟.

Screenshot

astrochili commented 4 years ago

Found a dirty and hacky solution. I created the separated debug.lua file that I run with debugger:

debug.vscode = os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1"

if debug.vscode then
    local separator = string.sub(package.config, 1, 1)
    local filePath = debug.getinfo(1).short_src
    local rootFolder = string.gsub(filePath, "^(.+"..separator..")[^"..separator.."]+$", "%1");
    package.path = rootFolder .. [[?.lua]]
end

dofile("main.lua")
Straskal commented 4 years ago

That work around works perfectly for now. @astrochili Thanks!

tomblind commented 4 years ago

Have you tried setting cwd in your launch.json?

    "configurations": [
        {
            "name": "Debug",
            "type": "lua-local",
            "request": "launch",
            "program": {
                "lua": "lua53.exe",
                "file": "main.lua"
            },
            "cwd": "${workspaceFolder}"
        }
    ]
astrochili commented 4 years ago

Have you tried setting cwd in your launch.json?

It doesn't help me, a similar error.

astrochili commented 4 years ago

Another problem is the luarocks libraries finding.

For example, luarocks installed the lume module to /usr/local/var/homebrew/linked/luarocks/share/lua/5.3/lume.lua. And the code require("lume") works perfectly with Lua in the terminal.

But the debugger tries to find modules in this short list of paths:

no field package.preload['lume']
no file '/Users/.../.vscode/extensions/tomblind.local-lua-debugger-vscode-0.1.3/debugger/lume.lua'
no file '/usr/local/lib/lua/5.3/lume.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './lume.so'

It looks like the debugger doesn't check the paths list that defined in the system for succesful modules finding.

tomblind commented 4 years ago

You're on OSX, right? It seems environment variables like LUA_PATH aren't passed to the child process in OSX (see # 5 #7). I don't have a Mac right now so I'll need to find one to debug this issue.

tomblind commented 4 years ago

Thinking about it, this issue must be different since the debugger script itself is being found.

astrochili commented 4 years ago

You're on OSX, right?

Yes. Feel free to ask any questions or test tasks, maybe I can help you.

astrochili commented 4 years ago

Wow. Found a more clean solution in #10 that solves all my path finding problems. Just add this to debug configutation:

"env": {
  "LUA_PATH": "./?.lua;[ADDITIONAL_PATHS]"
}

where [ADDITIONAL_PATHS] in my case is all that luarocks path terminal command returns. The final configuration looks like this:

{
    "configurations": [
        {
            "name": "Debug",
            "type": "lua-local",
            "request": "launch",
            "program": {
              "lua": "lua5.3",
              "file": "${workspaceFolder}/main.lua"
            },
            "env": {
                "LUA_PATH": "./?.lua;/usr/local/Cellar/luarocks/3.2.1/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?.lua;/usr/local/share/lua/5.3/?/init.lua;/usr/local/lib/lua/5.3/?.lua;/usr/local/lib/lua/5.3/?/init.lua;./?.lua;./?/init.lua;/Users/username/.luarocks/share/lua/5.3/?.lua;/Users/username/.luarocks/share/lua/5.3/?/init.lua"
            }
        }
    ]
}

Now I don't have any problems with local and external lua modules requiring and I don't need to create a separated module for debugger running.

tomblind commented 4 years ago

It looks like LUA_PATH is not being passed to the debug adapter from vscode. Will you try this in your launch.json and see if it works?

"env": {
    "LUA_PATH": "${env:LUA_PATH}"
}
astrochili commented 4 years ago
tomblind commented 4 years ago

Thanks for the exhaustive testing! 😄

It looks like LUA_PATH isn't being used to set package.path. If you don't mind, will you try testing with LUA_PATH_5_3? It's possible that's what your setup is using instead of LUA_PATH.

astrochili commented 4 years ago

Nope, the same situation:

tomblind commented 4 years ago

In the terminal of vscode or an external one? There should be something in one of those two env variables, because that's where package.path is set from.

astrochili commented 4 years ago

I mean the external system terminal app, not VSCode.

With Lua in the terminal:

With debugger in VSCode:

tomblind commented 4 years ago

Very strange, Lua must be getting its path from somewhere other than the standard env vars.

tomblind commented 4 years ago

On the terminal, can you type declare and see if any env vars reference those paths?

astrochili commented 4 years ago

I typed declare and there are no matches for those paths and nothing with word lua 🤔.

tomblind commented 4 years ago

Well now I'm very confused 😆 It's got to be getting those paths from somewhere.

astrochili commented 4 years ago

Look at this, sounds like Lua get default paths from luaconf.h. And that seems real because it has LUA_PATH_DEFAULT variable with my paths.

tomblind commented 4 years ago

Ah yeah, that's where it's getting the defaults. But how does it find the luarocks modules? You said before those work when running from terminal, right?

astrochili commented 4 years ago

Yeah, it's simple because luarocks installs modules in these paths. For example luarocks module lume has a path /usr/local/share/lua/5.3/lume.lua. I don't know why luarocks path returns such long list of paths but I don't need them, as we see. The default list of paths from luaconf.h is enough.

tomblind commented 4 years ago

Ahhh. Ok. Now it all makes sense 😄

astrochili commented 4 years ago

Yes, in short, the problem is that the debugger doesn't read the standard lua paths from luaconf.h. I don’t know what about LUA_PATH, because I don’t have it, maybe it doesn’t work too.

Straskal commented 4 years ago

"cwd": "${workspaceFolder}" definitely helped with loading assets. Thanks!

And it looks like everything's working! Awesome.