I’m starting to use the new Zig 0.14.0 release and I have a certain amount of constant data that works really well being factored out into separate .zon files for each table. Almost everything is working great with that!
The one thing I haven’t got yet is a build step to format all the .zon files at once. (Nothing wrong with fmt-on-save in an editor, nothing wrong with individual ‘zig fmt myfile.zon’ calls, that’s just not what I’m looking for.)
I’ve got a “zig build fmt” build step that uses b.addFmt() to scan directories and format zig files. I specify a parent directory and it finds all the .zig files. I see that I can add each individual .zon file in the .paths, but can I set it up to find all my .zon files without naming each one?
The std.Build can get you access to the build_root which is a Directory struct. That has access to the actual fs.Dir which can let you walk the directory: Zig Documentation
So one option is to do something like the following:
const dir = b.build_root.handle;
var walker = try dir.walk(b.allocator);
while (try walker.next()) |file| {
if (file.kind != .file) continue;
// Check if file.basename ends in `.zon`
// Then add it for your addFmt call
}
I tested it before I asked. If you are saying you have verified it, then maybe something else was wrong with my test. Are you saying you have verified that arbitrary .zon files are formatted?
Oh, I see it. Need sleep. But I see it. I’ll look tomorrow for interesting differences in my example.
Sorry! Forgive me for being unclear in my first response and ensuing quick edit. I was half-asleep and should have waited 'til I wasn’t. Here’s what I wanted to say, with less confusion:
Yes, I now see that your message (the first one) clearly shows that “zig fmt .” formats arbitrary .zon files in subfolders.
Yes, I did verify it before I posted the question, but I saw a different result than you did. (And I wasn’t sure if it was supposed to work like that, I didn’t recall reading anything about it.)
Now that I know that it should have just worked, I will go retest it and see if I can reproduce it. I will report back.
At least I can revert to the simpler code with the &.{“.”} that I started with. (And I’ll probably never know exactly how I fooled myself to begin with. Something-something-editor buffer out of sync, or I was just ‘holding it wrong’.)