Trying to remove the warning `module is not available or is yet to be defined`

Hi all,

I have a mix project A (otp app name :prj) that uses a path dependency B (otp app name :lib).

In A I have a config similar to:

config :lib, foo: A.Some.Module

Then in B I use it this way (simplified):

defmodule B.Helpers do
  def get_foo do
    Application.get_env(:lib, :foo, B.DefaultModule)
  end
end

defmodule B.One do
  @foo B.Helpers.get_foo()
  ...
end

defmodule B.Two do
  @foo B.Helpers.get_foo()
  ...
end

When I compile A, I got warnings in the form of:

... (module A.Some.Module is not available or is yet to be defined)

It makes sense, since when B is compiling, A isn’t compiled yet.

One solution I found is to not use a module attribute, so in B I would do something like

...
m = B.Helpers.get_foo()
m.something()
...

I was wondering if there’s any other approach to remove those warnings, because I’m using @foo in maaaany modules…

Thank you,
cheers!

I would go with ecto’s way and how it deals with a configuration:

defmodule MyApp.Repo do
  use Ecto.Repo, adapter: Ecto.Adapters.Postgres, otp_app: :my_app
end

So here we have a simple macro that takes otp_app i.e. the app that uses ecto library. With that ecto instead of trying to read :ecto config it looks for :my_app and current module (__MODULE__ special form), for example:

config :my_app, MyApp.Repo, database: "my_app_db_name"

Since above configuration is using MyApp.Repo in :my_app project there are no warnings emitted.