Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to the default error format when a file is empty #2931

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

williamthome
Copy link
Contributor

This change resolves a bad match error that occurs when a .erl file is empty by falling back to the default error format.

Before

===> Task failed: {{badmatch,[]},
                              [{rebar_compiler_format,colorize,2,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_compiler_format.erl"},
                                 {line,74}]},
                               {rebar_compiler_format,format,5,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_compiler_format.erl"},
                                 {line,25}]},
                               {rebar_base_compiler,
                                '-format_errors/4-lc$^1/1-1-',4,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_base_compiler.erl"},
                                 {line,278}]},
                               {rebar_base_compiler,
                                '-format_errors/4-lc$^0/1-0-',3,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_base_compiler.erl"},
                                 {line,278}]},
                               {rebar_base_compiler,error_tuple,5,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_base_compiler.erl"},
                                 {line,160}]},
                               {rebar_compiler,compile_worker,2,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_compiler.erl"},
                                 {line,337}]},
                               {rebar_parallel,worker,3,
                                [{file,
                                  "/home/runner/work/rebar3/rebar3/apps/rebar/src/rebar_parallel.erl"},
                                 {line,260}]}]}

After

===> Compiling src/r3_colorize_bug.erl failed
src/r3_colorize_bug.erl:1:1: no module definition

Comment on lines 32 to 41
try
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try
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
try 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}

This might give you similar semantics but a cleaner result, although it won't capture errors in re:split or lists:nth anymore, so we can maybe disregard this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the try/catch in ff987ad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing now. It seems we need the try/catch here to prevent a crash. I'll push a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e383d3b

Copy link
Collaborator

@ferd ferd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

@williamthome williamthome force-pushed the fix/rich-format-badmatch branch from 6f1e917 to ff987ad Compare December 18, 2024 19:20
This change resolves a bad match error that occurs when a .erl file is
empty by falling back to the default error format.
@williamthome williamthome force-pushed the fix/rich-format-badmatch branch from ff987ad to e383d3b Compare December 18, 2024 19:53
@ferd ferd merged commit 1dbce09 into erlang:main Dec 19, 2024
6 checks passed
@williamthome williamthome deleted the fix/rich-format-badmatch branch December 19, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants