From 9a9f2d2024506efcea72b0979946ffb23ad99eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Costas=20S=C3=A1nchez?= Date: Mon, 5 Oct 2020 20:22:14 +0200 Subject: [PATCH 1/2] Make rebar3 templates check for name clashes This commit changes rebar3 to check for name clashes of the modules created from its templates with existing Erlang modules to produce a non-blocking warning for each clashing module. --- src/rebar_templater.erl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 6acccc52..c8d743fe 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -196,6 +196,21 @@ maybe_warn_about_name(Vars) -> ok end. +maybe_warn_about_name_clash(File) -> + case filename:extension(File) of + ".erl" -> + Module0 = re:replace(filename:basename(File), "\.erl", "", [{return, list}]), + Module = list_to_atom(Module0), + try Module:module_info() of + _ -> ?WARN("The module definition of '~ts' in file ~ts " + "will clash with an existing Erlang module.", + [Module, File]) + catch + _:_ -> ok + end; + _ -> ok + end. + validate_atom(Str) -> case io_lib:fread("~a", unicode:characters_to_list(Str)) of {ok, [Atom], ""} -> @@ -258,6 +273,7 @@ execute_template([{template, From, To} | Terms], Files, {Template, Type, Cwd}, V In = expand_path(From, Vars), Out = expand_path(To, Vars), Tpl = load_file(Files, Type, filename:join(Cwd, In)), + maybe_warn_about_name_clash(Out), case write_file(Out, render(Tpl, Vars), Force) of ok -> ok; From a1d17fce6a2648cb05977163bfb549dc419b2077 Mon Sep 17 00:00:00 2001 From: Pablo Costas Date: Tue, 6 Oct 2020 22:59:57 +0200 Subject: [PATCH 2/2] Update rebar_templater with safer regex Co-authored-by: Fred Hebert --- src/rebar_templater.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index c8d743fe..001f6b1c 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -199,7 +199,7 @@ maybe_warn_about_name(Vars) -> maybe_warn_about_name_clash(File) -> case filename:extension(File) of ".erl" -> - Module0 = re:replace(filename:basename(File), "\.erl", "", [{return, list}]), + Module0 = re:replace(filename:basename(File), "\\.erl$", "", [{return, list}]), Module = list_to_atom(Module0), try Module:module_info() of _ -> ?WARN("The module definition of '~ts' in file ~ts "