Skip to content

Commit 7d57dc9

Browse files
author
José Valim
committed
Revert "Ensure project's Logger config is used when running Mix tasks (#9332)"
This reverts commit 78551c1. Closes #9675.
1 parent 7f86ea3 commit 7d57dc9

File tree

3 files changed

+18
-88
lines changed

3 files changed

+18
-88
lines changed

lib/mix/lib/mix/cli.ex

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Mix.CLI do
22
@moduledoc false
33

4-
@compile {:no_warn_undefined, Logger.App}
5-
64
@doc """
75
Runs Mix according to the command line arguments.
86
"""
@@ -78,8 +76,6 @@ defmodule Mix.CLI do
7876
try do
7977
ensure_no_slashes(name)
8078
Mix.Task.run("loadconfig")
81-
# Restart Logger so it uses the configuration of the project.
82-
if logger_configured?(), do: restart_logger()
8379
Mix.Task.run(name, args)
8480
rescue
8581
# We only rescue exceptions in the Mix namespace, all
@@ -95,18 +91,6 @@ defmodule Mix.CLI do
9591
end
9692
end
9793

98-
defp logger_configured? do
99-
:logger in Mix.ProjectStack.config_apps()
100-
end
101-
102-
defp restart_logger do
103-
# Mix should not depend directly on Logger, that's why we first check if it's loaded.
104-
if Process.whereis(Logger) do
105-
Logger.App.stop()
106-
:ok = Logger.App.start()
107-
end
108-
end
109-
11094
defp env_variable_activated?(name) do
11195
System.get_env(name) in ~w(1 true)
11296
end

lib/mix/lib/mix/tasks/app.start.ex

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,25 @@ defmodule Mix.Tasks.App.Start do
7070
end
7171
end
7272

73-
unless "--no-start" in args do
74-
apps = apps(config)
75-
76-
# Stop Logger if the application does not depend on it.
77-
#
78-
# Mix should not depend directly on Logger, that's why we first check if it's loaded.
79-
if not logger_dependency?(apps) && Process.whereis(Logger), do: Logger.App.stop()
73+
# Stop Logger when starting the application as it is
74+
# up to the application to decide if it should be restarted
75+
# or not.
76+
#
77+
# Mix should not depend directly on Logger so check that it's loaded.
78+
logger = Process.whereis(Logger)
79+
80+
if logger do
81+
Logger.App.stop()
82+
end
8083

81-
start(apps, type(config, opts), "--no-validate-compile-env" not in args)
84+
if "--no-start" in args do
85+
# Start Logger again if the application won't be starting it
86+
if logger do
87+
:ok = Logger.App.start()
88+
end
89+
else
90+
config = Mix.Project.config()
91+
start(apps(config), type(config, opts), "--no-validate-compile-env" not in args)
8292

8393
# If there is a build path, we will let the application
8494
# that owns the build path do the actual check
@@ -215,31 +225,6 @@ defmodule Mix.Tasks.App.Start do
215225
:ok
216226
end
217227

218-
defp logger_dependency?(apps), do: logger_dependency?(apps, [])
219-
220-
defp logger_dependency?([], _checked_apps) do
221-
false
222-
end
223-
224-
defp logger_dependency?([app | apps], checked_apps) do
225-
deps =
226-
List.wrap(Application.spec(app, :included_applications)) ++
227-
List.wrap(Application.spec(app, :applications))
228-
229-
cond do
230-
deps == [] ->
231-
logger_dependency?(apps, [app | checked_apps])
232-
233-
:logger in deps ->
234-
true
235-
236-
true ->
237-
checked_apps = [app | checked_apps]
238-
new_apps = (deps -- apps) -- checked_apps
239-
logger_dependency?(apps ++ new_apps, checked_apps)
240-
end
241-
end
242-
243228
defp load_protocol(file) do
244229
case file do
245230
"Elixir." <> _ ->

lib/mix/test/mix/cli_test.exs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,45 +67,6 @@ defmodule Mix.CLITest do
6767
end)
6868
end
6969

70-
test "runs the task using the Logger configuration of the project", context do
71-
in_tmp(context.test, fn ->
72-
File.mkdir_p!("lib")
73-
74-
File.write!("mix.exs", """
75-
defmodule MyProject do
76-
use Mix.Project
77-
78-
def project do
79-
[app: :my_project, version: "0.0.1"]
80-
end
81-
end
82-
""")
83-
84-
File.mkdir_p!("config")
85-
86-
File.write!("config/config.exs", """
87-
import Config
88-
89-
config :logger, :console, format: "TEST[$level] $message\n"
90-
""")
91-
92-
File.write!("lib/hello.ex", """
93-
defmodule Mix.Tasks.LogHello do
94-
use Mix.Task
95-
96-
require Logger
97-
98-
def run(_) do
99-
Logger.info("HELLO")
100-
end
101-
end
102-
""")
103-
104-
contents = mix(~w[log_hello])
105-
assert contents =~ "TEST[info] HELLO\n"
106-
end)
107-
end
108-
10970
test "no task error", context do
11071
in_tmp(context.test, fn ->
11172
contents = mix(~w[no_task])

0 commit comments

Comments
 (0)