Skip to content

Commit 6415955

Browse files
authored
Consider mix alises in umbrella projects (#12576)
Previously, when an alias existed within an umbrella child, running this from the umbrella root would result in an error that the mix task was not found. This commit checks the aliases of the children, and if any of the children have the alias then it will be executed in the children. Any children that do not have the alias will be skipped.
1 parent 1932c89 commit 6415955

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

lib/mix/lib/mix/task.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ defmodule Mix.Task do
367367
alias && Mix.TasksServer.run({:alias, task, proj}) ->
368368
run_alias(List.wrap(alias), args, proj, task, apps, :ok)
369369

370+
Mix.Project.umbrella?() && umbrella_child_alias?(task) ->
371+
Mix.ProjectStack.recur(fn ->
372+
recur(
373+
fn _ -> Mix.Project.config()[:aliases][String.to_atom(task)] && run(task, args) end,
374+
apps
375+
)
376+
end)
377+
370378
!Mix.TasksServer.get({:task, task, proj}) ->
371379
run_task(proj, task, args, apps)
372380

@@ -375,6 +383,18 @@ defmodule Mix.Task do
375383
end
376384
end
377385

386+
defp umbrella_child_alias?(task) do
387+
umbrella_deps =
388+
Mix.Dep.Umbrella.cached()
389+
|> Enum.filter(& &1.opts[:from_umbrella])
390+
391+
Enum.find(umbrella_deps, fn %Mix.Dep{app: app, opts: opts} ->
392+
Mix.Project.in_project(app, opts[:path], [inherit_parent_config_files: true], fn _ ->
393+
!!Mix.Project.config()[:aliases][String.to_atom(task)]
394+
end)
395+
end)
396+
end
397+
378398
defp run_task(proj, task, args, apps) do
379399
# 1. If the task is available, we run it.
380400
# 2. Otherwise we compile and load dependencies

lib/mix/test/fixtures/umbrella_test/apps/bar/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ defmodule Bar.MixProject do
88
# Choose something besides *_test.exs so that these test files don't
99
# get accidentally swept up into the actual Mix test suite.
1010
test_pattern: "*_tests.exs",
11-
test_coverage: [ignore_modules: [Bar, ~r/Ignore/]]
11+
test_coverage: [ignore_modules: [Bar, ~r/Ignore/]],
12+
aliases: [mytask: ["cmd echo bar_running"]]
1213
]
1314
end
1415
end

lib/mix/test/fixtures/umbrella_test/apps/foo/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule Foo.MixProject do
77
version: "0.1.0",
88
# Choose something besides *_test.exs so that these test files don't
99
# get accidentally swept up into the actual Mix test suite.
10-
test_pattern: "*_tests.exs"
10+
test_pattern: "*_tests.exs",
11+
aliases: [mytask: ["cmd echo foo_running"]]
1112
]
1213
end
1314
end

lib/mix/test/mix/task_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,40 @@ defmodule Mix.TaskTest do
160160
end)
161161
end
162162

163+
test "aliases considered for umbrella apps" do
164+
in_fixture("umbrella_test", fn ->
165+
Mix.Project.in_project(:umbrella, ".", fn _ ->
166+
File.mkdir_p!("apps/baz/lib/")
167+
168+
File.write!("apps/baz/mix.exs", """
169+
defmodule Baz.MixProject do
170+
use Mix.Project
171+
172+
def project do
173+
[
174+
app: :baz,
175+
version: "0.1.0"
176+
]
177+
end
178+
end
179+
""")
180+
181+
File.write!("apps/baz/lib/baz.ex", """
182+
defmodule Baz do
183+
def hello do
184+
:world
185+
end
186+
end
187+
""")
188+
189+
assert [:ok, nil, :ok] = Mix.Task.run("mytask")
190+
191+
assert_received {:mix_shell, :run, ["foo_running" <> _]}
192+
assert_received {:mix_shell, :run, ["bar_running" <> _]}
193+
end)
194+
end)
195+
end
196+
163197
test "reenable/1 for recursive inside umbrella child" do
164198
in_fixture("umbrella_dep/deps/umbrella", fn ->
165199
Mix.Project.in_project(:umbrella, ".", fn _ ->

0 commit comments

Comments
 (0)