Skip to content

Commit a536143

Browse files
authored
Do not propagate parent config when building archives/escripts (#15097)
Closes #15094.
1 parent 0b1d3fc commit a536143

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

lib/mix/lib/mix/local/installer.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,23 @@ defmodule Mix.Local.Installer do
9191
end
9292

9393
fetch(dep_spec, fn _ ->
94-
local_install(module, module.build(install_spec, opts), opts)
94+
local_install(module, build(module, install_spec, opts), opts)
9595
end)
9696

9797
{path_or_url, src} when path_or_url in [:local, :url] ->
9898
local_install(module, src, opts)
9999

100100
:project ->
101-
local_install(module, module.build(install_spec, opts), opts)
101+
local_install(module, build(module, install_spec, opts), opts)
102102
end
103103
end
104104

105+
defp build(module, install_spec, opts) do
106+
Mix.Tasks.Loadconfig.preserve_config(fn ->
107+
module.build(install_spec, opts)
108+
end)
109+
end
110+
105111
defp task(module) do
106112
Mix.Utils.module_name_to_command(module, 2)
107113
end

lib/mix/lib/mix/state.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ defmodule Mix.State do
4545

4646
## Persistent term cache (persistent, cleared in tests)
4747

48-
def read_cache(key) do
49-
:persistent_term.get({__MODULE__, key}, nil)
48+
def read_cache(key, default \\ nil) do
49+
:persistent_term.get({__MODULE__, key}, default)
5050
end
5151

5252
def write_cache(key, value) do

lib/mix/lib/mix/tasks/loadconfig.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule Mix.Tasks.Loadconfig do
5151

5252
@doc false
5353
def read_compile() do
54-
Mix.State.read_cache(__MODULE__) || []
54+
Mix.State.read_cache(__MODULE__, [])
5555
end
5656

5757
@doc false
@@ -70,6 +70,25 @@ defmodule Mix.Tasks.Loadconfig do
7070
config
7171
end
7272

73+
@doc false
74+
def preserve_config(fun) do
75+
config = read_compile()
76+
77+
try do
78+
delete_all_config(config)
79+
fun.()
80+
after
81+
delete_all_config(read_compile())
82+
Application.put_all_env(config, persistent: true)
83+
end
84+
end
85+
86+
defp delete_all_config(config) do
87+
for {app, kv} <- config, Application.spec(app, :vsn) == nil, {key, _} <- kv do
88+
Application.delete_env(app, key, persistent: true)
89+
end
90+
end
91+
7392
defp hydrate_apps(config) do
7493
for {app, pairs} <- config do
7594
hd(Config.Reader.merge([{app, Application.get_all_env(app)}], [{app, pairs}]))

lib/mix/test/fixtures/escript_test/lib/escript_test.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ defmodule EscriptTest do
3030
:erl_prim_loader.list_dir(~c"#{:code.lib_dir(app)}/priv") |> IO.inspect()
3131
end
3232

33+
@parent Application.compile_env(:foobar, :parent, "NIL")
34+
35+
def main(["--config"]) do
36+
IO.puts(["VALUE=", Application.get_env(:foobar, :value, "TEST")])
37+
IO.puts(["PARENT=", @parent])
38+
end
39+
3340
def main(_argv) do
3441
IO.puts(Application.get_env(:foobar, :value, "TEST"))
3542
end

lib/mix/test/mix/tasks/escript_test.exs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ defmodule Mix.Tasks.EscriptTest do
8383
test "generate escript with compile config" do
8484
in_fixture("escript_test", fn ->
8585
push_project_with_config(Escript)
86-
8786
File.mkdir_p!("config")
8887

8988
File.write!("config/config.exs", ~S"""
@@ -93,14 +92,14 @@ defmodule Mix.Tasks.EscriptTest do
9392

9493
Mix.Tasks.Escript.Build.run([])
9594
assert_received {:mix_shell, :info, ["Generated escript escript_test with MIX_ENV=dev"]}
95+
9696
assert System.cmd("escript", ["escript_test"]) == {"COMPILE dev TARGET host\n", 0}
9797
end)
9898
end
9999

100100
test "generate escript with runtime config" do
101101
in_fixture("escript_test", fn ->
102102
push_project_with_config(Escript)
103-
104103
File.mkdir_p!("config")
105104

106105
File.write!("config/config.exs", """
@@ -127,7 +126,6 @@ defmodule Mix.Tasks.EscriptTest do
127126
test "generate escript with debug information" do
128127
in_fixture("escript_test", fn ->
129128
push_project_with_config(Escript, escript: [main_module: EscriptTest, strip_beams: false])
130-
131129
Mix.Tasks.Escript.Build.run([])
132130

133131
msg = "Generated escript escript_test with MIX_ENV=dev"
@@ -211,7 +209,6 @@ defmodule Mix.Tasks.EscriptTest do
211209
test "generate escript with Erlang and deps" do
212210
in_fixture("escript_test", fn ->
213211
push_project_with_config(EscriptErlangWithDeps)
214-
215212
Mix.Tasks.Escript.Build.run([])
216213

217214
message = "Generated escript escript_test with MIX_ENV=dev"
@@ -261,7 +258,20 @@ defmodule Mix.Tasks.EscriptTest do
261258
end
262259

263260
test "escript install and uninstall" do
264-
File.rm_rf!(tmp_path(".mix/escripts"))
261+
escripts = tmp_path(".mix/escripts")
262+
File.rm_rf!(escripts)
263+
264+
# Configuration in the parent should not impact the escript
265+
in_tmp("config", fn ->
266+
File.mkdir_p!("config")
267+
268+
File.write!("config/config.exs", ~S"""
269+
import Config
270+
config :foobar, :parent, "SET_ON_PARENT"
271+
""")
272+
273+
Mix.Tasks.Loadconfig.run([])
274+
end)
265275

266276
in_fixture("escript_test", fn ->
267277
push_project_with_config(Escript)
@@ -274,6 +284,10 @@ defmodule Mix.Tasks.EscriptTest do
274284
send(self(), {:mix_shell_input, :yes?, true})
275285
Mix.Tasks.Escript.Install.run([])
276286

287+
# execute the script
288+
assert System.cmd("escript", [Path.join(escripts, "escript_test"), "--config"]) ==
289+
{"VALUE=TEST\nPARENT=NIL\n", 0}
290+
277291
# check that it shows in the list
278292
Mix.Tasks.Escript.run([])
279293
assert_received {:mix_shell, :info, ["* escript_test"]}
@@ -400,7 +414,7 @@ defmodule Mix.Tasks.EscriptTest do
400414
end)
401415
after
402416
System.delete_env("MIX_OS_DEPS_COMPILE_PARTITION_COUNT")
403-
purge([SourceRepo.Escript, SourceRepo.MixProject, Mix.Local.Installer.MixProject])
417+
purge([GitRepo, SourceRepo.Escript, SourceRepo.MixProject, Mix.Local.Installer.MixProject])
404418
end
405419
end
406420

0 commit comments

Comments
 (0)