Skip to content

Commit

Permalink
Add an environment variable to optionally disable compilation locking
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Dec 30, 2024
1 parent ed2ff6a commit c2e63e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ defmodule Mix do
* `MIX_INSTALL_DIR` *(since v1.12.0)* - specifies directory where `Mix.install/2` keeps
install cache
* `MIX_DISABLE_LOCK` - disables mix compilation locking. While not recommended, this may be
necessary in cases where hard links or TCP sockets are not available. When opting for this
behaviour, make sure to not start concurrent compilations of the same project.
* `MIX_PATH` - appends extra code paths
* `MIX_PROFILE` - a list of comma-separated Mix tasks to profile the time spent on
Expand Down
20 changes: 13 additions & 7 deletions lib/mix/lib/mix/sync/lock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ defmodule Mix.Sync.Lock do
This function can also be called if this process already has the
lock. In such case the function is executed immediately.
When the `MIX_DISABLE_LOCK` environment variable is set, the lock is
ignored and the function is executed immediately.
## Options
* `:on_taken` - a one-arity function called if the lock is held
Expand All @@ -96,9 +99,9 @@ defmodule Mix.Sync.Lock do
path = Path.join([System.tmp_dir!(), "mix_lock", hash])

pdict_key = {__MODULE__, path}
has_lock? = Process.get(pdict_key)
has_lock? = Process.get(pdict_key, false)

if has_lock? do
if has_lock? or lock_disabled?() do
fun.()
else
lock = lock(path, opts[:on_taken])
Expand All @@ -115,6 +118,8 @@ defmodule Mix.Sync.Lock do
end
end

defp lock_disabled?(), do: System.get_env("MIX_DISABLE_LOCK") in ~w(1 true)

defp lock(path, on_taken) do
File.mkdir_p!(path)

Expand Down Expand Up @@ -198,11 +203,12 @@ defmodule Mix.Sync.Lock do
:invalidated

{:error, reason} ->
raise File.LinkError,
reason: reason,
action: "create hard link",
existing: port_path,
new: lock_path
Mix.raise("""
could not create hard link from #{port_path} to "#{lock_path}: #{:file.format_error(reason)}.
Hard link support is required for Mix compilation locking. If your system \
does not support hard links, set MIX_DISABLE_LOCK=1\
""")
end
end

Expand Down

0 comments on commit c2e63e9

Please sign in to comment.