Skip to content

Commit ff987ad

Browse files
committed
Fallback to the default error format when a file is empty
This change resolves a bad match error that occurs when a .erl file is empty by falling back to the default error format.
1 parent 1959c16 commit ff987ad

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Diff for: apps/rebar/src/rebar_compiler_format.erl

+9-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ format(Source, {Line, Column}, Extra, Desc, Config) ->
2929
end.
3030

3131
find_line(Nth, Source) ->
32-
try
33-
{ok, Bin} = file:read_file(Source),
34-
Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
35-
{ok, lists:nth(Nth, Splits)}
36-
catch
37-
error:X -> {error, X}
38-
end.
32+
case file:read_file(Source) of
33+
{ok, <<>>} ->
34+
{error, empty_file};
35+
{ok, Bin} ->
36+
Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
37+
{ok, lists:nth(Nth, Splits)};
38+
{error, Reason} ->
39+
{error, Reason}
40+
end.
3941

4042
indent(0, _) -> "";
4143
indent(N, <<"\t", Rest/binary>>) -> [$\t | indent(N-1, Rest)];

Diff for: apps/rebar/test/rebar_compiler_format_SUITE.erl

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ init_per_testcase(_, Config) ->
1818
application:set_env(cf, colour_term, cf_term:has_color("dumb")),
1919
FileName = filename:join(?config(priv_dir, Config), "oracle.erl"),
2020
ok = file:write_file(FileName, oracle()),
21+
EmptyFileName = filename:join(?config(priv_dir, Config), "empty.erl"),
22+
ok = file:write_file(EmptyFileName, ""),
2123
Conf = dict:from_list([{compiler_error_format, rich}]),
22-
[{conf, Conf}, {file, FileName}, {term, OriginalTerm} | Config].
24+
[{conf, Conf}, {file, FileName}, {empty_file, EmptyFileName}, {term, OriginalTerm} | Config].
2325

2426
end_per_testcase(_, Config) ->
2527
case ?config(term, Config) of
@@ -65,6 +67,7 @@ nocolor() ->
6567
[{doc, "testing all sorts of planned output"}].
6668
nocolor(Config) ->
6769
Path = ?config(file, Config),
70+
EmptyPath = ?config(empty_file, Config),
6871
Conf = ?config(conf, Config),
6972
?assertEqual(" ┌─ "++Path++":"++?EOL++
7073
""++?EOL++
@@ -90,5 +93,8 @@ nocolor(Config) ->
9093
rebar_compiler_format:format(Path, {855,1}, "", "invalid ranges.", Conf)),
9194
?assertEqual("/very/fake/path.oof:1:1: unknown file."++?EOL,
9295
rebar_compiler_format:format("/very/fake/path.oof", {1,1}, "", "unknown file.", Conf)),
96+
%% empty file
97+
?assertEqual(EmptyPath++":1:1: should fallback to the minimal output"++?EOL,
98+
rebar_compiler_format:format(EmptyPath, {1,1}, "", "should fallback to the minimal output", Conf)),
9399
ok.
94100

0 commit comments

Comments
 (0)