-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2783 from ferd/try-fancy-compiler-error-msgs
Support rich compiler error messages
- Loading branch information
Showing
11 changed files
with
211 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,3 +88,4 @@ | |
]} | ||
]}. | ||
|
||
{compiler_error_format, rich}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
%%% @doc Module handling rich formatting of errors. | ||
-module(rebar_compiler_format). | ||
-export([format/5]). | ||
|
||
-include("rebar.hrl"). | ||
|
||
-spec format(file:filename_all(), {Line, Column}, Extra, Desc, rebar_dict()) -> | ||
string() when | ||
Extra :: iodata(), | ||
Line :: non_neg_integer(), | ||
Column :: non_neg_integer(), | ||
Desc :: iodata(). | ||
format(Source, {Line, Column}, Extra, Desc, Config) -> | ||
CompilerErrFmt = compiler_error_format(Config), | ||
case CompilerErrFmt == rich andalso find_line(Line, Source) of | ||
{ok, LnBin} -> | ||
LnPad = lists:duplicate(length(integer_to_list(Line)), " "), | ||
Arrow = cf:format("~!R~ts~!!",["╰──"]), | ||
?FMT(" ~ts ┌─ ~ts:~n" | ||
" ~ts │~n" | ||
" ~w │ ~ts~n" | ||
" ~ts │ ~s~ts ~ts~ts~n~n", | ||
[LnPad, Source, | ||
LnPad, | ||
Line, colorize(LnBin, Column), | ||
LnPad, lists:duplicate(max(0, Column-1), " "), Arrow, Extra, Desc]); | ||
_ -> | ||
?FMT("~ts:~w:~w: ~ts~ts~n", [Source, Line, Column, Extra, Desc]) | ||
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. | ||
|
||
compiler_error_format(Opts) -> | ||
%% `Opts' can be passed in both as a list or a dictionary depending | ||
%% on whether the first call to rebar_erlc_compiler was done with | ||
%% the type `rebar_dict()' or `rebar_state:t()'. | ||
LookupFn = if is_list(Opts) -> fun(K,L) -> lists:keyfind(K, 1, L) end | ||
; true -> fun(K,O) -> rebar_opts:get(O, K, false) end | ||
end, | ||
case LookupFn(compiler_error_format, Opts) of | ||
false -> ?DEFAULT_COMPILER_ERROR_FORMAT; | ||
{ok, minimal} -> minimal; | ||
{ok, rich} -> rich; | ||
minimal -> minimal; | ||
rich -> rich | ||
end. | ||
|
||
%% @private try to colorize data based on common ways to end terminators | ||
%% in Erlang-like languages. Any character that isn't one of the following | ||
%% is considered to end a "word" of some type: | ||
%% | ||
%% - letters | ||
%% - numbers | ||
%% - underscore | ||
%% - quotations | ||
%% | ||
%% This will have false positives in some cases and if that becomes annoying | ||
%% we'll need to allow per-compiler module configurations here, but it should | ||
%% generally lead to proper colorization. | ||
colorize(Str, Col) -> | ||
Pre = string:slice(Str, 0, max(0,Col-1)), | ||
At = string:slice(Str, max(0,Col-1)), | ||
[Bad | Tail] = [B || B <- re:split(At, "([^[A-Za-z0-9_#\"]+)", []), | ||
B =/= <<>>], | ||
cf:format("~ts~!R~ts~!!~ts", [Pre,Bad,Tail]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
-module(rebar_compiler_format_SUITE). | ||
-compile([export_all, nowarn_export_all]). | ||
|
||
-include_lib("common_test/include/ct.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
-define(EOL, lists:flatten(io_lib:format("~n",[]))). | ||
|
||
all() -> | ||
[minimal, nocolor]. | ||
|
||
init_per_testcase(minimal, Config) -> | ||
Conf = dict:from_list([{compiler_error_format, minimal}]), | ||
[{conf, Conf} | init_per_testcase(regular, Config)]; | ||
init_per_testcase(_, Config) -> | ||
OriginalTerm = os:getenv("TERM"), | ||
os:putenv("TERM", "dumb"), % disable color | ||
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()), | ||
Conf = dict:from_list([{compiler_error_format, rich}]), | ||
[{conf, Conf}, {file, FileName}, {term, OriginalTerm} | Config]. | ||
|
||
end_per_testcase(_, Config) -> | ||
case ?config(term, Config) of | ||
false -> | ||
os:unsetenv("TERM"), | ||
application:unset_env(cf, colour_term); | ||
Original -> | ||
os:putenv("TERM", Original), | ||
application:set_env(cf, colour_term, cf_term:has_color("Original")) | ||
end, | ||
Config. | ||
|
||
oracle() -> | ||
"-module(noline_end);\n" | ||
++ lists:duplicate(9, $\n) ++ | ||
"first character on line 11.\n" | ||
++ lists:duplicate(99, $\n) ++ | ||
"case X of ^whatever % on line 111\n". | ||
|
||
minimal() -> | ||
[{doc, "showing minimal (default) output"}]. | ||
minimal(Config) -> | ||
Path = ?config(file, Config), | ||
Conf = ?config(conf, Config), | ||
?assertEqual(Path++":1:20: => unexpected token: ;"++?EOL, | ||
rebar_compiler_format:format(Path, {1,20}, "=> ", "unexpected token: ;", Conf)), | ||
?assertEqual(Path++":11:1: some message"++?EOL, | ||
rebar_compiler_format:format(Path, {11,1}, "", "some message", Conf)), | ||
?assertEqual(Path++":111:11: the character '^' is not expected here."++?EOL, | ||
rebar_compiler_format:format(Path, {111,11}, "", "the character '^' is not expected here.", Conf)), | ||
?assertEqual(Path++":-23:-42: invalid ranges."++?EOL, | ||
rebar_compiler_format:format(Path, {-23,-42}, "", "invalid ranges.", Conf)), | ||
?assertEqual(Path++":-23:-42: invalid ranges."++?EOL, | ||
rebar_compiler_format:format(Path, {-23,-42}, "", "invalid ranges.", Conf)), | ||
?assertEqual(Path++":855:1: invalid ranges."++?EOL, | ||
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)), | ||
ok. | ||
|
||
|
||
nocolor() -> | ||
[{doc, "testing all sorts of planned output"}]. | ||
nocolor(Config) -> | ||
Path = ?config(file, Config), | ||
Conf = ?config(conf, Config), | ||
?assertEqual(" ┌─ "++Path++":"++?EOL++ | ||
" │"++?EOL++ | ||
" 1 │ -module(noline_end);"++?EOL++ | ||
" │ ╰── => unexpected token: ;"++?EOL++?EOL, | ||
rebar_compiler_format:format(Path, {1,20}, "=> ", "unexpected token: ;", Conf)), | ||
?assertEqual(" ┌─ "++Path++":"++?EOL++ | ||
" │"++?EOL++ | ||
" 11 │ first character on line 11."++?EOL++ | ||
" │ ╰── some message"++?EOL++?EOL, | ||
rebar_compiler_format:format(Path, {11,1}, "", "some message", Conf)), | ||
?assertEqual(" ┌─ "++Path++":"++?EOL++ | ||
" │"++?EOL++ | ||
" 111 │ case X of ^whatever % on line 111"++?EOL++ | ||
" │ ╰── the character '^' is not expected here."++?EOL++?EOL, | ||
rebar_compiler_format:format(Path, {111,11}, "", "the character '^' is not expected here.", Conf)), | ||
%% invalid cases fall back to minimal mode | ||
?assertEqual(Path++":-23:-42: invalid ranges."++?EOL, | ||
rebar_compiler_format:format(Path, {-23,-42}, "", "invalid ranges.", Conf)), | ||
?assertEqual(Path++":-23:-42: invalid ranges."++?EOL, | ||
rebar_compiler_format:format(Path, {-23,-42}, "", "invalid ranges.", Conf)), | ||
?assertEqual(Path++":855:1: invalid ranges."++?EOL, | ||
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)), | ||
ok. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,4 +67,5 @@ | |
]} | ||
]}. | ||
|
||
{compiler_error_format, rich}. | ||
%% The rest of the config is in apps/rebar/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters