Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to resolve ENV vars with rails 4.2 and dotenv 2.0.1 #191

Closed
nickjj opened this issue Jun 4, 2015 · 15 comments
Closed

Unable to resolve ENV vars with rails 4.2 and dotenv 2.0.1 #191

nickjj opened this issue Jun 4, 2015 · 15 comments
Labels

Comments

@nickjj
Copy link

@nickjj nickjj commented Jun 4, 2015

Rails version: 4.2.1
dotenv version: 2.0.1
Ruby version: 2.2.0

Gemfile:

source 'https://rubygems.org'

group :development, :test do
  gem 'dotenv-rails', '~> 2.0.1', require: 'dotenv/rails-now'
end

gem 'rails', '~> 4.2.1'
gem 'turbolinks', '~> 2.5.3'
gem 'jquery-rails', '~> 4.0.3'
gem 'jquery-turbolinks', '~> 2.1.0'
#gem 'jbuilder'

gem 'font-awesome-rails', '~> 4.3.0'
gem 'bootstrap-sass', '~> 3.3.4'

gem 'pg', '~> 0.18.2'
gem 'redis-rails', '~> 4.0.0'

gem 'unicorn', '~> 4.9.0'
#gem 'puma', '~> 2.11.2'
gem 'sidekiq', '~> 3.3.4'
gem 'sinatra', '>= 1.4.5', require: false

gem 'whenever', '~> 0.9.4', require: false

gem 'sitemap_generator', '~> 5.0.5'
gem 'favicon_maker', '~> 1.3'
gem 'kaminari', '~> 0.16.3'

group :development do
  gem 'foreman', require: false
  gem 'spring', '~> 1.3.6'
  gem 'web-console', '~> 2.1.2'
  gem 'rack-mini-profiler', '~> 0.9.3'
  gem 'bullet', '~> 4.14.7'
  gem 'meta_request', '~> 0.3.0'
  gem 'railroady', '~> 1.3.0', require: false
end

group :assets do
  gem 'sass-rails', '~> 5.0.0'
  gem 'coffee-rails', '~> 4.1.0'
  gem 'uglifier', '~> 2.7.1'
end

group :doc do
  gem 'sdoc', '~> 0.4.0', require: false
end

Tried dropping in Dotenv::Railtie.load in my application.rb config as stated in a few other issues but all ENV vars are still undefined.

There's nothing special about my app's bootstrap process. It loads in a bunch of env variables from a .env file and then starts a unicorn server.

If you have postgres/redis handy you can install Orats (use the github version) to get an example project to play with, alternatively you can poke around the templated files here:
https://github.com/nickjj/orats/tree/master/lib/orats/templates/includes/new/rails

Note: Some of these files are not exactly what ends up being written to a generated project because certain values get replaced. The overall structure of the files are the same tho.

@seanknox

This comment has been minimized.

Copy link

@seanknox seanknox commented Jun 5, 2015

Why do you have an assets group? Support for that was removed in Rails 4.

@nickjj

This comment has been minimized.

Copy link
Author

@nickjj nickjj commented Jun 5, 2015

Old habits die hard, but that isn't causing a difference in the issue here.

@bkeepers

This comment has been minimized.

Copy link
Owner

@bkeepers bkeepers commented Jun 8, 2015

Here's what I just did:

$ rails --version
Rails 4.2.1
$ rails new test && cd test

Then I added dotenv to the top of the Gemfile exactly as you have it.

$ echo "FOO=bar" > .env
$ rails runner "puts ENV['FOO']"
bar

It's definitely working on a new Rails 4.2.1 project.

all ENV vars are still undefined

Can you elaborate? Are they never defined, or just at a certain point in the bootstrap process? If you use the rails runner command and print them out are they defined?

@nickjj

This comment has been minimized.

Copy link
Author

@nickjj nickjj commented Jun 8, 2015

@bkeepers, Can you run that same test with unicorn 4.9.0 just so our data matches?

They are undefined at the point unicorn tries to read them in from its config. For example if you had a WORKERS variable, it will be undefined (non-existent in the output of puts ENV) on line 1 of the unicorn config.

@bkeepers

This comment has been minimized.

Copy link
Owner

@bkeepers bkeepers commented Jun 8, 2015

Ah, yes. That is an issue and needs to be more clearly documented somewhere. Since dotenv is loaded by rails, the environment variables won't get set until Rails is loaded, which is long after unicorn has done its job.

This is one of the reasons I don't advocate for using dotenv in production.

@nickjj

This comment has been minimized.

Copy link
Author

@nickjj nickjj commented Jun 8, 2015

That's the thing though. I've been using this setup in development for over a year with no issues. I don't use dotenv in production, this is just development.

I use unicorn in development because I use unicorn in production.

@oskarpearson

This comment has been minimized.

Copy link

@oskarpearson oskarpearson commented Jun 20, 2015

I've just run into a similar problem, I'm pretty sure the issue is that you're using https://github.com/rails/spring in development

Spring persists in the background with precompiled version of the app in memory. Since it's already loaded the Gems, something in the load process doesn't get triggered.

Try 'ps -efl |grep -i spring' and kill the spring processes. Then restart the app with rails console or similar. I'm pretty sure that your ENV variables will appear.

@nickjj

This comment has been minimized.

Copy link
Author

@nickjj nickjj commented Jun 20, 2015

I do use spring in development mode but the issue persists without any spring instances running.

@alxgsv

This comment has been minimized.

Copy link

@alxgsv alxgsv commented Oct 12, 2015

For those who will come here from Google. Simplest way to deal with it is these two lines in the beginning of unicorn config:

require "dotenv"
Dotenv.load("/path/to/env/.env")
@bf4

This comment has been minimized.

Copy link

@bf4 bf4 commented Dec 11, 2015

export DISABLE_SPRING=1 and relax. Use zeus if you want your pre-loader to fail in obvious way. :)

@nickjj

This comment has been minimized.

Copy link
Author

@nickjj nickjj commented Dec 13, 2015

@longman That will work for Unicorn but if you use env variables in sidekiq.yml you will get the same issue. It seems like a band-aid fix to patch all these files.

@bf4 Ah. I haven't tested it personally but maybe that will fix it.

@ldenman

This comment has been minimized.

Copy link

@ldenman ldenman commented Mar 22, 2016

@bf4 that helped me out. Thanks!

@stale

This comment has been minimized.

Copy link

@stale stale bot commented Mar 15, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 15, 2017
@stale stale bot closed this Apr 1, 2017
@skalee

This comment has been minimized.

Copy link

@skalee skalee commented Feb 27, 2018

I have added <% require "dotenv" ; Dotenv.load %> on top of my sidekiq.yml, and that helped. I have removed Spring from my Gemfile so it couldn't be a reason.

@jhirn

This comment has been minimized.

Copy link

@jhirn jhirn commented Jan 2, 2020

Careful with @skalee's suggestion if using dotenv-rails and expecting .env.production or .env.staging to override.

I had to add <% require "dotenv" ; require "rails"; require "dotenv/rails-now"; Dotenv.load %> to get the precedence to work as described in https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use. Not sure if I'l stick with this solution however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.