Skip to content

Commit

Permalink
Cli: Reload for fail build
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Froxcey committed Mar 25, 2024
1 parent 19c04f1 commit f01abb8
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions cli/commands/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

const new_mtime = try totalMtime(allocator, cwd, "src");
awaitFileChange(allocator, cwd, mtime);
std.debug.print("Changes detected, restarting server...\n", .{});
_ = try process.kill();
}
}

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;
}
}
}
Expand Down

0 comments on commit f01abb8

Please sign in to comment.