Skip to content

Instantly share code, notes, and snippets.

@floooh
Last active September 2, 2024 16:41
Show Gist options
  • Save floooh/31143278a0c0bae4f38b8722a8a98463 to your computer and use it in GitHub Desktop.
Save floooh/31143278a0c0bae4f38b8722a8a98463 to your computer and use it in GitHub Desktop.
How to debug Zig tests in VSCode

Tested on macOS:

  1. 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.
  2. 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.
  3. 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 args item):
{
    "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.

@mikymod
Copy link

mikymod commented Aug 28, 2024

@DayBr3ak maybe it's because you are on windows. Replace lldb with cppvsdbg

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppvsdbg",
            "request": "launch",
            "name": "Debug test",
            "program": "${workspaceFolder}/zig-out/bin/${fileBasenameNoExtension}-test",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "test",
        }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment