ZigBrains icon indicating copy to clipboard operation
ZigBrains copied to clipboard

Add support for debugging tests launched from Zig Build

Open PasVegan opened this issue 1 year ago • 9 comments

It would be nice to be able to debug tests.

PasVegan avatar Nov 11 '24 10:11 PasVegan

Debugging single-file tests is already supported. image

FalsePattern avatar Nov 11 '24 11:11 FalsePattern

If you mean debugging tests launched using zig build test, it is unfortunately too complicated for me to implement for the foreseeable future, but I'm open for contributions.

FalsePattern avatar Nov 11 '24 11:11 FalsePattern

If you mean debugging tests launched using zig build test, it is unfortunately too complicated for me to implement for the foreseeable future, but I'm open for contributions.

Yes that's what I meant, if I have some time I will try to help 👍🏻

PasVegan avatar Nov 11 '24 12:11 PasVegan

You can debug single file test only if the test in question does not contain dependencies. It's restriction of 'zig test' - it does not use build system.

In order to debud tests, I

g41797 avatar Dec 09 '24 13:12 g41797

If you mean debugging tests launched using zig build test, it is unfortunately too complicated for me to implement for the foreseeable future, but I'm open for contributions.

@FalsePattern Can you please explain why it is too complicated. I am asking to know where to start too help you and tackle this feature.

Bastczuak avatar Mar 27 '25 14:03 Bastczuak

The main issue comes from the fact that inspecting task outputs for finding test binaries requires a custom build runner, at that would be a large maintenance burden.

In the latest version of zigbrains, you can do it semi-manually though, by creating an install step for the test binary, and then setting up the task for it.

Example for the zig init template repo for zig 0.14.0:

Add this to the end of build.zig's build function:

    const test_artifact = b.addInstallArtifact(
        exe_unit_tests,
        .{ .dest_dir = .{ .override = .{ .custom = "tests" } } },
    );
    const install_test_step = b.step("install_test", "Create test binaries for debugging");
    install_test_step.dependOn(&test_artifact.step);

This generates the following file when run: Image

Next, create a new Zig build intellij run configuration, and set it up like so: Image

And when you debug this configuration, the test executable generated by the install_test task gets sent to gdb correctly: Image

FalsePattern avatar Mar 27 '25 17:03 FalsePattern

Only works for single-executable tests though, so if you have multiple b.addTest(...) tests, you need to set up an install step + intellij run config for each

FalsePattern avatar Mar 27 '25 17:03 FalsePattern

Image

aohanhongzhi avatar May 23 '25 04:05 aohanhongzhi

@aohanhongzhi scroll up, there's a tutorial on how to modify your build.zig to support debugging tests

FalsePattern avatar May 23 '25 10:05 FalsePattern