fix(build): inline dep-patch logic into mix.exs so it runs during deps.get

Previously the `mix deps.patch` task lived at `lib/mix/tasks/deps.patch.ex`
and the `deps.get` alias invoked it. This broke `mix deps.get --only prod`
in Docker: `lib/` isn't compiled yet when deps are fetched, so the task
module couldn't be resolved and the build failed with
`The task "deps.patch" could not be found`.

Move the patch-application function directly into `mix.exs` as a private
helper and wire it into the alias via `&apply_dep_patches/1`. `mix.exs` is
always loadable, so the function is available the moment `deps.get` runs.
This commit is contained in:
Graham McIntire 2026-04-21 14:06:24 -05:00
parent 32ba5de456
commit 6c6e413105
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
2 changed files with 53 additions and 79 deletions

View file

@ -1,77 +0,0 @@
defmodule Mix.Tasks.Deps.Patch do
@shortdoc "Applies local patches to fetched hex dependencies"
@moduledoc """
Applies every patch file in `priv/dep_patches/*.patch` to its
corresponding dependency under `deps/`. Each patch name follows the
convention `<dep_name>-<version>.patch` (e.g. `live_table-0.4.1.patch`).
Applied idempotently if the patched text is already present we skip
the dep. Run after `mix deps.get` / `mix deps.compile`. Safe to invoke
repeatedly.
Why this exists: `live_table` 0.4.1 has a call to
`LiveTable.LiveSelectHelpers.restore_live_select_from_params/2` whose
return value is discarded inside the macro-expanded `handle_params/3`,
tripping the strict dialyzer `:unmatched_return` flag at every `use`
site. The patch adds an `_ =` binding. Upstream PR TODO once merged
and released, drop both the patch and this task.
"""
use Mix.Task
@patches_dir "priv/dep_patches"
@impl Mix.Task
def run(_args) do
case File.ls(@patches_dir) do
{:ok, entries} ->
patches = entries |> Enum.filter(&String.ends_with?(&1, ".patch")) |> Enum.sort()
Enum.each(patches, &apply_patch/1)
{:error, :enoent} ->
Mix.shell().info("deps.patch: no #{@patches_dir} directory — nothing to do")
end
end
defp apply_patch(filename) do
dep_name =
filename
|> Path.basename(".patch")
|> String.split("-")
|> Enum.drop(-1)
|> Enum.join("-")
dep_dir = Path.join("deps", dep_name)
cond do
not File.dir?(dep_dir) ->
Mix.shell().info("deps.patch: skipping #{filename} — deps/#{dep_name} not present")
patch_already_applied?(filename, dep_dir) ->
Mix.shell().info("deps.patch: #{filename} already applied")
true ->
patch_path = Path.join(@patches_dir, filename)
{output, exit_code} = System.cmd("patch", ["-p1", "-i", Path.absname(patch_path)], cd: dep_dir)
if exit_code == 0 do
Mix.shell().info("deps.patch: applied #{filename}")
touch_sentinel(filename, dep_dir)
else
Mix.raise("deps.patch: failed to apply #{filename}:\n#{output}")
end
end
end
# Record that the patch landed by writing a sentinel file inside the
# dep dir. `File.touch!/1` avoids spurious re-applications when the
# patch contains text the dep happens to contain already.
defp touch_sentinel(filename, dep_dir) do
File.touch!(Path.join(dep_dir, ".#{filename}.applied"))
end
defp patch_already_applied?(filename, dep_dir) do
File.exists?(Path.join(dep_dir, ".#{filename}.applied"))
end
end

55
mix.exs
View file

@ -123,8 +123,8 @@ defmodule Microwaveprop.MixProject do
# See the documentation for `Mix` for more info on aliases. # See the documentation for `Mix` for more info on aliases.
defp aliases do defp aliases do
[ [
setup: ["deps.get", "deps.patch", "ecto.setup", "assets.setup", "assets.build"], setup: ["deps.get", "ecto.setup", "assets.setup", "assets.build"],
"deps.get": ["deps.get", "deps.patch"], "deps.get": ["deps.get", &apply_dep_patches/1],
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"], "ecto.reset": ["ecto.drop", "ecto.setup"],
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
@ -138,4 +138,55 @@ defmodule Microwaveprop.MixProject do
precommit: ["compile --warnings-as-errors", "deps.unlock --unused", "format", "test"] precommit: ["compile --warnings-as-errors", "deps.unlock --unused", "format", "test"]
] ]
end end
# Applies every patch file in `priv/dep_patches/*.patch` to its corresponding
# dependency under `deps/`. Inlined here (instead of a `Mix.Task` module
# under `lib/mix/tasks/`) so it can run during `mix deps.get` itself —
# `lib/` isn't compiled yet at that point, so a proper task would not be
# resolvable. Idempotent: a sentinel file per patch prevents re-application.
@patches_dir "priv/dep_patches"
defp apply_dep_patches(_args) do
case File.ls(@patches_dir) do
{:ok, entries} ->
entries
|> Enum.filter(&String.ends_with?(&1, ".patch"))
|> Enum.sort()
|> Enum.each(&apply_single_patch/1)
{:error, :enoent} ->
:ok
end
end
defp apply_single_patch(filename) do
dep_name =
filename
|> Path.basename(".patch")
|> String.split("-")
|> Enum.drop(-1)
|> Enum.join("-")
dep_dir = Path.join("deps", dep_name)
sentinel = Path.join(dep_dir, ".#{filename}.applied")
cond do
not File.dir?(dep_dir) ->
Mix.shell().info("deps.patch: skipping #{filename} — deps/#{dep_name} not present")
File.exists?(sentinel) ->
Mix.shell().info("deps.patch: #{filename} already applied")
true ->
patch_path = Path.absname(Path.join(@patches_dir, filename))
{output, exit_code} = System.cmd("patch", ["-p1", "-i", patch_path], cd: dep_dir)
if exit_code == 0 do
Mix.shell().info("deps.patch: applied #{filename}")
File.touch!(sentinel)
else
Mix.raise("deps.patch: failed to apply #{filename}:\n#{output}")
end
end
end
end end