Skip to content

Commit e514454

Browse files
committedJun 2, 2018
Make zig fmt exit with error on any parse errors
This is required for proper detection in editor plugins. Other files may have been formatted correctly, this only indicates that some failed.
·
0.15.20.3.0
1 parent f06bce5 commit e514454

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed
 

‎src-self-hosted/main.zig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,18 +728,21 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
728728
}
729729
};
730730

731+
var fmt_errors = false;
731732
for (flags.positionals.toSliceConst()) |file_path| {
732733
var file = try os.File.openRead(allocator, file_path);
733734
defer file.close();
734735

735736
const source_code = io.readFileAlloc(allocator, file_path) catch |err| {
736737
try stderr.print("unable to open '{}': {}", file_path, err);
738+
fmt_errors = true;
737739
continue;
738740
};
739741
defer allocator.free(source_code);
740742

741743
var tree = std.zig.parse(allocator, source_code) catch |err| {
742744
try stderr.print("error parsing file '{}': {}\n", file_path, err);
745+
fmt_errors = true;
743746
continue;
744747
};
745748
defer tree.deinit();
@@ -752,6 +755,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
752755
try errmsg.printToFile(&stderr_file, msg, color);
753756
}
754757
if (tree.errors.len != 0) {
758+
fmt_errors = true;
755759
continue;
756760
}
757761

@@ -764,6 +768,10 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
764768
try baf.finish();
765769
}
766770
}
771+
772+
if (fmt_errors) {
773+
os.exit(1);
774+
}
767775
}
768776

769777
// cmd:targets /////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)
Please sign in to comment.