From 6d1e70e242de48672d0c9c2a8267a4851e1c4d56 Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 16 Jul 2025 11:27:26 -0400 Subject: [PATCH 1/2] feat: format build.zig --- build.zig | 4 +++- src/cmd_sync.zig | 2 +- src/run_zig.zig | 17 +++++++++++++++++ src/sync_build_file.zig | 19 ++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index ac13729..af27020 100644 --- a/build.zig +++ b/build.zig @@ -18,7 +18,9 @@ pub fn build(b: *std.Build) void { .root_module = module_zbuild, }); - const install_exe_zbuild = b.addInstallArtifact(exe_zbuild, .{}); + const install_exe_zbuild = b.addInstallArtifact(exe_zbuild, .{ + }); + const tls_install_exe_zbuild = b.step("build-exe:zbuild", "Install the zbuild executable"); tls_install_exe_zbuild.dependOn(&install_exe_zbuild.step); b.getInstallStep().dependOn(&install_exe_zbuild.step); diff --git a/src/cmd_sync.zig b/src/cmd_sync.zig index fed8dce..a8d1c5e 100644 --- a/src/cmd_sync.zig +++ b/src/cmd_sync.zig @@ -11,6 +11,6 @@ pub fn exec(gpa: Allocator, arena: Allocator, global_opts: GlobalOptions, config if (global_opts.no_sync) { fatal("--no-sync is incompatible with the sync command", .{}); } - try syncBuildFile(gpa, arena, config, .{ .out_dir = global_opts.project_dir }); + try syncBuildFile(gpa, arena, config, global_opts, .{ .out_dir = global_opts.project_dir }); try syncManifest(gpa, arena, global_opts, config, .{ .out_dir = global_opts.project_dir }); } diff --git a/src/run_zig.zig b/src/run_zig.zig index 5ee85d0..6fe5286 100644 --- a/src/run_zig.zig +++ b/src/run_zig.zig @@ -23,9 +23,18 @@ pub fn runZigFetch(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env: }); } +pub fn runZigFmt(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env: ZigEnv, args: []const []const u8) !void { + try runZig(gpa, arena, child_opts, env, .{ + .fmt = .{ + .args = args, + }, + }); +} + pub const ZigCmd = union(enum) { build: Build, fetch: Fetch, + fmt: Fmt, pub const Build = struct { step: ?[]const u8, @@ -42,6 +51,10 @@ pub const ZigCmd = union(enum) { exact: ?[]const u8, }; }; + + pub const Fmt = struct { + args: []const []const u8, + }; }; pub const ChildOpts = struct { @@ -87,6 +100,10 @@ pub fn runZig(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env: ZigE }, } }, + .fmt => |fmt| { + try argv.append("fmt"); + try argv.appendSlice(fmt.args); + }, } var child = std.process.Child.init(argv.items, gpa); diff --git a/src/sync_build_file.zig b/src/sync_build_file.zig index c66824d..bf20181 100644 --- a/src/sync_build_file.zig +++ b/src/sync_build_file.zig @@ -3,6 +3,8 @@ const Allocator = std.mem.Allocator; const Config = @import("Config.zig"); const ConfigBuildgen = @import("ConfigBuildgen.zig"); +const GlobalOptions = @import("GlobalOptions.zig"); +const runZigFmt = @import("run_zig.zig").runZigFmt; pub const SyncBuildFileOpts = struct { out_dir: ?[]const u8 = null, @@ -11,17 +13,28 @@ pub const SyncBuildFileOpts = struct { const max_bytes_zbuild_file = 16_000; -pub fn syncBuildFile(gpa: Allocator, arena: Allocator, config: Config, opts: SyncBuildFileOpts) !void { - _ = gpa; +pub fn syncBuildFile(gpa: Allocator, arena: Allocator, config: Config, global_opts: GlobalOptions, opts: SyncBuildFileOpts) !void { const build_root_directory = std.fs.cwd(); const out_dir = if (opts.out_dir) |o| try build_root_directory.openDir(o, .{}) else build_root_directory; const out_file = try out_dir.createFile(opts.build_file orelse "build.zig", .{ .truncate = true }); - defer out_file.close(); + errdefer out_file.close(); const writer = out_file.writer().any(); var buildgen = ConfigBuildgen.init(arena, config, writer); defer buildgen.deinit(); try buildgen.write(); + + // after writing, close the file and format it with `zig fmt`, which modifies files in-place + out_file.close(); + try runZigFmt( + gpa, + arena, + .{ .cwd = global_opts.project_dir }, + global_opts.getZigEnv(), + &[_][]const u8{ + opts.build_file orelse "build.zig", + }, + ); } From 690ce58e0e80f84fd6e680c7548d04f4cc1ed88f Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 16 Jul 2025 12:06:50 -0400 Subject: [PATCH 2/2] chore: ignore stdout/err output of zig fmt --- build.zig | 8 +++----- src/sync_build_file.zig | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/build.zig b/build.zig index af27020..a847713 100644 --- a/build.zig +++ b/build.zig @@ -18,8 +18,7 @@ pub fn build(b: *std.Build) void { .root_module = module_zbuild, }); - const install_exe_zbuild = b.addInstallArtifact(exe_zbuild, .{ - }); + const install_exe_zbuild = b.addInstallArtifact(exe_zbuild, .{}); const tls_install_exe_zbuild = b.step("build-exe:zbuild", "Install the zbuild executable"); tls_install_exe_zbuild.dependOn(&install_exe_zbuild.step); @@ -35,7 +34,7 @@ pub fn build(b: *std.Build) void { const test_zbuild = b.addTest(.{ .name = "zbuild", .root_module = module_zbuild, - .filters = &[_][]const u8{ }, + .filters = &[_][]const u8{}, }); const install_test_zbuild = b.addInstallArtifact(test_zbuild, .{}); const tls_install_test_zbuild = b.step("build-test:zbuild", "Install the zbuild test"); @@ -56,7 +55,7 @@ pub fn build(b: *std.Build) void { const test_sync = b.addTest(.{ .name = "sync", .root_module = module_sync, - .filters = &[_][]const u8{ }, + .filters = &[_][]const u8{}, }); const install_test_sync = b.addInstallArtifact(test_sync, .{}); const tls_install_test_sync = b.step("build-test:sync", "Install the sync test"); @@ -68,5 +67,4 @@ pub fn build(b: *std.Build) void { tls_run_test.dependOn(&run_test_sync.step); module_sync.addImport("zbuild", module_zbuild); - } diff --git a/src/sync_build_file.zig b/src/sync_build_file.zig index bf20181..6435de0 100644 --- a/src/sync_build_file.zig +++ b/src/sync_build_file.zig @@ -31,10 +31,8 @@ pub fn syncBuildFile(gpa: Allocator, arena: Allocator, config: Config, global_op try runZigFmt( gpa, arena, - .{ .cwd = global_opts.project_dir }, + .{ .cwd = global_opts.project_dir, .stderr_behavior = .Ignore, .stdout_behavior = .Ignore }, global_opts.getZigEnv(), - &[_][]const u8{ - opts.build_file orelse "build.zig", - }, + &[_][]const u8{opts.build_file orelse "build.zig"}, ); }