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

Add an environment variable to optionally disable compilation locking #14129

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Member Author

Choose a reason for hiding this comment

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

My only concern is that the name sounds related to mix.lock, so alternative ideas are welcome. Though it's going to be rarely used, so maybe that's fine.

Copy link
Member

Choose a reason for hiding this comment

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

maybe MIX_DISABLE_CONCURRENCY_LOCKS? I am not sure I like _DISABLE_ in the name though. It could also be MIX_CONCURRENCY_LOCKS=0 or MIX_CONCURRENCY_LOCKS=false?

Copy link
Contributor

Choose a reason for hiding this comment

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

MIX_COMPILER_LOCK maybe? It’s not affecting concurrency “in general” just turning off the new compiler lock.

Copy link
Member

Choose a reason for hiding this comment

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

It applies to deps management as well. What about MIX_OS_CONCURRENCY_LOCK?

Copy link
Member Author

Choose a reason for hiding this comment

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

MIX_OS_CONCURRENCY_LOCK sounds good, I'd say in this case longer is actually better :D I'm generally not a fan _DISABLE_ either, but I saw that the other env vars (MIX_DEBUG, MIX_QUIET) are opt-ins. If you are fine with 0/false then definitely works for me!

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
Loading