Skip to content

Add environment variable for reusing Mix.install/2 installation #13378

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

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions lib/iex/lib/iex/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ defmodule IEx.Helpers do

Mix.installed?() ->
Mix.in_install_project(fn ->
# TODO: remove this once Mix requires Hex with the fix from
# https://github.com/hexpm/hex/pull/1015
# Context: Mix.install/1 starts :hex if necessary and stops
# it afterwards. Calling compile here may require hex to be
# started and that should happen automatically, but because
# of a bug it is not (fixed in the linked PR).
_ = Application.ensure_all_started(:hex)

do_recompile(options)
# Just as with Mix.install/2 we clear all task invocations,
# so that we can recompile the dependencies again next time
Expand Down
52 changes: 52 additions & 0 deletions lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,13 @@ defmodule Mix do
This function can only be called outside of a Mix project and only with the
same dependencies in the given VM.

The `MIX_INSTALL_RESTORE_PROJECT_DIR` environment variable may be specified.
It should point to a previous installation directory, which can be obtained
with `Mix.install_project_dir/0` (after calling `Mix.install/2`). Using a
restore dir may speed up the installation, since matching dependencies do
not need be refetched nor recompiled. This environment variable is ignored
if `:force` is enabled.

## Options

* `:force` - if `true`, runs with empty install cache. This is useful when you want
Expand Down Expand Up @@ -870,6 +877,13 @@ defmodule Mix do

try do
first_build? = not File.dir?(build_dir)

restore_dir = System.get_env("MIX_INSTALL_RESTORE_PROJECT_DIR")

if first_build? and restore_dir != nil and not force? do
File.cp_r(restore_dir, install_dir)
end

File.mkdir_p!(install_dir)

File.cd!(install_dir, fn ->
Expand Down Expand Up @@ -928,6 +942,10 @@ defmodule Mix do
end
end

if restore_dir do
remove_leftover_deps(install_dir)
end

Mix.State.put(:installed, {id, dynamic_config})
:ok
after
Expand Down Expand Up @@ -965,6 +983,28 @@ defmodule Mix do
Path.join(app_dir, relative_path)
end

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

deps = File.ls!(build_lib_dir)

loaded_deps =
for {app, _description, _version} <- Application.loaded_applications(),
into: MapSet.new(),
do: Atom.to_string(app)

# We want to keep :mix_install, but it has no application
loaded_deps = MapSet.put(loaded_deps, "mix_install")

for dep <- deps, not MapSet.member?(loaded_deps, dep) do
build_path = Path.join(build_lib_dir, dep)
File.rm_rf(build_path)
dep_path = Path.join(deps_dir, dep)
File.rm_rf(dep_path)
end
end

defp install_dir(cache_id) do
install_root =
System.get_env("MIX_INSTALL_DIR") ||
Expand Down Expand Up @@ -1013,6 +1053,18 @@ defmodule Mix do
end
end

@doc """
Returns the directory where the current `Mix.install/2` project
resides.
"""
@spec install_project_dir() :: Path.t()
def install_project_dir() do
case Mix.State.get(:installed) do
{id, _dynamic_config} -> install_dir(id)
nil -> nil
end
end

@doc """
Returns whether `Mix.install/2` was called in the current node.
"""
Expand Down
138 changes: 110 additions & 28 deletions lib/mix/test/mix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -263,29 +263,25 @@ defmodule MixTest do
[
{:git_repo, git: fixture_path("git_repo")}
],
lockfile: lockfile,
verbose: true
lockfile: lockfile
)

assert_received {:mix_shell, :info, ["* Getting git_repo " <> _]}
assert_received {:mix_shell, :info, ["Mix.install/2 using " <> install_dir]}

install_dir = Mix.install_project_dir()
assert File.read!(Path.join(install_dir, "mix.lock")) =~ rev
after
purge([GitRepo, GitRepo.MixProject])
end

test ":lockfile merging", %{tmp_dir: tmp_dir} do
[rev1, rev2 | _] = get_git_repo_revs("git_repo")

Mix.install(
[
{:git_repo, git: fixture_path("git_repo")}
],
verbose: true
)
Mix.install([
{:git_repo, git: fixture_path("git_repo")}
])

assert_received {:mix_shell, :info, ["* Getting git_repo " <> _]}
assert_received {:mix_shell, :info, ["Mix.install/2 using " <> install_dir]}

install_dir = Mix.install_project_dir()
assert File.read!(Path.join(install_dir, "mix.lock")) =~ rev1

Mix.Project.push(GitApp)
Expand All @@ -301,8 +297,6 @@ defmodule MixTest do
)

assert File.read!(Path.join(install_dir, "mix.lock")) =~ rev1
after
purge([GitRepo, GitRepo.MixProject])
end

test ":lockfile with application name", %{tmp_dir: tmp_dir} do
Expand All @@ -318,15 +312,12 @@ defmodule MixTest do
{:install_test, path: Path.join(tmp_dir, "install_test")},
{:git_repo, git: fixture_path("git_repo")}
],
lockfile: :install_test,
verbose: true
lockfile: :install_test
)

assert_received {:mix_shell, :info, ["* Getting git_repo " <> _]}
assert_received {:mix_shell, :info, ["Mix.install/2 using " <> install_dir]}
install_dir = Mix.install_project_dir()
assert File.read!(Path.join(install_dir, "mix.lock")) =~ rev
after
purge([GitRepo, GitRepo.MixProject])
end

test ":lockfile that does not exist" do
Expand All @@ -335,6 +326,73 @@ defmodule MixTest do
end
end

test "restore dir", %{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_dir = Mix.install_project_dir()
build_lib_path = Path.join([install_dir, "_build", "dev", "lib"])
deps_path = Path.join([install_dir, "deps"])

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

System.put_env("MIX_INSTALL_RESTORE_PROJECT_DIR", install_dir)
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_dir = Mix.install_project_dir()
build_lib_path = Path.join([install_dir, "_build", "dev", "lib"])
deps_path = Path.join([install_dir, "deps"])

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

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

System.put_env("MIX_INSTALL_RESTORE_PROJECT_DIR", install_dir)
end)

# Removing a dependency

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

refute_received _

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

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

test "installed?", %{tmp_dir: tmp_dir} do
refute Mix.installed?()

Expand Down Expand Up @@ -380,15 +438,7 @@ defmodule MixTest do

on_exit(fn ->
:code.set_path(path)
purge([InstallTest, InstallTest.MixProject, InstallTest.Protocol])

ExUnit.CaptureLog.capture_log(fn ->
Application.stop(:git_repo)
Application.unload(:git_repo)

Application.stop(:install_test)
Application.unload(:install_test)
end)
cleanup_deps()
end)

Mix.State.put(:installed, nil)
Expand Down Expand Up @@ -424,5 +474,37 @@ defmodule MixTest do

[tmp_dir: tmp_dir]
end

defp with_cleanup(fun) do
path = :code.get_path()

try do
fun.()
after
:code.set_path(path)
cleanup_deps()

Mix.State.clear_cache()
Mix.State.put(:installed, nil)
end
end

defp cleanup_deps() do
purge([
GitRepo,
GitRepo.MixProject,
InstallTest,
InstallTest.MixProject,
InstallTest.Protocol
])

ExUnit.CaptureLog.capture_log(fn ->
Application.stop(:git_repo)
Application.unload(:git_repo)

Application.stop(:install_test)
Application.unload(:install_test)
end)
end
end
end