Skip to content

Commit 24e3c2e

Browse files
committed
Add support for Mix.install/2 in deps.partition
1 parent a616cc4 commit 24e3c2e

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

lib/mix/lib/mix.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ defmodule Mix do
420420
421421
"""
422422

423-
@mix_install_project __MODULE__.InstallProject
423+
@mix_install_project Mix.InstallProject
424424
@mix_install_app :mix_install
425425
@mix_install_app_string Atom.to_string(@mix_install_app)
426426

@@ -905,9 +905,7 @@ defmodule Mix do
905905

906906
case Mix.State.get(:installed) do
907907
nil ->
908-
Application.put_all_env(config, persistent: true)
909908
System.put_env(system_env)
910-
911909
install_project_dir = install_project_dir(id)
912910

913911
if Keyword.fetch!(opts, :verbose) do
@@ -924,10 +922,14 @@ defmodule Mix do
924922
config_path: config_path
925923
]
926924

927-
config = install_project_config(dynamic_config)
925+
:ok =
926+
Mix.ProjectStack.push(
927+
@mix_install_project,
928+
[compile_config: config] ++ install_project_config(dynamic_config),
929+
"nofile"
930+
)
928931

929932
started_apps = Application.started_applications()
930-
:ok = Mix.ProjectStack.push(@mix_install_project, config, "nofile")
931933
build_dir = Path.join(install_project_dir, "_build")
932934
external_lockfile = expand_path(opts[:lockfile], deps, :lockfile, "mix.lock")
933935

@@ -944,9 +946,9 @@ defmodule Mix do
944946
File.mkdir_p!(install_project_dir)
945947

946948
File.cd!(install_project_dir, fn ->
947-
if config_path do
948-
Mix.Task.rerun("loadconfig")
949-
end
949+
# This steps need to be mirror in mix deps.partition
950+
Application.put_all_env(config, persistent: true)
951+
Mix.Task.rerun("loadconfig")
950952

951953
cond do
952954
external_lockfile ->
@@ -1079,7 +1081,7 @@ defmodule Mix do
10791081

10801082
defp install_project_config(dynamic_config) do
10811083
[
1082-
version: "0.1.0",
1084+
version: "1.0.0",
10831085
build_per_environment: true,
10841086
build_path: "_build",
10851087
lockfile: "mix.lock",

lib/mix/lib/mix/tasks/deps.partition.ex

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule Mix.Tasks.Deps.Partition do
77

88
## Server
99

10+
@deps_partition_install_mix_exs ~c"deps.partition.mix.exs"
11+
1012
def server(deps, count, force?) do
1113
{:ok, socket} = :gen_tcp.listen(0, [:binary, packet: :line, active: true, reuseaddr: true])
1214

@@ -26,6 +28,31 @@ defmodule Mix.Tasks.Deps.Partition do
2628
ansi_flag = if IO.ANSI.enabled?(), do: ~c"--color", else: ~c"--no-color"
2729
force_flag = if force?, do: ~c"--force", else: ~c"--no-force"
2830

31+
env_vars =
32+
if Mix.install?() do
33+
blob =
34+
Mix.Project.config()
35+
|> :erlang.term_to_binary()
36+
|> :binary.bin_to_list()
37+
|> Enum.join(",")
38+
39+
# We replicate the initialization logic from Mix.install/2 as part of mix.exs
40+
File.write!(@deps_partition_install_mix_exs, """
41+
config = <<#{blob}>>
42+
project = :erlang.binary_to_term(config)
43+
44+
if compile_config = project[:compile_config] do
45+
Application.put_all_env(compile_config, persistent: true)
46+
end
47+
48+
Mix.ProjectStack.push(Mix.InstallProject, project, "nofile")
49+
""")
50+
51+
[{~c"MIX_EXS", @deps_partition_install_mix_exs}]
52+
else
53+
[]
54+
end
55+
2956
args = [
3057
ansi_flag,
3158
~c"-e",
@@ -45,7 +72,7 @@ defmodule Mix.Tasks.Deps.Partition do
4572
:stderr_to_stdout,
4673
line: 1_000_000,
4774
args: args,
48-
env: [{~c"MIX_OS_CONCURRENCY_LOCK", ~c"false"}]
75+
env: [{~c"MIX_OS_CONCURRENCY_LOCK", ~c"false"} | env_vars]
4976
]
5077

5178
clients =
@@ -123,11 +150,11 @@ defmodule Mix.Tasks.Deps.Partition do
123150

124151
{:tcp_closed, socket} ->
125152
shutdown_clients(available ++ busy)
126-
Mix.raise("ERROR! mix deps.partition #{inspect(socket)} closed unexpectedly")
153+
Mix.raise("mix deps.partition #{inspect(socket)} closed unexpectedly")
127154

128155
{:tcp_error, socket, error} ->
129156
shutdown_clients(available ++ busy)
130-
Mix.raise("ERROR! mix deps.partition #{inspect(socket)} errored: #{inspect(error)}")
157+
Mix.raise("mix deps.partition #{inspect(socket)} errored: #{inspect(error)}")
131158

132159
{port, {:data, {eol, data}}} ->
133160
with %{index: index} <-
@@ -164,7 +191,12 @@ defmodule Mix.Tasks.Deps.Partition do
164191
{^port, {:data, {:noeol, data}}} -> [data | close_port(port, prefix)]
165192
after
166193
0 ->
167-
Port.close(port)
194+
try do
195+
Port.close(port)
196+
catch
197+
_, _ -> :ok
198+
end
199+
168200
[]
169201
end
170202
end

lib/mix/lib/mix/tasks/loadconfig.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ defmodule Mix.Tasks.Loadconfig do
4040
end
4141

4242
defp load_default do
43-
config = Mix.Project.config()
43+
config_path = Mix.Project.config()[:config_path]
4444

45-
if File.regular?(config[:config_path]) or config[:config_path] != "config/config.exs" do
46-
load_compile(config[:config_path])
45+
if config_path != nil and (File.regular?(config_path) or config_path != "config/config.exs") do
46+
load_compile(config_path)
4747
else
4848
[]
4949
end

lib/mix/test/mix/tasks/loadconfig_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ defmodule Mix.Tasks.LoadconfigTest do
6767
end)
6868
end
6969

70+
test "is a no-op with nil custom config_path" do
71+
Mix.ProjectStack.post_config(config_path: nil)
72+
assert Mix.Task.run("loadconfig", []) == []
73+
end
74+
7075
test "updates config files and config mtime", context do
7176
in_tmp(context.test, fn ->
7277
Mix.Project.push(MixTest.Case.Sample)

0 commit comments

Comments
 (0)