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: https://elixir-lang.org/blog/2020/01/27/elixir-v1-10-0-released/

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

Other popular topics 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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement