Skip to content

Commit 668fb69

Browse files
committed
Add --no-check-pwd to mix compile
1 parent bb262d8 commit 668fb69

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

lib/mix/lib/mix/compilers/elixir.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
defmodule Mix.Compilers.Elixir do
66
@moduledoc false
77

8-
@manifest_vsn 28
8+
@manifest_vsn 29
99
@checkpoint_vsn 4
1010

1111
import Record
@@ -49,8 +49,8 @@ defmodule Mix.Compilers.Elixir do
4949
timestamp = System.os_time(:second)
5050
all_paths = Mix.Utils.extract_files(srcs, [:ex])
5151

52-
{all_modules, all_sources, all_local_exports, old_parents, old_cache_key, old_deps_config,
53-
old_project_mtime, old_config_mtime, old_protocols_and_impls} =
52+
{all_modules, all_sources, all_local_exports, old_parents, old_cache_key, old_cwd,
53+
old_deps_config, old_project_mtime, old_config_mtime, old_protocols_and_impls} =
5454
parse_manifest(manifest, dest)
5555

5656
# In case we aborted in the middle of a verification,
@@ -100,7 +100,8 @@ defmodule Mix.Compilers.Elixir do
100100

101101
{force?, stale, new_deps_config} =
102102
cond do
103-
!!opts[:force] or is_nil(old_deps_config) or old_cache_key != new_cache_key ->
103+
!!opts[:force] or is_nil(old_deps_config) or old_cache_key != new_cache_key or
104+
(Keyword.get(opts, :check_cwd, true) and old_cwd != File.cwd!()) ->
104105
{true, stale, deps_config(local_deps)}
105106

106107
deps_changed? or compile_env_apps != [] ->
@@ -313,7 +314,7 @@ defmodule Mix.Compilers.Elixir do
313314
rescue
314315
_ -> {[], []}
315316
else
316-
{@manifest_vsn, modules, sources, _, _, _, _, _, _, _} -> {modules, sources}
317+
{@manifest_vsn, modules, sources, _, _, _, _, _, _, _, _} -> {modules, sources}
317318
_ -> {[], []}
318319
end
319320
end
@@ -881,7 +882,7 @@ defmodule Mix.Compilers.Elixir do
881882

882883
## Manifest handling
883884

884-
@default_manifest {%{}, %{}, %{}, [], nil, nil, 0, 0, {%{}, %{}}}
885+
@default_manifest {%{}, %{}, %{}, [], nil, nil, nil, 0, 0, {%{}, %{}}}
885886

886887
# Similar to read_manifest, but for internal consumption and with data migration support.
887888
defp parse_manifest(manifest, compile_path) do
@@ -891,9 +892,9 @@ defmodule Mix.Compilers.Elixir do
891892
_ ->
892893
@default_manifest
893894
else
894-
{@manifest_vsn, modules, sources, local_exports, parent, cache_key, deps_config,
895+
{@manifest_vsn, modules, sources, local_exports, parent, cache_key, cwd, deps_config,
895896
project_mtime, config_mtime, protocols_and_impls} ->
896-
{modules, sources, local_exports, parent, cache_key, deps_config, project_mtime,
897+
{modules, sources, local_exports, parent, cache_key, cwd, deps_config, project_mtime,
897898
config_mtime, protocols_and_impls}
898899

899900
# {vsn, %{module => record}, sources, ...} v22-?
@@ -952,8 +953,8 @@ defmodule Mix.Compilers.Elixir do
952953
File.mkdir_p!(Path.dirname(manifest))
953954

954955
term =
955-
{@manifest_vsn, modules, sources, exports, parents, cache_key, deps_config, project_mtime,
956-
config_mtime, protocols_and_impls}
956+
{@manifest_vsn, modules, sources, exports, parents, cache_key, File.cwd!(), deps_config,
957+
project_mtime, config_mtime, protocols_and_impls}
957958

958959
manifest_data = :erlang.term_to_binary(term, [:compressed])
959960
File.write!(manifest, manifest_data)

lib/mix/lib/mix/tasks/compile.elixir.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ defmodule Mix.Tasks.Compile.Elixir do
7171
* `--long-verification-threshold N` - sets the "long verification" threshold
7272
(in seconds) to `N` (see the docs for `Kernel.ParallelCompiler.compile/2`)
7373
* `--no-verification` - disables code verification, such as unused functions,
74-
deprecation warnings, and type checking. It must be used alongside `MIX_DEBUG=1`
74+
deprecation warnings, and type checking. It must be used solely for debugging
75+
alongside `MIX_DEBUG=1`
76+
* `--no-check-cwd` - Elixir stores absolute paths in .beam files, which means rellocating
77+
the project root triggers a full build. Pass this option if you don't want the current
78+
working directory to be checked
7579
* `--purge-consolidation-path-if-stale PATH` - deletes and purges modules in the
7680
given protocol consolidation path if compilation is required
7781
* `--profile` - if set to `time`, outputs timing information of compilation steps
@@ -109,7 +113,8 @@ defmodule Mix.Tasks.Compile.Elixir do
109113
profile: :string,
110114
all_warnings: :boolean,
111115
verification: :boolean,
112-
tracer: :keep
116+
tracer: :keep,
117+
check_cwd: :boolean
113118
]
114119

115120
@impl true
@@ -127,7 +132,7 @@ defmodule Mix.Tasks.Compile.Elixir do
127132

128133
manifest = manifest()
129134
base = xref_exclude_opts(project[:elixirc_options] || [], project)
130-
cache_key = {base, srcs, File.cwd!(), "--no-optional-deps" in args}
135+
cache_key = {base, srcs, "--no-optional-deps" in args}
131136

132137
opts =
133138
base

lib/mix/test/mix/tasks/compile.elixir_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ defmodule Mix.Tasks.Compile.ElixirTest do
118118
end)
119119
end
120120

121+
test "does not recompiles project if cwd changes and --no-check-pwd is given" do
122+
in_fixture("no_mixfile", fn ->
123+
Mix.Project.push(MixTest.Case.Sample)
124+
assert Mix.Tasks.Compile.Elixir.run(["--verbose"]) == {:ok, []}
125+
assert_received {:mix_shell, :info, ["Compiled lib/a.ex"]}
126+
Mix.Project.pop()
127+
128+
old_cwd = File.cwd!()
129+
130+
in_tmp("new_cwd", fn ->
131+
Mix.Project.push(MixTest.Case.Sample)
132+
File.cp_r!(old_cwd, File.cwd!())
133+
134+
purge([A, B])
135+
Mix.Task.clear()
136+
assert {:noop, _} = Mix.Tasks.Compile.Elixir.run(["--verbose", "--no-check-cwd"])
137+
end)
138+
end)
139+
end
140+
121141
test "recompiles files using Mix.Project if mix.exs changes" do
122142
in_fixture("no_mixfile", fn ->
123143
Mix.Project.push(MixTest.Case.Sample, __ENV__.file)

0 commit comments

Comments
 (0)