Proposal for System.get_env!

124 views
Skip to first unread message

Fl4m3Ph03n1x

unread,
Mar 1, 2019, 12:03:03 PM3/1/19
to elixir-lang-core
Background
I know this was proposed before and refuted: https://github.com/elixir-lang/elixir/issues/5608

However, I recently had an issue that would have benefited from System.get_env! and was advised to create a proposal in this group and reopen the discussion.

What was the issue?
Imagine you have a config.exs where you define some configurations. Imagine one of the configurations is the following:

config :my_app,
  geolix_db_path: System.get_env("PROJECTS_PATH") <> "/my_app/lib/geoip/GeoLite2-Country.mmdb"


Now this may look normal, but what if you don't have the "PROJETCS_PATH" variable defined?
What error do you think you would get?

We got this:

** (ArgumentError) argument error (stdlib) eval_bits.erl:101: :eval_bits.eval_exp_field1/6 (stdlib) eval_bits.erl:92: :eval_bits.eval_field/3 (stdlib) eval_bits.erl:68: :eval_bits.expr_grp/4 (stdlib) erl_eval.erl:484: :erl_eval.expr/5 (stdlib) erl_eval.erl:888: :erl_eval.expr_list/6 (stdlib) erl_eval.erl:240: :erl_eval.expr/5 (stdlib) erl_eval.erl:232: :erl_eval.expr/5

What now?
If you find this error message borderline useless and beyond cryptic, then you are with me.
The problem is that the stacktrace is obfuscated while the config file is being evaluated.

The WHY of the proposal
A way of avoiding this would be to have a System.get_env! that blows immediately making the error very clear.
I know I can use || raise "error" but I would like to avoid that every time I use System.get_env in a config file (or somewhere else for that matter).

Have you imagined what is like reading a config.ex file hundreds of lines long while using the || raise trick?
We already have trouble managing our huge config files, adding this would make it even worst.


Additional info

This discussion was generated from the forums. The original post with all the information can be seen here:

https://elixirforum.com/t/mix-exs-does-not-compile/20516

thia.md...@gmail.com

unread,
Mar 1, 2019, 12:38:36 PM3/1/19
to elixir-lang-core
I really would to see something like this on elixir. I suggested something really similar, the addition of an `fetch_env!` function to the System module. https://groups.google.com/forum/#!msg/elixir-lang-core/gijvoPm-a-0/Y2TzcHaiBgAJ

José Valim

unread,
Mar 1, 2019, 1:00:37 PM3/1/19
to elixir-l...@googlegroups.com
I agree. But it should be called System.fetch_env!/2 and we should have System.fetch_env/2 too. PR is welcome.


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/9b535dab-41b5-4caf-a7fc-b9e918aeffa0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrea Leopardi

unread,
Mar 1, 2019, 2:39:51 PM3/1/19
to elixir-lang-core
I'm working on it, PR will be up in a few minutes.

Andrea Leopardi


Fl4m3Ph03n1x

unread,
Mar 4, 2019, 3:35:26 AM3/4/19
to elixir-lang-core
Let us know if/when it gets accepted please !
Also, add a link, would love to see the work!

Fernando Tapia Rico

unread,
Mar 4, 2019, 3:59:43 AM3/4/19
to elixir-lang-core
Here is the link to the PR (already in master): https://github.com/elixir-lang/elixir/pull/8846

Spencer Carlson

unread,
Mar 16, 2019, 3:33:37 AM3/16/19
to elixir-lang-core
José, Andrea & Fernando, 

What are your thoughts on adding a default value via System.fetch_env/2? - https://github.com/elixir-lang/elixir/pull/8897
Reply all
Reply to author
Forward
0 new messages