File 0007-Fallback-to-the-default-error-format-when-a-file-is-.patch of Package rebar3
From e383d3b2fc2a05c3868e139e89eed0610665c048 Mon Sep 17 00:00:00 2001
From: williamthome <williamthome@hotmail.com>
Date: Tue, 17 Dec 2024 18:14:13 -0300
Subject: [PATCH] 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.
---
apps/rebar/src/rebar_compiler_format.erl | 22 +++++++++++++------
.../test/rebar_compiler_format_SUITE.erl | 8 ++++++-
2 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/apps/rebar/src/rebar_compiler_format.erl b/apps/rebar/src/rebar_compiler_format.erl
index f27d5f64..50502605 100644
--- a/apps/rebar/src/rebar_compiler_format.erl
+++ b/apps/rebar/src/rebar_compiler_format.erl
@@ -29,13 +29,21 @@ format(Source, {Line, Column}, Extra, Desc, Config) ->
end.
find_line(Nth, Source) ->
- try
- {ok, Bin} = file:read_file(Source),
- Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
- {ok, lists:nth(Nth, Splits)}
- catch
- error:X -> {error, X}
- end.
+ try do_find_line(Nth, Source)
+ catch
+ error:X -> {error, X}
+ end.
+
+do_find_line(Nth, Source) ->
+ case file:read_file(Source) of
+ {ok, <<>>} ->
+ {error, empty_file};
+ {ok, Bin} ->
+ Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
+ {ok, lists:nth(Nth, Splits)};
+ {error, Reason} ->
+ {error, Reason}
+ end.
indent(0, _) -> "";
indent(N, <<"\t", Rest/binary>>) -> [$\t | indent(N-1, Rest)];
diff --git a/apps/rebar/test/rebar_compiler_format_SUITE.erl b/apps/rebar/test/rebar_compiler_format_SUITE.erl
index 1a7d297f..7aaeb044 100644
--- a/apps/rebar/test/rebar_compiler_format_SUITE.erl
+++ b/apps/rebar/test/rebar_compiler_format_SUITE.erl
@@ -18,8 +18,10 @@ init_per_testcase(_, Config) ->
application:set_env(cf, colour_term, cf_term:has_color("dumb")),
FileName = filename:join(?config(priv_dir, Config), "oracle.erl"),
ok = file:write_file(FileName, oracle()),
+ EmptyFileName = filename:join(?config(priv_dir, Config), "empty.erl"),
+ ok = file:write_file(EmptyFileName, ""),
Conf = dict:from_list([{compiler_error_format, rich}]),
- [{conf, Conf}, {file, FileName}, {term, OriginalTerm} | Config].
+ [{conf, Conf}, {file, FileName}, {empty_file, EmptyFileName}, {term, OriginalTerm} | Config].
end_per_testcase(_, Config) ->
case ?config(term, Config) of
@@ -65,6 +67,7 @@ nocolor() ->
[{doc, "testing all sorts of planned output"}].
nocolor(Config) ->
Path = ?config(file, Config),
+ EmptyPath = ?config(empty_file, Config),
Conf = ?config(conf, Config),
?assertEqual(" ┌─ "++Path++":"++?EOL++
" │"++?EOL++
@@ -90,5 +93,8 @@ nocolor(Config) ->
rebar_compiler_format:format(Path, {855,1}, "", "invalid ranges.", Conf)),
?assertEqual("/very/fake/path.oof:1:1: unknown file."++?EOL,
rebar_compiler_format:format("/very/fake/path.oof", {1,1}, "", "unknown file.", Conf)),
+ %% empty file
+ ?assertEqual(EmptyPath++":1:1: should fallback to the minimal output"++?EOL,
+ rebar_compiler_format:format(EmptyPath, {1,1}, "", "should fallback to the minimal output", Conf)),
ok.
--
2.43.0