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

add check_for_undefined_functions option using xref on release #815

Merged
merged 4 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

%% used in tests
{rlx_app_info, new, 5},
{rlx_app_info, new, 6},
{rlx_file_utils, type, 1},
{rlx_file_utils, write, 2},
{rlx_release, applications, 1},
Expand Down
22 changes: 19 additions & 3 deletions src/rlx_app_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
-module(rlx_app_info).

-export([new/5,
new/6,
name/1,
vsn/1,
dir/1,
Expand All @@ -50,27 +51,42 @@

-include("relx.hrl").

-type app_type() :: project | dep | checkout | system.
-type t() :: #{name := atom() | undefined,
vsn := string() | undefined,

applications := [atom()],
included_applications := [atom()],

dir := file:name() | undefined,
link := boolean() | undefined}.
link := boolean() | undefined,

-export_type([t/0]).
%% `project' app is one the user is actively developing on
%% `dep' is dependency fetched by rebar3
%% `checkout' is a dependency linked to from the _checkouts dir
%% and treated like a project app
%% `system' applications are dependencies from Erlang/OTP
app_type := app_type()}.

-export_type([t/0,
app_type/0]).

-spec new(atom(), string(), file:name(), [atom()], [atom()]) -> t().
new(Name, Vsn, Dir, Applications, IncludedApplications) ->
new(Name, Vsn, Dir, Applications, IncludedApplications, dep).

-spec new(atom(), string(), file:name(), [atom()], [atom()], app_type()) -> t().
new(Name, Vsn, Dir, Applications, IncludedApplications, AppType) ->
#{name => Name,
vsn => Vsn,

applications => Applications,
included_applications => IncludedApplications,

dir => Dir,
link => false}.
link => false,

app_type => AppType}.

