From e514454c0e092794171d4a62260971cc8de6f200 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Sat, 2 Jun 2018 20:49:35 +1200 Subject: [PATCH] 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. --- src-self-hosted/main.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 80b1c3889..7a62f4985 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -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 /////////////////////////////////////////////////////////////////////////////////////