Mix deps.get and Mix.Task.run "deps.get" behaving differently

I want to simplify the installation process for my application into a single command line:

mix my_app.install

I’d like this installation process to get and compile all dependencies and any assets that are not tracked in git.

However, when I clone my application from git to a new repository and run the custom mix task, I get following message from Mix.Tasks.Deps.Loadpaths:

Unchecked dependencies for environment dev:
* ex_doc (Hex package)
  the dependency is not available, run "mix deps.get"
...

This is strange to me because deps.get is the first function I call in my custom task via Mix.Task.run/2. When I run mix deps.get --no-archive-check from the CLI I don’t have any issues.

Anyone know why this is?

My custom mix task:

defmodule Mix.Tasks.MyApp.Install do

  use Mix.Task

  def run(_) do
    get_deps()
    compile_deps()
    install_brunch()
    setup_database()
  end

  defp get_deps do
    Mix.Task.run "deps.get", ["--no-archives-check"]
  end

  defp compile_deps do
    Mix.Task.run "deps.compile"
  end

  defp install_brunch do
    args =
      for app <- Mix.Project.config[:web_apps] do
        "--app #{app}"
      end
      |> Kernel.++(["npm install"])

    Mix.shell.cmd args
  end

  defp setup_database do
    Mix.Task.run "ecto.setup"
  end

end
1 Like

You can’t…

Mix requires that all dependencies are available when you run something that is not deps.get, because it assumes, that even your custom mix task requires your dependencies installed.

I’m not sure about task-aliases though if they do have the same requirement. Try the following in your mix.exs:

def project do
  […
   aliases: aliases]
end

defp aliases do
  [install: ["deps.get", "deps.compile", &install_brunch/1, "ecto.setup"]]
end

def install_brunch do
  …
end
2 Likes

That works! Thank you!

1 Like