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.
This commit is contained in:
Marc Tiehuis 2018-06-02 20:49:35 +12:00
parent f06bce5dda
commit e514454c0e

View File

@ -728,18 +728,21 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
}
};
var fmt_errors = false;
for (flags.positionals.toSliceConst()) |file_path| {
var file = try os.File.openRead(allocator, file_path);
defer file.close();
const source_code = io.readFileAlloc(allocator, file_path) catch |err| {
try stderr.print("unable to open '{}': {}", file_path, err);
fmt_errors = true;
continue;
};
defer allocator.free(source_code);
var tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing file '{}': {}\n", file_path, err);
fmt_errors = true;
continue;
};
defer tree.deinit();
@ -752,6 +755,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
try errmsg.printToFile(&stderr_file, msg, color);
}
if (tree.errors.len != 0) {
fmt_errors = true;
continue;
}
@ -764,6 +768,10 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
try baf.finish();
}
}
if (fmt_errors) {
os.exit(1);
}
}
// cmd:targets /////////////////////////////////////////////////////////////////////////////////////