Search code examples
elixirelixir-mix

Elixir: Mix aliases, two `run` tasks in one alias


When setting aliases like:

defp aliases do
    [
      test_run: ["run -e 'IO.puts(\"One\")'", "run -e 'IO.puts(\"Two\")'"]
    ]
end

The result of mix test_run should be

$ mix test_run
One
Two

But it only executes the first task and prints

$ mix test_run
One

Not sure if it's intended behaviour, but before putting an issue I wanted to make sure. Here is the repository to reproduce the error: https://github.com/wende/mix_run_twice


Solution

  • Mix does not allow a task to be run twice. You can however, use Mix.Task.reenable/1 to run it again.

      test_run: ["run -e 'IO.puts(\"One\"); Mix.Task.reenable(:run)'", "run -e 'IO.puts(\"Two\")'"]
    

    You must reenable the task at the end of the first run otherwise it will never get to the second task. You can't do something like:

     ["run -e 'IO.puts(\"One\")'", "run -e 'Mix.Task.reenable(:run)'"]
    

    I would suggest making a custom mix task which calls Mix.Task.run/2 for each task you want to run, reenabling as you go. Elixir 1.3 will make this easier by providing a rerun/2 function that does the reenable and run for a task. https://github.com/elixir-lang/elixir/pull/4394