tomblind / local-lua-debugger-vscode

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

Breakpoints don't work with TypeScriptToLua #23

Closed Keyslam closed 4 years ago

Keyslam commented 4 years ago

As the title states. Breakpoints do not work in .ts files when transpiling them with TSTL. Breakpoints in the transpiled Lua do work. I am also using the custom environment for LÖVE as it was described in the readme.

ts.config:

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["esnext"],
    "moduleResolution": "node",
    "types": ["lua-types/jit", "love-typescript-definitions"],
    "strict": true,
    "outDir": "build",
    "rootDir": "src",
    "baseUrl": "src",
    "sourceRoot": "src",
    "sourceMap": true,
  },
  "tstl": {
    "luaTarget": "JIT",
    "noHeader": true,
    "sourceMapTraceback": true,
  }
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Love",
      "type": "lua-local",
      "request": "launch",
      "program": {
        "command": "lovec"
      },
      "args": ["${workspaceFolder}/build"],
      "sourceRoot": "${workspaceFolder}/src",
      "verbose": true,
    }
  ]
}

output with verbose set to true

[request] launchRequest

[request] setBreakPointsRequest

[request] configurationDoneRequest

[info] launching `"lovec" C:\Users\Laptop-Justin\Documents\TSTL/build` from "c:\Users\Laptop-Justin\Documents\TSTL"

[info] process launched

[request] threadsRequest

[message] {"threadId":1,"breakType":"step","tag":"$luaDebug","type":"debugBreak","message":"step"}

[command] break clear

[message] {"type":"breakpoints","breakpoints":[],"tag":"$luaDebug"}

[command] break set c:\Users\Laptop-Justin\Documents\TSTL\src\init.ts:9

[message] {"type":"breakpoints","breakpoints":[{"file":"C:\\Users\\Laptop-Justin\\Documents\\TSTL\\src\\init.ts","enabled":true,"line":9}],"tag":"$luaDebug"}

[command] autocont
loaded

[info] debugging ended: 0
Keyslam commented 4 years ago

As requested: A log for when a breakpoint is set within Lua (init.lua. Line 4. Contained local a = 4)


[request] launchRequest

[request] setBreakPointsRequest

[request] configurationDoneRequest

[info] launching `"lovec" C:\Users\Laptop-Justin\Documents\TSTL-LOVE-Skeleton/build` from "c:\Users\Laptop-Justin\Documents\TSTL-LOVE-Skeleton"

[info] process launched

[request] threadsRequest

[message] {"threadId":1,"breakType":"step","tag":"$luaDebug","type":"debugBreak","message":"step"}

[command] break clear

[message] {"type":"breakpoints","breakpoints":[],"tag":"$luaDebug"}

[command] break set c:\Users\Laptop-Justin\Documents\TSTL-LOVE-Skeleton\build\init.lua:4

[message] {"type":"breakpoints","breakpoints":[{"file":"C:\\Users\\Laptop-Justin\\Documents\\TSTL-LOVE-Skeleton\\build\\init.lua","enabled":true,"line":4}],"tag":"$luaDebug"}

[command] autocont

[message] {"threadId":1,"breakType":"breakpoint","tag":"$luaDebug","type":"debugBreak","message":"breakpoint hit: \"C:\\Users\\Laptop-Justin\\Documents\\TSTL-LOVE-Skeleton\\build\\init.lua:4\""}

[request] threadsRequest

[command] threads

[message] {"type":"threads","threads":[{"active":true,"name":"main thread","id":1}],"tag":"$luaDebug"}

[request] stackTraceRequest 0/20 (thread 1)

[command] thread 1

[message] {"type":"stack","frames":[{"line":4,"active":true,"source":"init.lua"},{"line":-1,"source":"[C]","func":"require"},{"source":"main.lua","line":5},{"line":-1,"source":"[C]","func":"require"},{"source":"boot.lua","line":570},{"line":-1,"source":"[C]","func":"xpcall"},{"source":"boot.lua","line":787},{"line":-1,"source":"[C]","func":"xpcall"},{"source":"boot.lua","line":802}],"tag":"$luaDebug"}

[request] continueRequest

[command] cont

[request] terminateRequest

[info] debugging ended: 1
Straskal commented 4 years ago

I am able to debug and hit breakpoints in my typescript files using source mapping. Try adding require("lldebugger").start() at the beginning of your main transpiled lua file.

Edit: (Just realized that you had mentioned breakpoints in transpiled lua works for you, which means you are probably calling require("lldebugger").start() already.

tomblind commented 4 years ago

Just to track conversation from discord: This is likely a bug cause the combination of path options in the tsconfig. I'll investigate and hopefully get a fix in soon.

Keyslam commented 4 years ago

Additionally. I have made a repository in which breakpoints in TSTL do not work. It could be used to find out if the issue is caused by something else, and can also act as a 'stress test'. I'll note that the contents in the repository are tailored to my (windows) environment. Other things might be broken if it's copied directly.

https://github.com/Tjakka5/TSTL-LOVE-Skeleton/tree/b2ef0d165350bc2f00b181b8aaf330a79fd5d432

tomblind commented 4 years ago

I managed to duplicate your setup and get ts breakpoints working using cwd to run the love project from the build directory:

tsconfig.json

{
    "compilerOptions": {
        "target": "esnext",
        "lib": ["esnext"],
        "moduleResolution": "node",
        "types": ["lua-types/jit", "love-typescript-definitions"],
        "strict": true,
        "outDir": "build",
        "rootDir": "src",
        "baseUrl": "src",
        "sourceMap": true
    },
    "tstl": {
        "luaTarget": "JIT",
        "noHeader": true,
        "sourceMapTraceback": true
    }
}

Note the absence of sourceRoot - this will cause incorrect paths to be put in the map files

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Love",
            "type": "lua-local",
            "request": "launch",
            "program": {
                "command": "love"
            },
            "args": [
                "."
            ],
            "cwd": "build"
        }
    ]
}

test.ts

if (os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") === "1") {
    require("lldebugger").start();
}

love.load = () => {
    print("foobar");
};

The scriptRoots option which will be in the next release doesn't actually handle sourcemaps properly yet. I'll probably try to get that working before releasing. In the meantime, this cwd method should work. Let me know if it doesn't.

Keyslam commented 4 years ago

Thanks, that did the trick!

We discussed using cwd earlier, but it not working. What I didn't realize was that that that replaced the args option. I still had that set to ${workspaceFolder}/build

tomblind commented 4 years ago

I just pushed a new release that supports the scriptRoots option, which is the proper fix for this. See the example in the README for proper usage.