tomblind / local-lua-debugger-vscode

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

How to specify working directory to find require'd modules #10

Closed michael-uman closed 4 years ago

michael-uman commented 4 years ago

Here is a script I am trying to debug and I encounter an error when stepping over the require line:

-- lesson6 - Michael Uman - Exercise OO programming

-- Added to support local-lua debugger
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
    require("lldebugger").start()
end

local r = require("car")

local cars = {
    Car:new("Ford",         "Mustang GT",   2019),
    Car:new("Toyota",       "Camry",        2012),
    Car:new("Chevrolet",    "Camero",       2018),
    Car:new("Dodge",        "Challenger",   2015),
}

-- Sort table by year
table.sort(cars, function (a,b) return a:year() < b:year() end)

for key, value in pairs(cars) do
    value:print();
end

If I execute this code in VSCode local-lua plugin I get the following message when stepping over the require "car" line:

Exception has occurred.
lesson6.lua:5: module 'car' not found:
    no field package.preload['car']
    no file '/home/muman/.vscode-server/extensions/tomblind.local-lua-debugger-vscode-0.1.3/debugger/car.lua'
    no file '/usr/local/lib/lua/5.2/car.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.2/car.so'
    no file '/usr/lib/lua/5.2/car.so'
    no file '/usr/local/lib/lua/5.2/loadall.so'
    no file './car.so'

This code executes fine in my WSL environment with the following output:

muman@DESKTOP-BSPUQUB:~/lua-stuff$ lua lesson6.lua 
2012 Toyota       - Camry       
2015 Dodge        - Challenger  
2018 Chevrolet    - Camero      
2019 Ford         - Mustang GT  

My launch.json script is reproduced below:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "lesson6",
            "type": "lua-local",
            "request": "launch",
            "stopOnEntry": true,
            "cwd": "${workspaceFolder}",
            "program": {
                "lua": "lua",
                "file": "${workspaceFolder}/lesson6.lua"
              }
        }
    ]
}

I have tried setting the environment LUA_PATH but can't seem to get it to work.

Is there a solution to this issue?

Thank you, Michael Uman Sr. Software Engineer Wunder-Bar Inc.

tomblind commented 4 years ago

How did you set LUA_PATH? If you set it in the debugger configuration, it should be passed to the program:

{
    "configurations": [
        {
            "name": "lesson6",
            "type": "lua-local",
            "request": "launch",
            "stopOnEntry": true,
            "cwd": "${workspaceFolder}",
            "program": {
                "lua": "lua",
                "file": "${workspaceFolder}/lesson6.lua"
            },
            "environment": {
                "LUA_PATH": "./?.lua"
            }
        }
    ]
}
michael-uman commented 4 years ago

Hello Tom,

Thank you for your prompt reply. You are mostly correct except for the name of the field. It is not 'environment' which needs to be set to ./?.lua but rather env.

I am using VSCode which identifies itself as:

Version: 1.40.1 (system setup)
Commit: 8795a9889db74563ddd43eb0a897a2384129a619
Date: 2019-11-13T16:49:35.976Z
Electron: 6.1.2
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Windows_NT x64 10.0.18362

I am using WSL lua version 'Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio'.

Here is the launch.json which works for me now:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "lesson6",
            "type": "lua-local",
            "request": "launch",
            "stopOnEntry": true,
            "cwd": "${workspaceFolder}",
            "program": {
                "lua": "lua",
                "file": "${workspaceFolder}/lesson6.lua"
             },
             "env": {
                 "LUA_PATH": "./?.lua"
             },
        }
    ]
}

Thanks for your support. I am just getting started with Lua for an embedded Lua project and appreciate being able to debug scripts. So far this debug plugin has worked excellently.

Thank you, Michael Uman

tomblind commented 4 years ago

Oops! Sorry I about the wrong name - glad you were able to figure it out.