Zig NEWS

Discussion on: Setting up Visual Studio Code for writing Zig

Collapse
 
batiati profile image
Rafael Batiati

Very nice post! I would love to know more about setting up VSCode for debuging Zig with gdb/lldb.

Collapse
 
kristoff profile image
Loris Cro

I've seen somebody make this work, it required adding a tiny little bit of scripting to vscode (mainly to know where the test executable gets saved) and then it would work very easily, IIRC.

Collapse
 
guidorice profile image
guidorice • Edited

I am using Zig + VSCode on MacOS with this LLDB extension vadimcn.vscode-lldb.
marketplace.visualstudio.com/items...
Some things are a bit unintuitive, like const values are displayed as <no location, value may have been optimized out>, but it basically seems to work.

One thing I have not solved though, is how to run the debugger on tests. How does one generate a test executable for debugging? Basically want to be able to launch an executable that is that same as: zig build test

Also found this article but YMMV, I didn't have any success with that other debugger.
dev.to/watzon/debugging-zig-with-v...

@kristoff @batiati

Thread Thread
 
guidorice profile image
guidorice • Edited

Figured it out: to run the test executable in a debugger, first run zig build test, then a test executable will be in the cache dir:

$ find zig-cache -name test
zig-cache/o/31202ceebbfdf2cf8ee2be9565e64183/test
Enter fullscreen mode Exit fullscreen mode

An example launch.json using the CodeLLDB extension:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug test runner",
            "program": "${workspaceFolder}/zig-cache/o/31202ceebbfdf2cf8ee2be9565e64183/test",
            "args": ["src/main.zig"],
            "cwd": "${workspaceFolder}"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Now try hitting some breakpoints in your tests- it works for me.

Thread Thread
 
drew profile image
Drew

Tried this today, but there's nothing in there called test. Have you found a solution that works with 0.11?

Collapse
 
fabioarnold profile image
Fabio Arnold

You can use Microsoft's C++ extension for debugging. When you hit F5 (Launch) in VS Code a wizard should pop up to guide you in creating a launch.json file for your project. Here's an old post of mine on how to do that on Windows: dev.to/fabioarnold/setup-zig-for-g...

One more exception: macOS arm64 isn't yet supported by Microsoft's debugging extension. I found CodeLLDB to be a good replacement.