What to use Application.compile_env! orApplication.fetch_env? or get_env ? what when and where

The error

[39mERROR! the application :core has a different value set for path [:chaser_after] inside key Core.Emails.Chaser during runtime compared to compile time. Since this application environment entry was marked as compile time, this difference can lead to different behaviour than expected:
* Compile time value was set to: 3
* Runtime value was set to: 5

The code

  @chaser_after Application.compile_env!(:core, [__MODULE__, :chaser_after])
  @time_unit Application.compile_env!(:core, [__MODULE__, :time_unit])

  def chaser_after do
    DateTime.utc_now()
    |> DateTime.add(@chaser_after, @time_unit)
  end

config

//dev
config :core, Core.Emails.Chaser,
  chaser_after: 3,
  time_unit: :minute

//prod
config :core, Core.Emails.Chaser,
  chaser_after: 3,
  time_unit: :day
import Config

//stagingg
# remaining config same as prod, as even in staging to. since controlled by env var
import_config "prod.exs"

# staging config overrides prod ones
config :core, Core.Emails.Chaser,
  chaser_after: 5,
  time_unit: :minute

//test
config core, Core.Emails.Chaser,
  chaser_after: 0,
  time_unit: :nanosecond

For different environement , re compilation happens , why then its being logged as error.

The what to use:

  • compile_env! or wihtout !
  • fetch
  • get_env
1 Like

It’s not visible from your post where is a different API used, nor where was the error thrown exactly?

1 Like

during ci cd after mix phx.server

1 Like

Good question. Faced same issues and the solution was simply not to use compile_env.

I recall facing the issue when doing releases.

1 Like

but what we should use and when we should use.
I went with compiled env since it will be constant for that deployment, like production ,staging.

compile_env/3
compile_env/4
compile_env!/2
compile_env!/3
fetch_env/2
fetch_env!/2
get_env/3