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

Fix global cache config overriding #2683

Merged
merged 3 commits into from
Feb 19, 2022
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
20 changes: 14 additions & 6 deletions src/rebar3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ run_aux(State, RawArgs) ->
State5 = case os:getenv("REBAR_CACHE_DIR") of
false ->
State4;
ConfigFile ->
rebar_state:set(State4, global_rebar_dir, ConfigFile)
CachePath ->
rebar_state:set(State4, global_rebar_dir, CachePath)
end,

{ok, Providers} = application:get_env(rebar, providers),
Expand Down Expand Up @@ -207,20 +207,21 @@ init_config() ->

Config = rebar_config:consult_root(),
Config1 = rebar_config:merge_locks(Config, rebar_config:consult_lock_file(?LOCK_FILE)),
InitState = rebar_state:new(Config1),

%% If $HOME/.config/rebar3/rebar.config exists load and use as global config
GlobalConfigFile = rebar_dir:global_config(),
GlobalConfigFile = rebar_dir:global_config(InitState),
State = case filelib:is_regular(GlobalConfigFile) of
true ->
?DEBUG("Load global config file ~ts", [GlobalConfigFile]),
try state_from_global_config(Config1, GlobalConfigFile)
catch
_:_ ->
?WARN("Global config ~ts exists but can not be read. Ignoring global config values.", [GlobalConfigFile]),
rebar_state:new(Config1)
InitState
end;
false ->
rebar_state:new(Config1)
InitState
end,

%% Determine the location of the rebar executable; important for pulling
Expand Down Expand Up @@ -402,7 +403,14 @@ ensure_running(App, Caller) ->
-spec state_from_global_config([term()], file:filename()) -> rebar_state:t().
state_from_global_config(Config, GlobalConfigFile) ->
GlobalConfigTerms = rebar_config:consult_file(GlobalConfigFile),
GlobalConfig = rebar_state:new(GlobalConfigTerms),
GlobalConfigTmp = rebar_state:new(GlobalConfigTerms),

GlobalConfig = case os:getenv("REBAR_CACHE_DIR") of
false ->
GlobalConfigTmp;
CachePath ->
rebar_state:set(GlobalConfigTmp, global_rebar_dir, CachePath)
end,

%% We don't want to worry about global plugin install state effecting later
%% usage. So we throw away the global profile state used for plugin install.
Expand Down
2 changes: 1 addition & 1 deletion src/rebar_prv_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ init(State) ->

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
GlobalConfigFile = rebar_dir:global_config(),
GlobalConfigFile = rebar_dir:global_config(State),
GlobalConfig = rebar_state:new(rebar_config:consult_file(GlobalConfigFile)),
GlobalPlugins = rebar_state:get(GlobalConfig, plugins, []),
GlobalSrcDirs = rebar_state:get(GlobalConfig, src_dirs, ["src"]),
Expand Down
1 change: 1 addition & 0 deletions test/rebar_plugins_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ compile_global_plugins(Config) ->

meck:new(rebar_dir, [passthrough]),
meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
meck:expect(rebar_dir, global_config, fun(_) -> GlobalConfig end),
meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),

Name = rebar_test_utils:create_random_name("app1_"),
Expand Down