@@ -426,9 +426,14 @@ pub const BuildOnSave = struct {
426426 return ;
427427 };
428428
429- errdefer _ = child_process .kill () catch | err | {
430- std .debug .panic ("failed to terminate build runner process, error: {}" , .{err });
431- };
429+ errdefer {
430+ _ = terminateChildProcessReportError (
431+ & child_process ,
432+ options .allocator ,
433+ "zig build runner" ,
434+ .kill ,
435+ );
436+ }
432437
433438 self .* = .{
434439 .allocator = options .allocator ,
@@ -453,31 +458,13 @@ pub const BuildOnSave = struct {
453458 return ;
454459 };
455460
456- const stderr_file = self .child_process .stderr .? ;
457- const stderr = stderr_file .readToEndAlloc (self .allocator , 16 * 1024 * 1024 ) catch "" ;
458- defer self .allocator .free (stderr );
459-
460- const term = self .child_process .wait () catch | err | {
461- log .warn ("Failed to await zig build runner: {}" , .{err });
462- return ;
463- };
464-
465- switch (term ) {
466- .Exited = > | code | if (code != 0 ) {
467- if (stderr .len != 0 ) {
468- log .warn ("zig build runner exited with non-zero status: {}\n stderr:\n {s}" , .{ code , stderr });
469- } else {
470- log .warn ("zig build runner exited with non-zero status: {}" , .{code });
471- }
472- },
473- else = > {
474- if (stderr .len != 0 ) {
475- log .warn ("zig build runner exitied abnormally: {s}\n stderr:\n {s}" , .{ @tagName (term ), stderr });
476- } else {
477- log .warn ("zig build runner exitied abnormally: {s}" , .{@tagName (term )});
478- }
479- },
480- }
461+ const success = terminateChildProcessReportError (
462+ & self .child_process ,
463+ self .allocator ,
464+ "zig build runner" ,
465+ .wait ,
466+ );
467+ if (! success ) return ;
481468
482469 self .thread .join ();
483470 self .* = undefined ;
@@ -499,15 +486,7 @@ pub const BuildOnSave = struct {
499486
500487 while (true ) {
501488 const header = transport .receiveMessage (null ) catch | err | switch (err ) {
502- error .EndOfStream = > {
503- const stderr = self .child_process .stderr .? .readToEndAlloc (self .allocator , 16 * 1024 * 1024 ) catch "" ;
504- defer self .allocator .free (stderr );
505-
506- if (stderr .len != 0 ) {
507- log .err ("zig build runner exited with stderr:\n {s}" , .{stderr });
508- }
509- return ;
510- },
489+ error .EndOfStream = > return ,
511490 else = > {
512491 log .err ("failed to receive message from build runner: {}" , .{err });
513492 return ;
@@ -559,3 +538,43 @@ pub const BuildOnSave = struct {
559538 try collection .publishDiagnostics ();
560539 }
561540};
541+
542+ fn terminateChildProcessReportError (
543+ child_process : * std.process.Child ,
544+ allocator : std.mem.Allocator ,
545+ name : []const u8 ,
546+ kind : enum { wait , kill },
547+ ) bool {
548+ const stderr = if (child_process .stderr ) | stderr |
549+ stderr .readToEndAlloc (allocator , 16 * 1024 * 1024 ) catch ""
550+ else
551+ "" ;
552+ defer allocator .free (stderr );
553+
554+ const term = (switch (kind ) {
555+ .wait = > child_process .wait (),
556+ .kill = > child_process .kill (),
557+ }) catch | err | {
558+ log .warn ("Failed to await {s}: {}" , .{ name , err });
559+ return false ;
560+ };
561+
562+ switch (term ) {
563+ .Exited = > | code | if (code != 0 ) {
564+ if (stderr .len != 0 ) {
565+ log .warn ("{s} exited with non-zero status: {}\n stderr:\n {s}" , .{ name , code , stderr });
566+ } else {
567+ log .warn ("{s} exited with non-zero status: {}" , .{ name , code });
568+ }
569+ },
570+ else = > {
571+ if (stderr .len != 0 ) {
572+ log .warn ("{s} exitied abnormally: {s}\n stderr:\n {s}" , .{ name , @tagName (term ), stderr });
573+ } else {
574+ log .warn ("{s} exitied abnormally: {s}" , .{ name , @tagName (term ) });
575+ }
576+ },
577+ }
578+
579+ return true ;
580+ }
0 commit comments