Tested on macOS:
- Install the CodeLLDB VSCode extension. Unlike the debugger in the C/C++ extension, this allows to set breakpoints inside Zig "test" blocks (in the MS C/C++ extension debugger, breakpoints inside test blocks will be disabled once the debugger starts for unknown reasons.
- When compiling the test, tell it to also emit a binary:
zig test -femit-bin=zig-out/bin/my-test src/bla.zig, otherwise there will be no executable to debug. - The compiled test executable expects the path to the Zig executable as first command line argument, the launch.json file
needs to be setup accordingly (note the
argsitem):
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/zig-out/bin/my-test",
"args": ["/usr/local/bin/zig"],
"cwd": "${workspaceFolder}",
}
]
}That should be all, now set breakpoints inside tests and start the debugger with F5.
I found that the above solution runs tests twice. I was able to fix this with some slight modifications:
launch.json{ "version": "0.2.0", "configurations": [ { "type": "lldb", "request": "launch", "name": "Debug test", "program": "${workspaceFolder}/zig-out/bin/${fileBasenameNoExtension}-test", "args": [], "cwd": "${workspaceFolder}", "preLaunchTask": "test", } ] }tasks.json{ "version": "2.0.0", "tasks": [ { "label": "test", "type": "shell", "command": "zig", "args": [ "test", "-femit-bin=zig-out/bin/${fileBasenameNoExtension}-test", "--test-no-exec", "${relativeFile}", ] } ] }Notice the additional argument
--test-no-exec. This will cause the test runner to emit the binary test file without actually running the tests.