Search code examples
elixirelixir-mix

Can't access a dependency config in main elixir project


I have a project world_app which I have included in hello_app as a dependency (I have included it as a local dependency if that's relevant)

defp deps do
  [
    {:world_app, path: "../world_app"}
  ]
end

The world_app has a config.exs that has this configuration

config :world_app, some_config: "config_string"

My problem arises when I try to get the config variable defined in world_app in hello_app (I ran iex -S mix in hello_app)

iex(1)> Application.get_all_env(:world_app)
[included_applications: []]

iex(2)> Application.get_env(:world_app, :some_config)
nil

However, when I do the same thing in world_app I can see the variables

iex(1)> Application.get_all_env(:world_app)
[some_config: "config_string", included_applications: []]

iex(2)> Application.get_env(:world_app, :some_config)
"config_string"

I've always been under the impression that I could access a dependency's config from the parent application; am I missing something crucial here?

I'm using Elixir 1.5.3 and erlang 20


Solution

  • Configurations of dependencies are not automatically imported. In umbrella projects, all children see each other's configuration because the root config contains this magical line:

    import_config "../apps/*/config/config.exs"
    

    which imports all configuration files of all its children, and conversely, all its children are pointing to the root config file in mix.exs:

    defmodule ChildProject.MixProject do
      use Mix.Project
    
      def project do
        [
          (...)
          config_path: "../../config/config.exs",
          (...)
        ]
      end
    
      (...)
    
    end
    

    This is somewhat explained in a chapter of the Mix & OTP Getting Started Guide.


    You can use the same trick to explicitly import the dependency's configuration by adding this line to hello_app/config/config.exs:

    import_config "../../world_app/config/config.exs"