Skip to content

Commit

Permalink
Make zig fmt exit with error on any parse errors
Browse files Browse the repository at this point in the history
This is required for proper detection in editor plugins. Other files may
have been formatted correctly, this only indicates that some failed.
  • Loading branch information
tiehuis committed Jun 2, 2018
1 parent f06bce5 commit e514454
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src-self-hosted/main.zig
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -764,6 +768,10 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
try baf.finish();
}
}

if (fmt_errors) {
os.exit(1);
}
}

// cmd:targets /////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit e514454

Please sign in to comment.