In my mix.exs
file, if I try to create an alias with a hex mix task, for example hex.audit
or hex.outdated
, I get an error saying the task could not be found. I know I have hex installed because the above mix tasks work on the command line, so I’m guessing it has something to do with those mix tasks not being sourced when run as an alias, but I’m unsure how I can fix it. Here’s a larger example:
defp aliases do
[
audit: ["hex.audit"]
]
end
Is the aliases
function actually called inside mix.exs
?
I think so? All my other non-hex mix tasks work just fine there as aliases.
@dimitarvp I think I may have misunderstood your question. Yes, I’m calling it inside mix.exs
. I initially thought you were asking if that’s what I should be doing. Like I said above, non-hex aliases are working. So for instance this works:
defp aliases do
[
setup: ["deps.get", "deps.compile", "ecto.reset", "assets.setup", "assets.build"]
]
end
A-ha, I see. Was just making sure. A drive-by contribution if you will. 
Is there anyone else that can help with this? I considered using elixirc_paths, but I don’t know what path I would give it, since the hex files are already compiled.
I just tried your initial example on a project and it seemed to work just fine.
Okay, so apparently my example wasn’t specific enough. 
Upon further examination, the hex mix task has to be at least the third in line in the list. The list can have more than three items in it as long as the hex mix task is number one or two. So for example this works:
defp aliases do
[
testtask: ["deps.get", "hex.audit", "deps.compile"]
]
end
But this does not:
defp aliases do
[
testtask: ["deps.get", "deps.compile", "hex.audit"]
]
end
It results in:
** (Mix) The task "hex.audit" could not be found. Did you mean "deps.audit"?
1 Like
I don’t have a solution, but I was able to reproduce it and note a few things:
- If
hex.audit
is the first task in the alias, it works
- If you only alias hex tasks (for example
["hex.info", "hex.audit"]
) - it also works
So it would seem that deps
tasks somehow disable them 
It’s actually not specific to deps
; any non-hex tasks (as you discovered) will cause a failure. For example:
defp aliases do
[
testtask3: ["format", "compile", "hex.audit"],
]
end
1 Like
So I traced it down to the fact that after running any non-hex task Code.ensure_loaded(Mix.Tasks.Hex.Audit)
returns {:error, :nofile}
. But if no other non-hex task was run before, it returns {:module, Mix.Tasks.Hex.Info}
. Unfortunately I don’t know why is that, but maybe someone more knowledgeable would have some idea.
3 Likes
This feels to me like mix is hitting an edge case resolving both modules available within your project and dependencies, and also modules installed by archives (like the hex tasks). I’d consider opening up an issue in Elixir with these reproduction instructions, reporting that mix
and :aliases
may not be working as intended with archives.
You can do this:
def aliases do
[
testtask: [
"deps.get",
"deps.compile",
# Compilation prunes code paths, so Hex task modules are no
# longer available. We need to bring the application back
# explicitly, in order to access its modules.
fn _ -> Mix.ensure_application!(:hex) end,
"hex.audit"
]
]
end
5 Likes