vrod

vrod

Unsafe Application configuration call Application.fetch_env!/2

I have put some module attributes in my tests for easier reference, like this

@example Application.fetch_env!(:my_app, :thing)

but I notice I get errors when I run mix credo --strict:

Module attribute @example makes use of unsafe Application configuration call Application.fetch_env!/2

I am not understanding why this is unsafe?

Most Liked

Nicd

Nicd

The value of the @example attribute will be set at compile time and cannot be changed later. This is “unsafe” because it is easy to mistakenly think that it will follow the value configured at runtime, for example in config/runtime.exs, leading to hard to track errors.

If you intend to set such a value at compile time, use Application.compile_env/3 available since Elixir 1.10.

Nicd

Nicd

It’s possible but you would have to go out of your way to make it possible.

The meaning of “unsafe” here is different. The reason is that code like that can be confusing to a developer. They may expect that after they have built the application into a Mix release and transferred that to the server, that they can still change the value of the module attribute by changing the config, for example in config/runtime.exs. But because module attributes are handled at compile time, this is not possible. So they may waste a lot of time not knowing why the config is not being read, or even not notice the problem at all until it causes a bug later.

What compile_env does is store the value at compile time, but also check at startup if there is another value configured (like in config/runtime.exs). If so, it will raise an error, thus alerting the developers that they have misunderstood how the config works and that they need to correct the issue.

You can find more here: Elixir v1.10 released - The Elixir programming language

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

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 54260 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement