Feature Request: Conditional execution of `mix deps.get`
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
AI Policy
- [x] I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.
Is your feature request related to a problem? Please describe.
I call Igniter from custom Elixir scripts. Igniter gives me long unwanted text output, resulting from running mix deps.get - useless noise that obscures my own script output. Igniter always calls mix deps.get whenever any task is queued, even if dependencies didn't change.
I asked Claude to investigate and here's what it told me...
Problem
When Igniter applies changes, it runs mix deps.get if any task is queued,
whether or not the run changed mix.exs. From Igniter.do_or_dry_run/2
(lib/igniter.ex, v0.8.4):
{:ok, _result} ->
if !Enum.empty?(igniter.tasks) do
case Enum.at(igniter.tasks, 0) do
{"deps.get", []} ->
:ok
_ ->
Mix.shell().cmd("mix deps.get")
end
end
A task that queues a follow-up step that has nothing to do with
dependencies (a git commit, a formatter, a code generator) still triggers a
full Hex resolution every time it runs. Because the call goes through
Mix.shell().cmd/1, Hex's output goes straight to the user's terminal,
including the list of every unchanged package:
Resolving Hex dependencies...
Resolution completed in 0.222s
Unchanged:
abnf_parsec 2.1.0
bandit 1.13.1
castore 1.0.22
... (about 70 more lines)
This has three costs:
- It does nothing. If
mix.exshasn't changed, the fetch can't change anything. - It's slower and needs the network. Hex resolution can reach the network, which adds time and can fail when the user is offline.
- It buries the output that matters. Dozens of lines of resolver output push the task's own messages off the screen.
It also happens when the task already fetched dependencies with
Igniter.apply_and_fetch_dependencies/2 earlier in the run. Then the
implicit deps.get is a second, redundant fetch.
There's no clean way around it from the task's side. Queuing deps.get
first only skips the implicit call; the queued deps.get still runs as a
normal task, with the same output.
Reproduction
An Igniter task that changes no files and queues one unrelated task:
defmodule Mix.Tasks.Demo.Noop do
use Igniter.Mix.Task
@impl Igniter.Mix.Task
def igniter(igniter) do
Igniter.add_task(igniter, "help", ["compile"])
end
end
$ mix demo.noop --yes
Resolving Hex dependencies...
Resolution completed in 0.2s
Unchanged:
...every locked package...
Expected: mix help compile runs, with no dependency resolution.
Describe the solution you'd like
Run the implicit deps.get only when the pending changes touch mix.exs.
Igniter already has the check it needs, Igniter.has_changes?/2, which
apply_and_fetch_dependencies/2 uses for exactly this purpose.
The check has to be made before Rewrite.write_all/1, while the sources
still count as changed:
fetch_deps? =
!Enum.empty?(igniter.tasks) &&
Enum.at(igniter.tasks, 0) != {"deps.get", []} &&
has_changes?(igniter, ["mix.exs"])
igniter.rewrite
|> Rewrite.write_all()
|> case do
{:ok, _result} ->
if fetch_deps?, do: Mix.shell().cmd("mix deps.get")
# ...rest unchanged
Why this is safe:
- The usual case still works. A run that adds a dependency and queues
its installer changes
mix.exs, so the fetch still runs before the queued tasks. - Explicit
deps.getstill works. A task that needs a fetch whatever happened tomix.exscan queuedeps.getitself, asigniter.adddoes today. That path doesn't change. - Earlier fetches aren't repeated. If
apply_and_fetch_dependencies/2already wrote and fetchedmix.exs, nothing is left pending for it, so there's no second fetch.
If you'd like a way back to the old behavior, an option such as
fetch_deps_before_tasks?: true could force the fetch. I'd lean toward
leaving it out unless there's a real use case for fetching when mix.exs
hasn't changed.
Describe alternatives you've considered
I could maybe jigger up my script to trap the mix.exs output, maybe. There probably other super-hackey solutions I could McGyver. But I think the issue is best addressed in Igniter itself. An Igniter solution would solve my problem and would benefit every Igniter user.
Additional context
I can submit a PR with this change, plus tests for both cases: no fetch when
only an unrelated task is queued, and a fetch when mix.exs changes and a task
is queued. Also available to test if you prefer a different approach. LMK how
you'd like to proceed.
Followup: I've made the change in a fork and am using it locally. See https://github.com/andyl/igniter/tree/conditional_deps_get
PR welcome!