dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

Environment variables aren't loading in Rails 4.2

Open dcunited001 opened this issue 12 years ago • 22 comments

I tried adding dotenv to my Gemfile with Rails 4.2 and the vars in .env weren't being loaded on initialization, either during or after the AppName::Application module block.

Also, when I tried adding the dotenv-rails gem, I was running into a nil error because Rails.root isn't defined until the AppName::Application block starts. So i was getting a nil error in Dotenv::Railtie.load on line 17.

dcunited001 avatar Oct 16 '14 06:10 dcunited001

Same issue here. I'm not sure what the previous behaviour was but the call to Rails.root.join('.env') on this file https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/rails.rb#L17 is returning .env only so the environment file(s) is/are never found/loaded.

As a workaround I have hardcoded the following inside the Application class block in application.rb

module YourApp
  class Applicaton
    Dotenv.load Rails.root.join('file.env')
    Spring.watch Rails.root.join('file.env') if defined?(Spring)
    ...
  end
end

rpassis avatar Oct 17 '14 04:10 rpassis

It looks like Rails 4.2 changed when the before_configuration callback is fired. It used to be called right after extending Rails::Application, but now it looks like it's not called until the instance of Application is instantiated, which happens as soon as config is first referenced.

For example, when I start the following application with dotenv-rails and a .env file with FOO=bar…

puts "before app: #{ENV["FOO"].inspect}"

module SampleApp
  class Application < Rails::Application
    puts "inside of app: #{ENV["FOO"].inspect}"

    # referencing the config will initialize the Application class
    config 

    puts "after referencing config: #{ENV["FOO"].inspect}"
  end
end

…I get this output:

before app: nil
inside of app: nil
after referencing config: "bar"

@dcunited001 @rpassis Can you confirm that this is consistent with what you are seeing? If so, you can call Dotenv::Railtie.load to initialize dotenv if you need it sooner.

bkeepers avatar Oct 17 '14 21:10 bkeepers

I'm actually getting nil for all 3 calls even after referencing config

The issue I am seeing is that Rails.root.join('.env') returns .env when (I assume) it should be returning the name of the .env file(s) in the app root folder.

The only way I can get it to work it to "hardcode" the file name and call Dotenv.load path_to_file directly.

Alternatively, you can iterate through an array of matching files using:

