dotenv: Unable to resolve ENV vars with rails 4.2 and dotenv 2.0.1

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.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

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")

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.

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.

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