From 0d01579e6d723b51263db20b9725cab5c9715620 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Thu, 18 Jan 2024 09:35:46 +0900 Subject: [PATCH 1/2] Fix typo --- lib/mix/lib/mix/compilers/elixir.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/lib/mix/compilers/elixir.ex b/lib/mix/lib/mix/compilers/elixir.ex index eb9da80e21c..d788bb47bec 100644 --- a/lib/mix/lib/mix/compilers/elixir.ex +++ b/lib/mix/lib/mix/compilers/elixir.ex @@ -68,7 +68,7 @@ defmodule Mix.Compilers.Elixir do do: [Mix.Project | stale], else: stale - # If the lock has changed or a local dependency was added ore removed, + # If the lock has changed or a local dependency was added or removed, # we need to traverse lock/config files. deps_changed? = Mix.Utils.stale?([Mix.Project.config_mtime()], [modified]) or From 145cde15890b2229f3581e9c6481f28693f4dc9b Mon Sep 17 00:00:00 2001 From: sabiwara Date: Fri, 19 Jan 2024 09:05:57 +0900 Subject: [PATCH 2/2] Add MIX_COMPILE_CWD_CACHE environment variable --- lib/mix/lib/mix.ex | 6 ++++++ lib/mix/lib/mix/compilers/elixir.ex | 11 ++++++++++- lib/mix/lib/mix/tasks/compile.elixir.ex | 3 ++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/mix/lib/mix.ex b/lib/mix/lib/mix.ex index 3afe169049b..117656bd53b 100644 --- a/lib/mix/lib/mix.ex +++ b/lib/mix/lib/mix.ex @@ -337,6 +337,12 @@ defmodule Mix do written to. For example, "_build". If `MIX_BUILD_PATH` is set, this option is ignored. + * `MIX_COMPILE_CWD_CACHE` - Invalidates the compilation cache when the current + directory gets changed. This is the recommended behavior to avoid potential issues, + but can trigger undesired recompilations in some workflows where files are relocated + between compilation and mix task execution. + True by default, set to `0` or `false` to disable. + * `MIX_DEBUG` - outputs debug information about each task before running it * `MIX_DEPS_PATH` - sets the project `Mix.Project.deps_path/0` config for the diff --git a/lib/mix/lib/mix/compilers/elixir.ex b/lib/mix/lib/mix/compilers/elixir.ex index d788bb47bec..8c1d73b00f9 100644 --- a/lib/mix/lib/mix/compilers/elixir.ex +++ b/lib/mix/lib/mix/compilers/elixir.ex @@ -81,7 +81,8 @@ defmodule Mix.Compilers.Elixir do {force?, stale, new_deps_config} = cond do - !!opts[:force] or is_nil(old_deps_config) or old_cache_key != new_cache_key -> + !!opts[:force] or is_nil(old_deps_config) or + not cache_key_matches?(old_cache_key, new_cache_key) -> {true, stale, deps_config(local_deps)} deps_changed? or compile_env_apps != [] -> @@ -267,6 +268,14 @@ defmodule Mix.Compilers.Elixir do end end + defp cache_key_matches?(same, same), do: true + + defp cache_key_matches?({base, srcs, left_cwd, opt}, {base, srcs, right_cwd, opt}) + when is_nil(left_cwd) or is_nil(right_cwd), + do: true + + defp cache_key_matches?(_left_key, _right_key), do: false + @doc """ Removes compiled files for the given `manifest`. """ diff --git a/lib/mix/lib/mix/tasks/compile.elixir.ex b/lib/mix/lib/mix/tasks/compile.elixir.ex index eb814d30992..e1370ef98ad 100644 --- a/lib/mix/lib/mix/tasks/compile.elixir.ex +++ b/lib/mix/lib/mix/tasks/compile.elixir.ex @@ -115,7 +115,8 @@ defmodule Mix.Tasks.Compile.Elixir do manifest = manifest() base = xref_exclude_opts(project[:elixirc_options] || [], project) - cache_key = {base, srcs, File.cwd!(), "--no-optional-deps" in args} + maybe_cwd = if System.get_env("MIX_COMPILE_CWD_CACHE") not in ["0", "false"], do: File.cwd!() + cache_key = {base, srcs, maybe_cwd, "--no-optional-deps" in args} opts = base