Skip to content

Commit 689f14a

Browse files
committed
Always compile project if task cannot be found
Closes #2704.
1 parent aa5b414 commit 689f14a

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [GenServer] Do not deliver out of order messages on `GenServer.cast/2` on distributed mode
1515
* [Mix] Do not pre-compile a Mix project if an alias was found
1616
* [Mix] Properly handle compilation errors in the Erlang compiler
17+
* [Mix] Always try to compile project if task cannot be found
1718

1819
* Deprecations
1920
* [Collectable] Deprecate `Collectable.empty/1` and `Enum.traverse/2`

lib/mix/lib/mix/cli.ex

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,7 @@ defmodule Mix.CLI do
5252
defp run_task(name, args) do
5353
try do
5454
Mix.Task.run "loadconfig"
55-
56-
# If the task is not available, let's try to
57-
# compile the repository and then run it again.
58-
cond do
59-
Mix.Task.get(name) || Mix.Task.alias?(name) ->
60-
Mix.Task.run(name, args)
61-
Mix.Project.get ->
62-
Mix.Task.run("compile")
63-
Mix.Task.run(name, args)
64-
true ->
65-
# Raise no task error
66-
Mix.Task.get!(name)
67-
end
55+
Mix.Task.run name, args
6856
rescue
6957
# We only rescue exceptions in the mix namespace, all
7058
# others pass through and will explode on the users face

lib/mix/lib/mix/task.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ defmodule Mix.Task do
244244
end
245245

246246
defp run_task(proj, task, args) do
247-
module = get!(task)
247+
module = get(task)
248+
249+
# If the task is not available, let's try to compile the project
250+
unless module do
251+
if proj, do: Mix.Task.run("compile")
252+
module = get!(task)
253+
end
248254

249255
if recursive(module) and Mix.Project.umbrella? and Mix.ProjectStack.enable_recursion do
250256
res = recur(fn _ -> run(task, args) end)

lib/mix/test/mix/task_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ Code.require_file "../test_helper.exs", __DIR__
33
defmodule Mix.TaskTest do
44
use MixTest.Case
55

6+
defmodule SampleProject do
7+
def project do
8+
[app: :sample, version: "0.0.1"]
9+
end
10+
end
11+
612
test "run/1" do
713
assert Mix.Task.run("hello") == "Hello, World!"
814
assert Mix.Task.run("hello") == :noop
@@ -16,6 +22,17 @@ defmodule Mix.TaskTest do
1622
end
1723
end
1824

25+
test "try to compile if task is missing" do
26+
Mix.Project.push(SampleProject, "sample")
27+
28+
assert_raise Mix.NoTaskError, fn ->
29+
Mix.Task.run("unknown")
30+
end
31+
32+
# Check if compile task have run
33+
refute Mix.TasksServer.run({:task, "compile", Mix.Project.get})
34+
end
35+
1936
test "clear/0" do
2037
assert Mix.Task.run("hello") == "Hello, World!"
2138
Mix.Task.clear

0 commit comments

Comments
 (0)