import_config should not fail if "#{Mix.env()}".exs is not available

51 views
Skip to first unread message

boris kotov

unread,
Dec 17, 2018, 5:49:39 AM12/17/18
to elixir-lang-core
Hi all,

when dealing with umbrella applications, you may need for some of the apps special env, for example a "e2e_test.exs" which is relevant for the web app, but not for all apps within the umbrella.
In my case, I've an subapp, which has a special configuration for the "test" env. But because I had to uncomment import_config "#{Mix.env()}.exs" line, I also have to provide all the other possible config files for all possible env within my umbrella. I ended up by providing empty config files, to make it not fail.

Proposal:

import_config should not fail if config cannot be discovered, or provide an optional-flag, like `import_config("#{Mix.env()}.exs", optional: true)` so the config directory will stay clean only with configs you need for that particular application.

Thanks!

boris kotov

unread,
Dec 17, 2018, 6:02:18 AM12/17/18
to elixir-lang-core
Found a better approach to creating empty config files:

if Enum.member?([:test], Mix.env()) do
  import_config
"#{Mix.env()}.exs"
end


Event this is very simple to implement, we could generalize it in the Mix project.

import_env_config [:test, :dev]

What do you think?

w.m.w...@student.rug.nl

unread,
Dec 17, 2018, 11:41:19 AM12/17/18
to elixir-lang-core
Very closely related to this, I've found myself multiple times lately wanting to write code that differed between development/test and production; mostly to introduce some function-call checks in the development-like environments that would not be there in production to improve speed.

This is currently impossible to do without either:
- Depending on `Mix.env` existing, which breaks the library for anyone who wants to build a release.
- Requiring manual configuration by the user of the library for each of the environments.

I feel like cases like this are common enough to warrant the standardization of a way to check the current execution environment.

Andrew Dryga

unread,
Dec 19, 2018, 5:13:37 AM12/19/18
to elixir-lang-core
You can use `Mix.env()` to do that today, if you want code to behave differently depending on the environment use it to conditionally define functions:

if Mix.env() == :test do
 
def foo, do: IO.puts "Hello test"
else
  def foo, do: IO.puts "Hello world!"
end
Reply all
Reply to author
Forward
0 new messages