Config/runtime.exs not loaded on mix tasks

I had a config/runtime.exs.

import Config

config :test,
  port: 1333

And the application.

def start(_type, _args) do
    Supervisor.start_link([], strategy: :one_for_one)
end

Mix task.

defmodule Mix.Tasks.Example do
  use Mix.Task

  def run(_args) do
    IO.inspect(Application.get_env(:test, :port))
  end
end

But when I hit my mix tasks mix something, it returns nil.
The interesting thing is if I run it on interactive mode (iex -S mix), the application env can retrieval normally.
What I could miss here, in order to get runtime config on mix task?

I was able to achieve that by running mix app.config first.

mix app.config

Loads and configures all registered apps.
This is done by loading config/runtime.exs if one exists.

So to see it works you could try running them like:

mix do app.config, something

And to make your task running it automatically - you could call it as a first thing to do in run/1 function

def run(_args) do
  Mix.Task.run("app.config")
  IO.inspect(Application.get_env(:test, :port))
  ...
2 Likes