envy icon indicating copy to clipboard operation
envy copied to clipboard

Preserve existing ENV variables if they exist

Open jakesgordon opened this issue 4 years ago • 0 comments

Overview

This PR ensures that existing environment variables take priority over those inside the .env file, this allows us to override the .env file values with command line values, e.g.

FOO=bar mix phx.server

... and the command line FOO=bar will take priority over any instance of FOO in the .env file

Priority

Note that because of this change in behavior I also needed to swap around the priority of the .env.#{current_env} file in order to ensure that version continues to take priority over the more generic .env variation (to maintain the existing behavior). The new order loads the env specific version first and now if a variable is set by that variant it takes priority over the generic .env. E.g the priority order is now:

  1. any existing environment variables
  2. any environment variables specified in the .env.#{current_env} variant (if it exists)
  3. any environment variables specified in the .env variant.

Tests

I added a new unit test to confirm this behavior. All existing tests still pass.

jakesgordon avatar Apr 02 '22 22:04 jakesgordon