Dir.glob(Rails.root.join('*.env').each do |path|
  Dotenv.load path
  Spring.watch path if defined?(Spring)
end

By the way I am seeing this on Rails 4.1.6

rpassis avatar Oct 17 '14 23:10 rpassis

Doh! I feel so dumb right now. For some reason I assumed dotenv would look for any file with a .env extension rather than simply a file named .env. That was my problem and everything is working fine (at least with Rails 4.1.6).

rpassis avatar Oct 18 '14 00:10 rpassis

I'm also seeing the nil error when calling :require => 'dotenv/rails-now', running Rails 4.1.4 and gem dotenv-rails 1.0.2. I'm not 100% familiar with the inner workings of Rails but I'm guessing that since we're calling Rails.root during the Bundler step (before we initialize our Rails app) that the method isn't properly defined. Any help would be much appreciated, thanks!

orenyk avatar Oct 19 '14 04:10 orenyk

I just encountered an issue where any changes to my .env file weren't loading. Upgrading from dotenv/dotenv-rails 0.11.1 to 1.0.2 fixed the issue. Using Rails 4.1.6.

dideler avatar Oct 25 '14 20:10 dideler

I'm having the same issue as @orenyk.

@bkeepers re: the workaround to call Dotenv::Railtie.load if we need the env vars sooner... wasn't the :require => 'dotenv/rails-now' there to ensure other gems which need to access the env vars would be ok? Can you think of a way to get around this?

EDIT: Looks like rails console is not working either. Just running:

$ bundle exec rails console

doesn't load all models and aliases - I assume due to env vars not being initialized in time.

Running this:

$ bundle exec dotenv rails console

loads up all the rails models properly

UPDATE: when I start up the rails console like this:

$ bin/rails c production

I get the expected result. Still digging into this...

sarkis avatar Oct 28 '14 18:10 sarkis

This is the PR to add the Rails.root back into Railtie before_configuration... https://github.com/rails/rails/commit/e1404ec824d263d25717468c6f4b73ee7b2bdf4c

Unfortunately, it looks like it will be in the 4.2.x release of Rails

sarkis avatar Nov 11 '14 19:11 sarkis

#158 fixed the issue with Rails.root being nil.

Let me know if anyone sees issues with 4.2 not working. I have a few 4.2 apps that are working great.

bkeepers avatar Jan 22 '15 01:01 bkeepers

Unfortunately, I still don't seem to be getting my ENV variables loaded from my .env file. Running Rails 4.2 and dotenv-rails 1.0.2. Any thing I might be missing? Rails.root is returning the proper path.

davidsantoso avatar Jan 24 '15 19:01 davidsantoso

Can you post what you have for dotenv in your gemfile?

bkeepers avatar Jan 25 '15 16:01 bkeepers

Definitely.

source 'https://rubygems.org'

ruby '2.1.3'

gem 'rails', '4.2.0'
gem 'pg'
gem 'jbuilder', '~> 2.0'
gem 'bcrypt', '~> 3.1.7'
gem 'unicorn'
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'dotenv-rails', :groups => [:development, :test]

group :development do
  gem 'byebug'
  gem 'spring'
end

As a side note, using gem 'dotenv-rails', :require => 'dotenv/rails-now' causes the same error as the original issue comment of undefined method 'join' for nil:NilClass on line 17 of rails.rb.

davidsantoso avatar Jan 25 '15 20:01 davidsantoso

If you try the latest unreleased version (add :github => "bkeepers/dotenv" to your gemfile and run bundle update) the exception should go away.

But that doesn't explain why it's not working without the require. What do you see if you add something like FOO=test to .env, and then inside of config/application.rb?

class MyApplication < Rails::Application
  puts "FOO is #{ENV["FOO"].inspect}"
  …
end

bkeepers avatar Jan 26 '15 02:01 bkeepers

I got a FOO is nil.

Adding :github => "bkeepers/dotenv" did fix it though.

davidsantoso avatar Jan 26 '15 03:01 davidsantoso

I had this same issue and it was fixed after changing my Gemfile to:

gem 'dotenv-rails', :github => "bkeepers/dotenv"

mpchlets avatar Feb 12 '15 20:02 mpchlets

Hmm .. . I'm not getting it to work: #gemfile

source 'https://rubygems.org'
ruby '2.1.5'
gem 'rails', '4.2.0'
group :development, :test do
  gem 'dotenv-rails', github: "bkeepers/dotenv"
end

#.env

OMNIAUTH_PROVIDER_KEY="12345"

in application.rb

Bundler.require(*Rails.groups)
Dotenv::Railtie.load

rails c

puts ENV['OMNIAUTH_PROVIDER_KEY']
-> 'xxxxx' (where xxxxx is not 12345)

Not sure how to get the env to load correctly.

tbbooher avatar Feb 14 '15 11:02 tbbooher

disregard: found i was loading a variable by the same name in my .zshrc

tbbooher avatar Feb 16 '15 23:02 tbbooher

Still not working for me on Rails 4.2.0... Installed the github version of this gem but the problem persisted. I had to manually add Dotenv::Railtie.load to my application.rb for it to load in time. Sorry but to me it isn't clear from the previous discussions whether Dotenv::Railtie.load is a required step, or is the newest version of the gem supposed to fix it already? If so I might be doing wrong somewhere?

x-ji avatar Feb 23 '15 04:02 x-ji

Using Rails 4.2.1 and dotenv-rails 2.0.1, I experience the same issue where it's not loading env vars. No resolution atm. Dropping in Dotenv::Railtie.load doesn't fix it, neither does loading the gem at the top of my gemfile.

If I inspect ENV from inside my unicorn config, it contains no traces of anything set in my .env file.

nickjj avatar May 17 '15 15:05 nickjj

I'm just loading "dotenv-rails" in application.rb as a workaround.

So, right after you load all of the rails dependencies, try adding require 'dotenv-rails'

Note: I'm using the Bundler.setup(:default, Rails.env) strategy within my app.

DemitryT avatar Jun 03 '15 14:06 DemitryT