Merge pull request #285 from nevar/fix_inheritance

Fix #249 (erlc regression)
This commit is contained in:
Tristan Sloughter 2014-06-15 14:48:12 -05:00
commit 8a0d8ad7a5
10 changed files with 92 additions and 11 deletions

View file

@ -1,3 +1,5 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-module(erlc_rt). -module(erlc_rt).
-export([files/0, -export([files/0,
run/1]). run/1]).
@ -7,6 +9,7 @@
-define(MODULES, -define(MODULES,
[first_xrl, [first_xrl,
first_yrl, first_yrl,
first_erl,
foo, foo,
foo_app, foo_app,
foo_sup, foo_sup,
@ -17,6 +20,7 @@
-define(BEAM_FILES, -define(BEAM_FILES,
["first_xrl.beam", ["first_xrl.beam",
"first_yrl.beam", "first_yrl.beam",
"first_erl.beam",
"foo.beam", "foo.beam",
"foo_app.beam", "foo_app.beam",
"foo_sup.beam", "foo_sup.beam",
@ -35,7 +39,10 @@ files() ->
{copy, "extra-src", "extra-src"}, {copy, "extra-src", "extra-src"},
{copy, "mibs", "mibs"}, {copy, "mibs", "mibs"},
{copy, "asn1", "asn1"}, {copy, "asn1", "asn1"},
{create, "ebin/foo.app", app(foo, ?MODULES)} {create, "ebin/foo.app", app(foo, ?MODULES)},
%% deps
{create, "deps/foobar/ebin/foobar.app", app(foobar, [foobar])},
{copy, "foobar.erl", "deps/foobar/src/foobar.erl"}
]. ].
run(_Dir) -> run(_Dir) ->
@ -53,6 +60,38 @@ run(_Dir) ->
ok = check_beams(true), ok = check_beams(true),
ok = check_debug_info(false), ok = check_debug_info(false),
?assertMatch(true, filelib:is_regular(MibResult)), ?assertMatch(true, filelib:is_regular(MibResult)),
%% Regression test for https://github.com/rebar/rebar/issues/249
%%
%% Root cause: We didn't have per-project .rebar/erlcinfo but just one in
%% <base_dir>/.rebar/erlcinfo.
%%
%% Solution: Ensure every project has its own .rebar/erlcinfo
%%
%% For the bug to happen, the following conditions must be met:
%%
%% 1. <base_dir>/rebar.config has erl_first_files
%% 2. one of the 'first' files depends on another file (in this
%% case via -include_lib())
%% 3. a sub project's rebar.config, if any, has no erl_first_files entry
%%
%% Now because erl_first_files is retrieved via rebar_config:get_list/3,
%% base_dir/rebar.config's erl_first_files is inherited, and because we had
%% a shared <base_dir>/.rebar/erlcinfo instead of one per project, the
%% cached entry was reused. Next, while compiling the sub project
%% rebar_erlc_compiler:needs_compile/3 gets a last modification time of
%% zero for the 'first' file which does not exist inside the sub project.
%% This, and the fact that it has at least one dependency, makes
%% needs_compile/3 return 'true'. The root cause is that we didn't have per
%% project .rebar/erlcinfo. For <base_dir>/.rebar/erlcinfo to be populated,
%% base_dir has to be compiled at least once. Therefore, after the first
%% compile any compile processing the sub project will fail because
%% needs_compile/3 will always return true for the non-existent 'first'
%% file.
?assertMatch({ok, _}, retest_sh:run("./rebar clean", [])),
?assertMatch({ok, _}, retest_sh:run("./rebar compile", [])),
ok = check_beams(true),
?assertMatch({ok, _}, retest_sh:run("./rebar compile", [])),
ok = check_beams(true),
ok. ok.
check_beams(Exist) -> check_beams(Exist) ->

View file

@ -1 +1,3 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-define(FOO_EXTRA, foo_extra). -define(FOO_EXTRA, foo_extra).

View file

@ -1,3 +1,5 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-module(foo_sup). -module(foo_sup).
-behavior(supervisor). -behavior(supervisor).

8
inttest/erlc/foobar.erl Normal file
View file

@ -0,0 +1,8 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-module(foobar).
-export([test/0]).
test() ->
true.

View file

@ -1 +1,3 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-define(FOO_CORE, foo_core). -define(FOO_CORE, foo_core).

View file

@ -1,6 +1,6 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et %% ex: ts=4 sw=4 ft=erlang et
{erl_first_files, ["first_xrl.erl", "first_yrl.erl"]}. {erl_first_files, ["src/first_xrl.erl", "src/first_yrl.erl"]}.
{erl_opts, {erl_opts,
[ [

View file

@ -1,6 +1,9 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- %% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et %% ex: ts=4 sw=4 ft=erlang et
{erl_first_files, ["first_xrl.erl", "first_yrl.erl"]}. {erl_first_files,
["src/first_xrl.erl", "src/first_yrl.erl", "src/first_erl.erl"]}.
{deps, [foobar]}.
{erl_opts, {erl_opts,
[ [

View file

@ -0,0 +1,10 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et
-module(first_erl).
-include_lib("eunit/include/eunit.hrl").
-export([test/0]).
test() ->
?debugHere.

View file

@ -21,7 +21,7 @@
%% Erlang files to compile before the rest. Rebar automatically compiles %% Erlang files to compile before the rest. Rebar automatically compiles
%% parse_transforms and custom behaviours before anything other than the files %% parse_transforms and custom behaviours before anything other than the files
%% in this list. %% in this list.
{erl_first_files, ["mymib1", "mymib2"]}. {erl_first_files, ["src/mymib1.erl", "src/mymib2.erl"]}.
%% Erlang compiler options %% Erlang compiler options
{erl_opts, [no_debug_info, {erl_opts, [no_debug_info,

View file

@ -216,7 +216,7 @@ info_help(Description) ->
"(linux|solaris|freebsd|darwin)", 'HAVE_SENDFILE'}, "(linux|solaris|freebsd|darwin)", 'HAVE_SENDFILE'},
{platform_define, "(linux|freebsd)", 'BACKLOG', 128}, {platform_define, "(linux|freebsd)", 'BACKLOG', 128},
{platform_define, "R13", 'old_inets'}]}, {platform_define, "R13", 'old_inets'}]},
{erl_first_files, ["mymib1", "mymib2"]}, {erl_first_files, ["src/mymib1.erl", "src/mymib2.erl"]},
{mib_opts, []}, {mib_opts, []},
{mib_first_files, []}, {mib_first_files, []},
{xrl_opts, []}, {xrl_opts, []},
@ -230,6 +230,13 @@ test_compile_config_and_opts(Config, ErlOpts, Cmd) ->
{Config2, PropErOpts} = proper_opts(Config1), {Config2, PropErOpts} = proper_opts(Config1),
{Config3, EqcOpts} = eqc_opts(Config2), {Config3, EqcOpts} = eqc_opts(Config2),
%% NOTE: For consistency, all *_first_files lists should be
%% retrieved via rebar_config:get_local. Right now
%% erl_first_files, eunit_first_files, and qc_first_files use
%% rebar_config:get_list and are inherited, but xrl_first_files
%% and yrl_first_files use rebar_config:get_local. Inheritance of
%% *_first_files is questionable as the file would need to exist
%% in all project directories for it to work.
OptsAtom = list_to_atom(Cmd ++ "_compile_opts"), OptsAtom = list_to_atom(Cmd ++ "_compile_opts"),
TestOpts = rebar_config:get_list(Config3, OptsAtom, []), TestOpts = rebar_config:get_list(Config3, OptsAtom, []),
Opts0 = [{d, 'TEST'}] ++ Opts0 = [{d, 'TEST'}] ++
@ -285,20 +292,28 @@ doterl_compile(Config, OutDir) ->
doterl_compile(Config, OutDir, [], ErlOpts). doterl_compile(Config, OutDir, [], ErlOpts).
doterl_compile(Config, OutDir, MoreSources, ErlOpts) -> doterl_compile(Config, OutDir, MoreSources, ErlOpts) ->
ErlFirstFiles = rebar_config:get_list(Config, erl_first_files, []), ErlFirstFilesConf = rebar_config:get_list(Config, erl_first_files, []),
?DEBUG("erl_opts ~p~n", [ErlOpts]), ?DEBUG("erl_opts ~p~n", [ErlOpts]),
%% Support the src_dirs option allowing multiple directories to %% Support the src_dirs option allowing multiple directories to
%% contain erlang source. This might be used, for example, should %% contain erlang source. This might be used, for example, should
%% eunit tests be separated from the core application source. %% eunit tests be separated from the core application source.
SrcDirs = rebar_utils:src_dirs(proplists:append_values(src_dirs, ErlOpts)), SrcDirs = rebar_utils:src_dirs(proplists:append_values(src_dirs, ErlOpts)),
RestErls = [Source || Source <- gather_src(SrcDirs, []) ++ MoreSources, AllErlFiles = gather_src(SrcDirs, []) ++ MoreSources,
not lists:member(Source, ErlFirstFiles)], %% NOTE: If and when erl_first_files is not inherited anymore
%% (rebar_config:get_local instead of rebar_config:get_list), consider
%% logging a warning message for any file listed in erl_first_files which
%% wasn't found via gather_src.
{ErlFirstFiles, RestErls} =
lists:partition(
fun(Source) ->
lists:member(Source, ErlFirstFilesConf)
end, AllErlFiles),
%% Make sure that ebin/ exists and is on the path %% Make sure that ebin/ exists and is on the path
ok = filelib:ensure_dir(filename:join("ebin", "dummy.beam")), ok = filelib:ensure_dir(filename:join("ebin", "dummy.beam")),
CurrPath = code:get_path(), CurrPath = code:get_path(),
true = code:add_path(filename:absname("ebin")), true = code:add_path(filename:absname("ebin")),
OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir), OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir),
G = init_erlcinfo(Config, RestErls), G = init_erlcinfo(Config, AllErlFiles),
%% Split RestErls so that files which are depended on are treated %% Split RestErls so that files which are depended on are treated
%% like erl_first_files. %% like erl_first_files.
{OtherFirstErls, OtherErls} = {OtherFirstErls, OtherErls} =
@ -395,8 +410,8 @@ check_erlcinfo(Config, _) ->
?ABORT("~s file is invalid. Please delete before next run.~n", ?ABORT("~s file is invalid. Please delete before next run.~n",
[erlcinfo_file(Config)]). [erlcinfo_file(Config)]).
erlcinfo_file(Config) -> erlcinfo_file(_Config) ->
filename:join([rebar_utils:base_dir(Config), ".rebar", ?ERLCINFO_FILE]). filename:join([rebar_utils:get_cwd(), ".rebar", ?ERLCINFO_FILE]).
init_erlcinfo(Config, Erls) -> init_erlcinfo(Config, Erls) ->
G = restore_erlcinfo(Config), G = restore_erlcinfo(Config),