Skip to content

Commit

Permalink
cleanup build runner process error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Dec 19, 2024
1 parent 86c0d4d commit e390f5c
Showing 1 changed file with 56 additions and 37 deletions.
93 changes: 56 additions & 37 deletions src/features/diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,14 @@ pub const BuildOnSave = struct {
return;
};

errdefer _ = child_process.kill() catch |err| {
std.debug.panic("failed to terminate build runner process, error: {}", .{err});
};
errdefer {
_ = terminateChildProcessReportError(
&child_process,
options.allocator,
"zig build runner",
.kill,
);
}

self.* = .{
.allocator = options.allocator,
Expand All @@ -453,31 +458,13 @@ pub const BuildOnSave = struct {
return;
};

const stderr_file = self.child_process.stderr.?;
const stderr = stderr_file.readToEndAlloc(self.allocator, 16 * 1024 * 1024) catch "";
defer self.allocator.free(stderr);

const term = self.child_process.wait() catch |err| {
log.warn("Failed to await zig build runner: {}", .{err});
return;
};

switch (term) {
.Exited => |code| if (code != 0) {
if (stderr.len != 0) {
log.warn("zig build runner exited with non-zero status: {}\nstderr:\n{s}", .{ code, stderr });
} else {
log.warn("zig build runner exited with non-zero status: {}", .{code});
}
},
else => {
if (stderr.len != 0) {
log.warn("zig build runner exitied abnormally: {s}\nstderr:\n{s}", .{ @tagName(term), stderr });
} else {
log.warn("zig build runner exitied abnormally: {s}", .{@tagName(term)});
}
},
}
const success = terminateChildProcessReportError(
&self.child_process,
self.allocator,
"zig build runner",
.wait,
);
if (!success) return;

self.thread.join();
self.* = undefined;
Expand All @@ -499,15 +486,7 @@ pub const BuildOnSave = struct {

while (true) {
const header = transport.receiveMessage(null) catch |err| switch (err) {
error.EndOfStream => {
const stderr = self.child_process.stderr.?.readToEndAlloc(self.allocator, 16 * 1024 * 1024) catch "";
defer self.allocator.free(stderr);

if (stderr.len != 0) {
log.err("zig build runner exited with stderr:\n{s}", .{stderr});
}
return;
},
error.EndOfStream => return,
else => {
log.err("failed to receive message from build runner: {}", .{err});
return;
Expand Down Expand Up @@ -559,3 +538,43 @@ pub const BuildOnSave = struct {
try collection.publishDiagnostics();
}
};

fn terminateChildProcessReportError(
child_process: *std.process.Child,
allocator: std.mem.Allocator,
name: []const u8,
kind: enum { wait, kill },
) bool {
const stderr = if (child_process.stderr) |stderr|
stderr.readToEndAlloc(allocator, 16 * 1024 * 1024) catch ""
else
"";
defer allocator.free(stderr);

const term = (switch (kind) {
.wait => child_process.wait(),
.kill => child_process.kill(),
}) catch |err| {
log.warn("Failed to await {s}: {}", .{ name, err });
return false;
};

switch (term) {
.Exited => |code| if (code != 0) {
if (stderr.len != 0) {
log.warn("{s} exited with non-zero status: {}\nstderr:\n{s}", .{ name, code, stderr });
} else {
log.warn("{s} exited with non-zero status: {}", .{ name, code });
}
},
else => {
if (stderr.len != 0) {
log.warn("{s} exitied abnormally: {s}\nstderr:\n{s}", .{ name, @tagName(term), stderr });
} else {
log.warn("{s} exitied abnormally: {s}", .{ name, @tagName(term) });
}
},
}

return true;
}

0 comments on commit e390f5c

Please sign in to comment.