Skip to content

"Build on Save" seems broken #405

@AdamStone

Description

@AdamStone

I've been struggling to get "build on save" to work consistently. Initially I tried following these guides and got it working, but after awhile I noticed it was only showing syntax errors. Making some changes in build.zig seemed to fix it for awhile, then it stopped working again.

The two different build-on-save options in VSCode settings made this pretty confusing to troubleshoot. I figured that zig.zls.enableBuildOnSave corresponds to the guides I was following and that I should turn off zig.buildOnSave if using it, but when that wasn't working I also tried turning off zig.zls.enableBuildOnSave and enabling zig.buildOnSave. In both cases nothing would show up in the problems tab when saving even though running zig build check in terminal was producing build errors. So I resorted to debugging the extension to try to see what was happening. As far as I can tell the boolean logic in maybeDoBuildOnSave seems broken, but maybe I'm misunderstanding how it's supposed to work?
Here's what I'm seeing:

  1. maybeDoBuildOnSave is called whenever I save, but doCompile is never called even if I make nontrivial code changes.
  2. If it's the first save of the session, it bails because this.dirtyChange.has(document.uri) is false - the WeakMap is only populated after the first pass through here. Not a huge deal if subsequent saves still worked, but I would think it should work even on the first save.
  3. On subsequent saves it bails because this.dirtyChange.get(document.uri) !== document.isDirty is apparently always false.

Intellisense when I hover document.isDirty says it's "true if there are unpersisted changes." Since this is called right after saving changes, it seems to imply that document.isDirty would always be false when maybeDoBuildOnSave is called. However that implies the value in the WeakMap would also always be false, which means it would bail without compiling every time at step 3. And that is the behavior I'm seeing. Bypassing this check and letting it call doCompile seems to fix it - I start seeing the build errors in the problems tab.

The code seems to anticipate this being called twice on each save, in that case the checks could be meant to only trigger one build per two calls, but I'm only seeing it called once now. I wonder if something has changed with the timing since this code was written?

Some more details:

  • VSCode 1.97.2
  • vscode-zig 0.6.4
  • ZLS 13.0
  • Windows 10

Minimal repro:

  1. zig init in a new folder
  2. open in VSCode with the extension installed and zig.buildOnSave setting enabled
  3. in main.zig declare a fn that takes a u32 argument like fn example(x: u32) void { _ = x; }
  4. call it within main passing a float literal like example(12.34).
  5. zig build run in terminal fails with a build error but I never see it in Problems tab even after multiple changes and saves.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions