Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incremental compilation for Mix.install/2 #13756

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,9 @@ defmodule Mix do
consolidate_protocols? = Keyword.fetch!(opts, :consolidate_protocols)
start_applications? = Keyword.fetch!(opts, :start_applications)

id =
{deps, config, system_env, consolidate_protocols?}
|> :erlang.term_to_binary()
|> :erlang.md5()
|> Base.encode16(case: :lower)
id_without_deps = generate_id({config, system_env, consolidate_protocols?})
deps_id = generate_id({deps})
id = id_without_deps <> deps_id

force? = System.get_env("MIX_INSTALL_FORCE") in ["1", "true"] or Keyword.fetch!(opts, :force)

Expand All @@ -874,6 +872,7 @@ defmodule Mix do
System.put_env(system_env)

install_project_dir = install_project_dir(id)
latest_cache_dir = latest_cache_dir(id_without_deps)

if Keyword.fetch!(opts, :verbose) do
Mix.shell().info("Mix.install/2 using #{install_project_dir}")
Expand All @@ -899,7 +898,7 @@ defmodule Mix do
try do
first_build? = not File.dir?(build_dir)

restore_dir = System.get_env("MIX_INSTALL_RESTORE_PROJECT_DIR")
restore_dir = System.get_env("MIX_INSTALL_RESTORE_PROJECT_DIR") || latest_cache_dir

if first_build? and restore_dir != nil and not force? do
File.cp_r(restore_dir, install_project_dir)
Expand Down Expand Up @@ -1005,6 +1004,13 @@ defmodule Mix do
Path.join(app_dir, relative_path)
end

defp generate_id(info) do
info
|> :erlang.term_to_binary()
|> :erlang.md5()
|> Base.encode16(case: :lower)
end

defp remove_leftover_deps(install_project_dir) do
build_lib_dir = Path.join([install_project_dir, "_build", "dev", "lib"])

Expand Down Expand Up @@ -1033,13 +1039,34 @@ defmodule Mix do
File.rm_rf(dep_path)
end

defp install_project_dir(cache_id) do
defp install_project_dir(cache_id), do: Path.join([version_dir(), cache_id])

defp latest_cache_dir(id_without_deps) do
version_dir = version_dir()

with {:ok, entries} <- File.ls(version_dir) do
entries
|> Enum.filter(&String.starts_with?(&1, id_without_deps))
|> Enum.map(&Path.join(version_dir, &1))
|> Enum.map(&{&1, File.stat!(&1).mtime})
|> Enum.max_by(&elem(&1, 1), fn -> nil end)
|> case do
{latest_dir, _mtime} -> latest_dir
nil -> nil
end
Comment on lines +1048 to +1056
Copy link
Contributor

@dkuku dkuku Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this would be easier to read?
I lost track on 1052, I'm just guessing what should be the result.

Suggested change
entries
|> Enum.filter(&String.starts_with?(&1, id_without_deps))
|> Enum.map(&Path.join(version_dir, &1))
|> Enum.map(&{&1, File.stat!(&1).mtime})
|> Enum.max_by(&elem(&1, 1), fn -> nil end)
|> case do
{latest_dir, _mtime} -> latest_dir
nil -> nil
end
for entry <- entries,
String.starts_with?(entry, id_without_deps),
current_path = Path.join(version_dir, entry),
%{mtime: current_mtime} = File.stat!(current_path),
reduce: {nil, nil} do
{nil, nil} -> {current_path, current_mtime}
{_path, mtime} when current_mtime > mtime -> {current_path, current_mtime}
acc -> acc
end
|> elem(0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH for me, I like pipe operator more, but thank you for advice.

else
_ -> nil
end
end

defp version_dir() do
install_root =
System.get_env("MIX_INSTALL_DIR") ||
Path.join(Mix.Utils.mix_cache(), "installs")

version = "elixir-#{System.version()}-erts-#{:erlang.system_info(:version)}"
Path.join([install_root, version, cache_id])

Path.join([install_root, version])
end

defp install_project_config(dynamic_config) do
Expand Down
63 changes: 62 additions & 1 deletion lib/mix/test/mix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,68 @@ defmodule MixTest do
end
end

test "using restore dir", %{tmp_dir: tmp_dir} do
test "incremental compilation by latest version", %{tmp_dir: tmp_dir} do
with_cleanup(fn ->
Mix.install([
{:git_repo, git: fixture_path("git_repo")}
])

assert_received {:mix_shell, :info, ["* Getting git_repo " <> _]}
assert_received {:mix_shell, :info, ["==> git_repo"]}
assert_received {:mix_shell, :info, ["Compiling 1 file (.ex)"]}
assert_received {:mix_shell, :info, ["Generated git_repo app"]}
refute_received _

install_project_dir = Mix.install_project_dir()
build_lib_path = Path.join([install_project_dir, "_build", "dev", "lib"])
deps_path = Path.join([install_project_dir, "deps"])

assert File.ls!(build_lib_path) |> Enum.sort() == ["git_repo", "mix_install"]
assert File.ls!(deps_path) == ["git_repo"]
end)

# Adding a dependency

with_cleanup(fn ->
Mix.install([
{:git_repo, git: fixture_path("git_repo")},
{:install_test, path: Path.join(tmp_dir, "install_test")}
])

assert_received {:mix_shell, :info, ["==> install_test"]}
assert_received {:mix_shell, :info, ["Compiling 2 files (.ex)"]}
assert_received {:mix_shell, :info, ["Generated install_test app"]}
refute_received _

install_project_dir = Mix.install_project_dir()
build_lib_path = Path.join([install_project_dir, "_build", "dev", "lib"])
deps_path = Path.join([install_project_dir, "deps"])

assert File.ls!(build_lib_path) |> Enum.sort() ==
["git_repo", "install_test", "mix_install"]

assert File.ls!(deps_path) == ["git_repo"]
end)

# Removing a dependency

with_cleanup(fn ->
Mix.install([
{:install_test, path: Path.join(tmp_dir, "install_test")}
])

refute_received _

install_project_dir = Mix.install_project_dir()
build_lib_path = Path.join([install_project_dir, "_build", "dev", "lib"])
deps_path = Path.join([install_project_dir, "deps"])

assert File.ls!(build_lib_path) |> Enum.sort() == ["install_test", "mix_install"]
assert File.ls!(deps_path) == []
end)
end

test "using restore dir by export environment variable", %{tmp_dir: tmp_dir} do
with_cleanup(fn ->
Mix.install([
{:git_repo, git: fixture_path("git_repo")}
Expand Down