jononomo
Why can't I do configuration in runtime.exs in my dev environment?
So I’m trying to understand how config/runtime.exs works. If I put:
IO.inspect("in runtime.exs")
at the top of the file, then when I start my application on my local machine with mix phx.server I can see it print out “in runtime.exs” in my terminal, and I can add other print statements to confirm that runtime.exs is being executed after dev.exs, which is being executed after config.exs.
But if I set some configuration in runtime.exs then it does not override the configuration set in config.exs, and of course it also does not override the configuation set in dev.exs. However, if I put some configuration in dev.exs then it does override the configuration set in config.exs.
My understanding is that runtime.exs is the last file executed of these three, and the first file executed when the compiled application starts up, so I would think that configuration set in runtime.exs would override configuration set in dev.exs. But this is not happening.
Does anyone have any idea what is going on here?
I’m using Phoenix 1.6-rc.0, by the way.
Marked As Solved
Nicd
This is an implementation detail: the config ... line actually does not set the application env at that point, it puts values to the process dictionary with Process.put. The application env is created later when the system starts proper. Thus if you want to share some common values inside runtime.exs (or other config files), you must put them in regular variables.
Try inspecting the values inside your application.ex start-callback. That function is executed at runtime and you should see the correct values there (and any other function executed at runtime).
Also Liked
John-Goff
The releases.exs file was released in elixir 1.9, this was the first attempt at runtime config but it only applied to releases and not in dev. Elixir 1.11 introduced runtime.exs which runs in both dev and release mode.
LostKobrakai
To clarify this further. In elixir any code, which is not within a function definition (or moved to such via a macro) will be executed at compile time.
Nicd
The issue is here. These lines that are at the bottom of the file will be executed at compile time. Thus runtime.exs will not be evaluated yet. Any values set in runtime.exs are only available at runtime.
I wrote this blog post that attempts to clarify the differences between the configurations (in the article I call “compile time” “build time” but it means the same thing): Elixir: Time for Some Configuration — Random Notes
To simplify, if you want to see your values from runtime.exs, you need to grab them at runtime, i.e. inside a function that is called at runtime.
Last Post!
John-Goff
The releases.exs file was released in elixir 1.9, this was the first attempt at runtime config but it only applied to releases and not in dev. Elixir 1.11 introduced runtime.exs which runs in both dev and release mode.
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









