jononomo
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jononomo
Also, I notice that
config/runtime.exsis apparently executed afterlib/myapp/application.exis executed when I start my app locally withmix phx.server. This is not what I was expecting.I think of
lib/myapp/application.exas the entry point to the actually running app, which should happen after everything is configured. Soruntime.exsshould be executed after compilation, but before the app actually starts, because I will need to load secret API keys throughruntime.exs, which my app will need when it starts. But if I’m already inlib/myapp/application.exbeforeruntime.exshas executed then isn’t that the wrong order?What am I not understanding? And how can I set configuration through
runtime.exsand have it actually override configuration set in the config files?Thanks.
stefanchrobot
What kind of settings are you seeing not being overriden via
runtime.exs? Are you by any chance trying to change a compile-time config param viaruntime.exs?jononomo
in config.exs I have the following lines:
in dev.exs I have the following lines:
in runtime.exs I have the following lines:
When I start the app, I see it print
‘in config.exs…’
‘in dev.exs…’
‘in runtime.exs…’
but the value of secret_api_key is “set-in-dev.exs”, at least as far as I can tell.
Also, if I add IO.inspect to lib/myapp/application.ex then it sometimes prints when I start the app up and it sometimes does not – I would like to be able to reliably IO.inspect my environment in application.ex and I don’t understand why that doesn’t work reliably.
I have the following lines at the bottom of lib/myapp/application.ex:
When I start the app for the first time after adding those lines it will print “in lib/myapp/application.ex” and then print the environment so I can inspect it – and it will do this BEFORE it prints “in runtime.exs”. But if I then stop and restart the app it will not print the lines from lib/myapp/application.ex.
jononomo
So yes, I think I am trying to change a compile-time config param via runtime.exs. Is this not possible? If not, then why not?
stefanchrobot
How are you reading the config value? If you’re reading it during compile-time, then it’s baked into the app and trying to modify at runtime will have no effect.
LostKobrakai
At the time
runtime.exsis read compilation is long done. So you cannot change compile time values throughruntime.exs. This is even more apparent when you build a release. Compilation happens when the release is created, but runtime.exs is only evaluated once the release is moved to wherever it’s supposed to run and started there.If you need things to be available at compile time use
config.exsand any additional files imported from there.jononomo
I understand that
runtime.exsis executed long after the compilation is finished. This is why I am so interested in setting my secret API key throughruntime.exsrather than throughconfig.exsor one ofdev/test/prod.exs.I would like for
runtime.exsto be evaluated only once the release is moved to wherever it is supposed to run and started there, and it is my understanding that that is what happens.On my local development environment I see that
runtime.exsis in fact evaluated afterconfig.exsanddev.exsare evaluated. Surely it is possible to useruntime.exsto set a secret API key, since that is the whole purpose of the file, as I understand it.Is it not possible to overwrite values in
runtime.exsthat have previously been set inconfig.exs?jononomo
I wonder if I don’t understand how to inspect my final runtime configuration properly. How can I inspect a value in my environment while the application is running, long after
runtime.exshas finished doing it’s thing?Nicd
The issue is here. These lines that are at the bottom of the file will be executed at compile time. Thus
runtime.exswill not be evaluated yet. Any values set inruntime.exsare 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.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.