Skip to content

Instantly share code, notes, and snippets.

@pwbh
Forked from floooh/zig_test_debugging_vscode.md
Created September 2, 2024 16:41
Show Gist options
  • Save pwbh/883d9aff7cdffceb2c110f1df5c2aedf to your computer and use it in GitHub Desktop.
Save pwbh/883d9aff7cdffceb2c110f1df5c2aedf 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.

@pwbh
Copy link
Author

pwbh commented Sep 2, 2024

My current setup is a combination of the VSCode Zig extension (https://marketplace.visualstudio.com/items?itemName=ziglang.vscode-zig) and CodeLLDB with the following vanilla launch.json:

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

Debugging and breakpoints work (on macOS). Only bug I see is a debug line mismatch at the end of scope blocks. Sometimes the debugger appears to step through or stop at the last line of an if-else else branch even though that else branch isn't taken.

For tests I simply install them as artifact, so that an executable file is created in the file system, and simply build them during zig build:

https://github.com/floooh/chipz/blob/8dd374eb1e5605e1f86f0d3cd7206e8ddf1f29a5/tests/build.zig#L37-L56

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