-spec name(t()) -> atom().
name(#{name := Name}) ->
Expand Down
73 changes: 72 additions & 1 deletion src/rlx_assemble.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
-include("relx.hrl").
-include("rlx_log.hrl").

-define(XREF_SERVER, rlx_xref).

do(Release, State) ->
RelName = rlx_release:name(Release),
?log_info("Assembling release ~p-~s...", [RelName, rlx_release:vsn(Release)]),
Expand Down Expand Up @@ -606,12 +608,17 @@ include_erts(State, Release, OutputDir, RelDir) ->

-spec make_boot_script(rlx_state:t(), rlx_release:t(), file:name(), file:name()) -> ok.
make_boot_script(State, Release, OutputDir, RelDir) ->
Options = [{path, [RelDir | rlx_util:get_code_paths(Release, OutputDir)]},
Paths = [RelDir | rlx_util:get_code_paths(Release, OutputDir)],
Options = [{path, Paths},
{outdir, RelDir},
{variables, make_boot_script_variables(Release, State)},
silent | make_script_options(State)],
Name = atom_to_list(rlx_release:name(Release)),
IsRelxSasl = rlx_state:is_relx_sasl(State),

%% relx built-in form of systools exref feature
maybe_check_for_undefined_functions(State, Release),

case make_start_script(Name, RelDir, Options, IsRelxSasl) of
Result when Result =:= ok orelse (is_tuple(Result) andalso
element(1, Result) =:= ok) ->
Expand All @@ -627,6 +634,70 @@ make_boot_script(State, Release, OutputDir, RelDir) ->
erlang:error(?RLX_ERROR({release_script_generation_error, Module, Error}))
end.

maybe_check_for_undefined_functions(State, Release) ->
case rlx_state:check_for_undefined_functions(State) of
true ->
maybe_check_for_undefined_functions_(State, Release);
_ ->
ok
end.

maybe_check_for_undefined_functions_(State, Release) ->
{ok, _} = xref:start(?XREF_SERVER, [{xref_mode, functions}]),

%% for every app in the release add it to the xref apps to be analyzed if
%% it is a project app as specified by rebar3.
add_project_apps_to_xref(rlx_release:app_specs(Release), State),

%% without adding the erts application there will be warnings about missing
%% functions from the preloaded modules even though they are in the runtime.
ErtsApp = code:lib_dir(erts, ebin),

%% xref library path is what is searched for functions used by the project apps.
%% we only add applications depended on by the release so that we catch
%% modules not included in the release to warn the user about.
CodePath = [ErtsApp | [filename:join(rlx_app_info:dir(App), "ebin") ||
App <- rlx_release:applications(Release)]],
_ = xref:set_library_path(?XREF_SERVER, CodePath),

%% check for undefined function calls from project apps in the release
case xref:analyze(?XREF_SERVER, undefined_function_calls) of
{ok, Warnings} ->
format_xref_warning(Warnings);
{error, _} = Error ->
?log_warn("Error running xref analyze: ~s", [xref:format_error(Error)])
end,

xref:stop(?XREF_SERVER).
add_project_apps_to_xref([], _) ->
ok;
add_project_apps_to_xref([AppSpec | Rest], State) ->
case maps:find(element(1, AppSpec), rlx_state:available_apps(State)) of
{ok, App=#{app_type := project}} ->
case xref:add_application(?XREF_SERVER,
rlx_app_info:dir(App),
[{name, rlx_app_info:name(App)}, {warnings, false}]) of
{ok, _} ->
ok;
{error, _} = Error ->
?log_warn("Error adding application ~s to xref context: ~s",
[rlx_app_info:name(App), xref:format_error(Error)])
end;
_ ->
ok
end,
add_project_apps_to_xref(Rest, State).

format_xref_warning([]) ->
ok;
format_xref_warning(Warnings) ->
?log_warn("There are missing function calls in the release.", []),
?log_warn("Make sure all applications needed at runtime are included in the release.", []),
lists:map(fun({{M1, F1, A1}, {M2, F2, A2}}) ->
?log_warn("~w:~tw/~w calls undefined function ~w:~tw/~w",
[M1, F1, A1, M2, F2, A2])
end, Warnings).

%% setup options for warnings as errors, src_tests and exref
make_script_options(State) ->
IncludeSrc = include_src_or_default(State),
Expand Down
2 changes: 2 additions & 0 deletions src/rlx_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ load({src_tests, SrcTests}, {ok, State}) ->
{ok, rlx_state:src_tests(State, SrcTests)};
load({exref, ExRef}, {ok, State}) ->
{ok, rlx_state:exref(State, ExRef)};
load({check_for_undefined_functions, CheckForUndefinedFunctions}, {ok, State}) ->
{ok, rlx_state:check_for_undefined_functions(State, CheckForUndefinedFunctions)};
load({include_erts, IncludeErts}, {ok, State}) ->
{ok, rlx_state:include_erts(State, IncludeErts)};
load({system_libs, SystemLibs}, {ok, State}) ->
Expand Down
11 changes: 11 additions & 0 deletions src/rlx_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
src_tests/2,
exref/1,
exref/2,
check_for_undefined_functions/1,
check_for_undefined_functions/2,
is_relx_sasl/1]).

-type mode() :: dev | prod | minimal.
Expand Down Expand Up @@ -120,6 +122,7 @@
upfrom :: string() | binary() | undefined,
warnings_as_errors=false :: boolean(),
src_tests=true :: boolean(),
check_for_undefined_functions=true :: boolean(),
exref=false :: boolean() | [atom()],
overlay=[] :: list(),
include_nodetool=true :: boolean(),
Expand Down Expand Up @@ -442,6 +445,14 @@ src_tests(#state_t{src_tests=SrcTests}) ->
src_tests(State, SrcTests) ->
State#state_t{src_tests=SrcTests}.

-spec check_for_undefined_functions(t()) -> boolean().
check_for_undefined_functions(#state_t{check_for_undefined_functions=CheckForUndefinedFunctions}) ->
CheckForUndefinedFunctions.

-spec check_for_undefined_functions(t(), boolean()) -> t().
check_for_undefined_functions(State, CheckForUndefinedFunctions) ->
State#state_t{check_for_undefined_functions=CheckForUndefinedFunctions}.

-spec exref(t()) -> boolean() | [atom()].
exref(#state_t{exref=ExRef}) ->
ExRef.
Expand Down
Loading