Skip to content

Commit f7b179a

Browse files
committed
Ensure system_env is applied for Elixir deps, closes #15095
1 parent 426a429 commit f7b179a

2 files changed

Lines changed: 52 additions & 23 deletions

File tree

lib/mix/lib/mix/tasks/deps.compile.ex

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,38 @@ defmodule Mix.Tasks.Deps.Compile do
209209
end
210210

211211
defp do_mix(dep, _config) do
212-
Mix.Dep.in_dependency(dep, fn _ ->
213-
config = Mix.Project.config()
214-
215-
if req = old_elixir_req(config) do
216-
Mix.shell().error(
217-
"warning: the dependency #{inspect(dep.app)} requires Elixir #{inspect(req)} " <>
218-
"but you are running on v#{System.version()}"
219-
)
220-
end
221-
222-
try do
223-
options = ["--from-mix-deps-compile", "--no-warnings-as-errors", "--no-code-path-pruning"]
224-
res = Mix.Task.run("compile", options)
225-
match?({:ok, _}, res)
226-
catch
227-
kind, reason ->
228-
app = dep.app
212+
Mix.Dep.Loader.with_system_env(dep, fn ->
213+
Mix.Dep.in_dependency(dep, fn _ ->
214+
config = Mix.Project.config()
229215

216+
if req = old_elixir_req(config) do
230217
Mix.shell().error(
231-
"could not compile dependency #{inspect(app)}, \"mix compile\" failed. " <>
232-
deps_compile_feedback(app)
218+
"warning: the dependency #{inspect(dep.app)} requires Elixir #{inspect(req)} " <>
219+
"but you are running on v#{System.version()}"
233220
)
234-
235-
:erlang.raise(kind, reason, __STACKTRACE__)
236-
end
221+
end
222+
223+
try do
224+
options = [
225+
"--from-mix-deps-compile",
226+
"--no-warnings-as-errors",
227+
"--no-code-path-pruning"
228+
]
229+
230+
res = Mix.Task.run("compile", options)
231+
match?({:ok, _}, res)
232+
catch
233+
kind, reason ->
234+
app = dep.app
235+
236+
Mix.shell().error(
237+
"could not compile dependency #{inspect(app)}, \"mix compile\" failed. " <>
238+
deps_compile_feedback(app)
239+
)
240+
241+
:erlang.raise(kind, reason, __STACKTRACE__)
242+
end
243+
end)
237244
end)
238245
end
239246

lib/mix/test/mix/dep_test.exs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,29 @@ defmodule Mix.DepTest do
355355
end)
356356
end
357357

358-
test "deps with system_env set" do
358+
@compile {:no_warn_undefined, [{A, :env, 0}]}
359+
test "mix dep with system_env set" do
360+
system_env = [{"CONTENTS_FROM_ENV", "contents dep test"}]
361+
deps = [{:ok, "~> 0.1", path: "deps/ok", system_env: system_env}]
362+
363+
with_deps(deps, fn ->
364+
in_fixture("deps_status", fn ->
365+
File.mkdir_p!("deps/ok/lib")
366+
367+
File.write!("deps/ok/lib/a.ex", """
368+
defmodule A do
369+
def env, do: unquote(System.fetch_env!("CONTENTS_FROM_ENV"))
370+
end
371+
""")
372+
373+
Mix.Tasks.Deps.Get.run([])
374+
Mix.Tasks.Deps.Compile.run([])
375+
assert A.env() == "contents dep test"
376+
end)
377+
end)
378+
end
379+
380+
test "rebar dep with system_env set" do
359381
file_path = tmp_path("load dependency with env vars/dep-test")
360382
dep_path = tmp_path("rebar_dep")
361383

0 commit comments

Comments
 (0)