From 646c45ea656cedee3902e289b2ec92a91c87b66a Mon Sep 17 00:00:00 2001 From: Froxcey Date: Mon, 25 Mar 2024 15:02:40 +0800 Subject: [PATCH 1/3] Replace exit with std.process.exit `std.os.exit` got removed in [cd62005](https://github.com/ziglang/zig/commit/cd62005f19ff966d2c42de4daeb9a1e4b644bf76#diff-4284cdf770ea88be46f4aab7f82ed3c58821e4b3433b7c6152278a9911348dc5) This commit fixes this by replacing all `std.os.exit` calls with `std.process.exit`. --- cli/cli.zig | 2 +- cli/commands/bundle.zig | 2 +- cli/commands/server.zig | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cli.zig b/cli/cli.zig index 8171e78..3e9908a 100644 --- a/cli/cli.zig +++ b/cli/cli.zig @@ -51,7 +51,7 @@ pub fn main() !void { run(allocator, options, writer) catch |err| { switch (err) { - error.JetzigCommandError => std.os.exit(1), + error.JetzigCommandError => std.process.exit(1), else => return err, } }; diff --git a/cli/commands/bundle.zig b/cli/commands/bundle.zig index 7435a7e..b452f55 100644 --- a/cli/commands/bundle.zig +++ b/cli/commands/bundle.zig @@ -134,6 +134,6 @@ pub fn run( } else { std.debug.print("Unable to locate compiled executable. Exiting.", .{}); util.printFailure(); - std.os.exit(1); + std.process.exit(1); } } diff --git a/cli/commands/server.zig b/cli/commands/server.zig index f38aca7..b8d4b42 100644 --- a/cli/commands/server.zig +++ b/cli/commands/server.zig @@ -72,7 +72,7 @@ pub fn run( const exe_path = try util.locateExecutable(allocator, cwd, .{}); if (exe_path == null) { std.debug.print("Unable to locate compiled executable. Exiting.\n", .{}); - std.os.exit(1); + std.process.exit(1); } const argv = &[_][]const u8{exe_path.?}; @@ -94,7 +94,7 @@ pub fn run( if (!options.reload) { const term = try process.wait(); - std.os.exit(term.Exited); + std.process.exit(term.Exited); } while (true) { From 19c04f1482ffa5093d9a6d497213e7d3fae06b7f Mon Sep 17 00:00:00 2001 From: Froxcey Date: Mon, 25 Mar 2024 15:27:05 +0800 Subject: [PATCH 2/3] Cli: Add zig compiler color The compiler doesn't pass color to the parent process unless it is called with `--colour on`. This commit adds those arguments to add support for coloring in compiler outputs. --- cli/commands/bundle.zig | 2 +- cli/commands/server.zig | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/commands/bundle.zig b/cli/commands/bundle.zig index b452f55..d9972a5 100644 --- a/cli/commands/bundle.zig +++ b/cli/commands/bundle.zig @@ -62,7 +62,7 @@ pub fn run( var install_argv = std.ArrayList([]const u8).init(allocator); defer install_argv.deinit(); - try install_argv.appendSlice(&[_][]const u8{ "zig", "build" }); + try install_argv.appendSlice(&[_][]const u8{ "zig", "build", "--color", "on" }); switch (builtin.os.tag) { .windows => try tar_argv.appendSlice(&[_][]const u8{ diff --git a/cli/commands/server.zig b/cli/commands/server.zig index b8d4b42..b257a33 100644 --- a/cli/commands/server.zig +++ b/cli/commands/server.zig @@ -63,10 +63,11 @@ pub fn run( ); while (true) { + // TODO: Catch this error instead of propagating try util.runCommand( allocator, realpath, - &[_][]const u8{ "zig", "build", "-Djetzig_runner=true", "install" }, + &[_][]const u8{ "zig", "build", "-Djetzig_runner=true", "install", "--color", "on" }, ); const exe_path = try util.locateExecutable(allocator, cwd, .{}); From f01abb86f576fe10501ca91f6d5dbd9edc9f5223 Mon Sep 17 00:00:00 2001 From: Froxcey Date: Mon, 25 Mar 2024 16:55:12 +0800 Subject: [PATCH 3/3] Cli: Reload for fail build When zig build fails, the `jetzig server` command also exits. This should be an uninteneded behavior. This commit fixes this by catching failed build and restart after file change is detected. Fixes #33 --- cli/commands/server.zig | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/cli/commands/server.zig b/cli/commands/server.zig index b257a33..915677d 100644 --- a/cli/commands/server.zig +++ b/cli/commands/server.zig @@ -63,12 +63,15 @@ pub fn run( ); while (true) { - // TODO: Catch this error instead of propagating - try util.runCommand( + util.runCommand( allocator, realpath, &[_][]const u8{ "zig", "build", "-Djetzig_runner=true", "install", "--color", "on" }, - ); + ) catch { + std.debug.print("Build failed, waiting for file change...\n", .{}); + try awaitFileChange(allocator, cwd, &mtime); + continue; + }; const exe_path = try util.locateExecutable(allocator, cwd, .{}); if (exe_path == null) { @@ -98,22 +101,22 @@ pub fn run( std.process.exit(term.Exited); } - while (true) { - if (process.term) |_| { - _ = try process.wait(); - std.debug.print("Server exited, restarting...\n", .{}); - } + // HACK: This currenly doesn't restart the server when it exits, maybe that + // could be implemented in the future. - std.time.sleep(watch_changes_pause_duration); + awaitFileChange(allocator, cwd, mtime); + std.debug.print("Changes detected, restarting server...\n", .{}); + _ = try process.kill(); + } +} - const new_mtime = try totalMtime(allocator, cwd, "src"); - - if (new_mtime > mtime) { - std.debug.print("Changes detected, restarting server...\n", .{}); - _ = try process.kill(); - mtime = new_mtime; - break; - } +fn awaitFileChange(allocator: std.mem.Allocator, cwd: std.fs.Dir, mtime: *i128) !void { + while (true) { + std.time.sleep(watch_changes_pause_duration); + const new_mtime = try totalMtime(allocator, cwd, "src"); + if (new_mtime > mtime.*) { + mtime.* = new_mtime; + return; } } }