Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Lack of .env files in config/** reverts any .env.#{environment} #13

Closed
dlindahl opened this issue Jul 2, 2014 · 12 comments · Fixed by #25 or #26
Closed

Lack of .env files in config/** reverts any .env.#{environment} #13

dlindahl opened this issue Jul 2, 2014 · 12 comments · Fixed by #25 or #26

Comments

@dlindahl
Copy link

dlindahl commented Jul 2, 2014

In a Rails app, if there are no .env files in config/**/, dotenv adds (and parses) the root .env file, reverting any overrides in .env.#{environment}

For example:

# in /.env
FOO=root

# in /.env.development
FOO=dev
Dotenv.load '.env'
pp ENV['FOO']
# > "root"

Dotenv.overload ".env.#{environment}"
pp ENV['FOO']
# > "dev"

Dotenv.overload *Dir.glob("#{Rails.root}/config/**/*.env.#{environment}")
pp ENV['FOO']
# > "root"

The glob call returns an empty array which is then backfilled with .env in Dotenv#with. This means that the non-environment specific file is not only parsed twice, it reverts anything that is overridden in .env.development

@reichertm
Copy link

+1

I can see the same behaviour. Settings defined in /.env file are overriding whatever is set in the /.env.test when running tests.

@LukeWinikates
Copy link

+1

We found the same issue. We noticed that Rails is defined, but Rails.root is empty. This means that the pattern passed to Dir.glob is /config/**/*env.development where the environment is development. That glob doesn't match any file, so we see the backfilling behavior and the .env file overrides any environment specific configuration file.

Our planned workaround is to drop dotenv-deployment for now. Instead we'll use the dotenv-rails gem and call Dotenv.overload directly with our configuration file.

@surfacedamage
Copy link

Found this same issue. I am working on a rails project where we have both dotenv and dotenv-deployment in place. We have a .env file in project root and a .env.development also in root.

When the initializer for dotenv-depoloyment runs, the .env will be properly loaded, then the first overload below runs and is properly overriden by .env.development. All is well up to this point. But then the second overload below will execute and since I have no files matching that glob, I lose anything that was overridden by .env.development and it gets reset back to .env.

# from: dotenv-deployment-0.0.2/lib/dotenv/deployment/deployment.rb
# lines 9-13

# Override any existing variables if an environment-specific file exists
if environment = ENV['RACK_ENV'] || (defined?(Rails) && Rails.env)
  Dotenv.overload ".env.#{environment}"
  Dotenv.overload *Dir.glob("#{Rails.root}/config/**/*.env.#{environment}") if defined?(Rails)
end

This happens because the overload method in dotenv.rb uses the protected with method which automatically pushes the .env back into play if no files are found in the glob.

# from: dotenv-0.11.1/lib/dotenv.rb
# lines 21-29

  def self.with(*filenames, &block)
    filenames << '.env' if filenames.empty?

    filenames.inject({}) do |hash, filename|
      filename = File.expand_path filename
      hash.merge(block.call(filename) || {})
    end
  end

Although it makes sense that .env is included on load(), it seems a bit non-intuitive to me that overload() would include the .env file if the glob is not matched. The fact that it is even called "overload" seems to indicate that at some point "load" has already happened (which means .env should have been included).

@bkeepers Would you support a pull request that would not include .env on calls to overload? I'd be happy to put something together.

@bkeepers
Copy link
Owner

👍

1 similar comment
@gretel
Copy link

gretel commented Sep 14, 2014

👍

@surfacedamage
Copy link

Pull request opened bkeepers/dotenv#133

@fpellanda
Copy link

👍

@fuJiin
Copy link

fuJiin commented Oct 9, 2014

+1 👍

@gmontard
Copy link

+1

bogdanovich pushed a commit to bogdanovich/dotenv-deployment that referenced this issue Nov 7, 2014
bogdanovich pushed a commit to bogdanovich/dotenv-deployment that referenced this issue Nov 7, 2014
@glaszig
Copy link

glaszig commented Dec 13, 2014

@surfacedamage's analysis is correct. same issue over here. using manual Dotenv.load '.env.test' in my spec_helper.rb for now.

@PikachuEXE
Copy link

Still not fixed?

@rypit
Copy link

rypit commented Jan 26, 2015

This doesn't occur in versions of dotenv before this gem was created, so you may be able to use something like dotenv 0.9.0. Which seems to load correctly with various RAILS_ENV settings, if that's all you're looking for.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.