jononomo

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

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

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

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

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.

emoragaf

emoragaf

I think the easiest way that you can check your config here, would be to start the app like iex -S mix and call Application.fetch_env(:myapp, :jon) inside iex

stefanchrobot

stefanchrobot

So basically try this:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    IO.inspect(Application.get_all_env(:myapp))
    # ...
  end
end

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement