How to get more out of Zig ZLS with VSCode?

As I’ve been coding through the AoC entries, I’m finding myself going back and forth between the terminal to run zig build run and VSCode only to see that most of the time, zig build run outputs errors even though VSCode doesn’t indicate any errors with ZLS.

For example:

pub fn perimeter(map: []const []const u8) void {
    _ = map;
}

fn minitests() !void {
    const map = [_][]const u8{
        "OOOOO",
        "OXOXO",
        "OOOOO",
        "OXOXO",
        "OOOOO",
    };
    perimeter(map);
}

The perimeter(map) should be perimeter(&map) but nothing in VSCode indicates that there’s an error on that line.

However, if I typo the map like “xmap”, I do get a red squiggle like in the below screenshot.

Am I somehow holding this tool wrong… why does it most of the time not give me the errors that I get when I run zig build?

ZLS has an option to build on save: Build-On-Save - zigtools
I believe this feature is off by default. When you enable this, it will run the build and get you more information.

You can also check out Loris’ blog on how to set up a project to get the most out of ZLS: Improving Your Zig Language Server Experience | Loris Cro's Blog

3 Likes

Thanks! I had tried setting the two:

image
image

But I don’t see that this had any effect on anything. I’m on Windows, I wonder if that’s somehow not working as well as other platforms.

Many other features are working though, like ast syntax check, autoformatting and autofixing.

I’ll check Loris’ blog and/or zls repo if there are any further hints.

I wish I had known about this zigtools.org web site! Filed issue github.com/ziglang/vscode-zig/issues/261 that the VSCode extension docs should link back to the zigtools site.

Whoahh!! This build-on-save is SO MUCH better than what I’ve ever had in my Zig VSCode experience. IMO this should be the primary way to use it and all the docs should point to this method as the first thing to try! This might be a game changer for my Zig editing ergonomics.

FWIW, the reason why VSCode settings for “Enable Build on Save” did nothing was because I did not have a “check” step in my build.zig. The option help strings could make this requirement a little more obvious, I guess.

Thanks for the help @Southporter!

2 Likes