Skip to content

Commit 7d42771

Browse files
GazlerJosé Valim
authored andcommitted
Fix nested use of Mix.Config.eval!/2 (#7925)
Previously, if a config file uses Mix.Config.eval!/2 then it would fail with the following error: > ** (RuntimeError) could not set configuration via Mix.Config. This usually > means you are trying to execute a configuration file directly instead of > using the proper command, such as mix loadconfig > code: assert Mix.Config.eval!(fixture_path("configs/nested_import.exs")) == > stacktrace: > (mix) lib/mix/config.ex:71: Mix.Config.raise_improper_use!/0 > (mix) lib/mix/config.ex:222: Mix.Config.eval!/2 > lib/mix/test/mix/config_test.exs:110: (test) Signed-off-by: José Valim <[email protected]>
1 parent 9812b74 commit 7d42771

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

lib/mix/lib/mix/config.ex

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ defmodule Mix.Config do
4747

4848
defp put_config(value) do
4949
Process.put(@config_key, value)
50-
:ok
5150
end
5251

5352
defp delete_config() do
@@ -60,7 +59,6 @@ defmodule Mix.Config do
6059

6160
defp put_files(value) do
6261
Process.put(@files_key, value)
63-
:ok
6462
end
6563

6664
defp delete_files() do
@@ -215,20 +213,23 @@ defmodule Mix.Config do
215213
It returns a tuple with the configuration and the imported paths.
216214
"""
217215
def eval!(file, imported_paths \\ []) do
218-
put_config([])
219-
put_files(imported_paths)
220-
{eval_config, _} = eval_config!(Path.expand(file))
216+
previous_config = put_config([])
217+
previous_files = put_files(imported_paths)
221218

222-
case get_config!() do
223-
[] when is_list(eval_config) ->
224-
{validate!(eval_config), get_files!()}
219+
try do
220+
{eval_config, _} = eval_config!(Path.expand(file))
225221

226-
pdict_config ->
227-
{pdict_config, get_files!()}
222+
case get_config!() do
223+
[] when is_list(eval_config) ->
224+
{validate!(eval_config), get_files!()}
225+
226+
pdict_config ->
227+
{pdict_config, get_files!()}
228+
end
229+
after
230+
if previous_config, do: put_config(previous_config), else: delete_config()
231+
if previous_files, do: put_files(previous_files), else: delete_files()
228232
end
229-
after
230-
delete_config()
231-
delete_files()
232233
end
233234

234235
@doc false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use Mix.Config
2+
Mix.Config.eval!(Path.join([__DIR__, "good_config.exs"]))
3+
:done

lib/mix/test/mix/config_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ defmodule Mix.ConfigTest do
107107
{[my_app: [key: :value]],
108108
[fixture_path("configs/good_config.exs"), fixture_path("configs/good_import.exs")]}
109109

110+
assert Mix.Config.eval!(fixture_path("configs/nested_import.exs")) ==
111+
{[], [fixture_path("configs/nested_import.exs")]}
112+
110113
assert_raise ArgumentError,
111114
~r"expected runtime config for app :sample to return keyword list",
112115
fn -> Mix.Config.eval!(fixture_path("configs/bad_app.exs")) end

0 commit comments

Comments
 (0)