Debugging tests with vscode

I’m trying to find a decent setup for debugging tests using either the zig build test command or the zig test filename.zig command.

I do have debugging working for the main exe on windows using the following vscode config:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Application",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/zig-out/bin/Application.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/zig-out/bin/",
            "environment": []
        }
    ]
}

I have noticed the test.exe file gets build everytime to the zig-cache/o/hash dirs but it’s quite cumbersome to add an extra config and change the program path each time, also it’s unclear what exe I need to pick as there can be multiple test.exe files.

Has anyone else run into this issue and find a simple solution?

I’m using the nightly build of zig btw, if that matters.

I can use vscode-llvm and Tasks Shell Input extension to resolve this issue.
The following is this link. (written by Japanese, sorry…)

1 Like

Hi,

Here’s my setup, roughly, you might need some adjustments as this is for linux.

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "debug_ut",
            "program": "./zig-out/bin/ut",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "build_ut",
        },
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build_ut",
            "type": "shell",
            "command": "zig test -femit-bin=zig-out/bin/ut --test-no-exec src/ut.zig",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
}

The magic is in the extra args to zig test, which prevents the test execution during the build step, and also emit an executable I can launch and debug.

also it’s unclear what exe I need to pick as there can be multiple test.exe files.

Here, I’m compiling tests for src/ut.zig, which includes the other files I want to test, alternatively, you can copy paste the blocks in tasks.json/launch.json to instead build/debug tests in a specific file.

My ut.zig looks like:

comptime {
    _ = @import("file1.zig");
    _ = @import("file2.zig");
    // ...
}
4 Likes

Thanks, this seems like a good solution! will try it

Found one small improvement: by using the following inputs for your task:

"inputs": [
        {
          "type": "promptString",
          "id": "testFile",
          "description": "Specific file to test",
          "default": "src/main.zig"
        }
    ]

you will be prompted to give a filename that than will be used, this way you don’t need to change your tasks.json file each time you want to test something else

2 Likes