Comments (8)
FYI this issue only occurs for me on my Rails project when I use dotenv-rails
and Dotenv::Railtie.load
when running rake. Does not occur when using dotenv
with Dotenv.load
.
Tried with dotenv-rails
2.0.2.
from dotenv.
The more I think about it, option 1 has the same problem as the current issue. Test helpers load config/environment.rb
, which loads config/application.rb
and then initializes. So there's not a good place for the spec helper to load the .env.test
in the bootstrap process.
Option 4: Continue to load .env
immediately, but wait to load .env.#{Rails.env}
before initialization. This would allow most ENV
vars to still be available in config/application.rb
, but environment specific configs would not be available until just before config/environments/*
and config/initializers/*
are loaded.
from dotenv.
In the app we are currently working on all the environment specific vars are used in modules and classes in our lib
folder, and each of those is required at the bottom of config/application.rb
. We could move that require
to an initializer in order to support option 4.
In our specific case option 3 also works (at least as our config/application.rb
stands right now).
Option 2 sounds like it adds too much complication to be worth it.
from dotenv.
Another way to avoid passing RAILS_ENV=test
is to use different keys for development, test, and production environment:
MYSQL2_DATABASE_DEVELOPMENT=
MYSQL2_USERNAME_DEVELOPMENT=
MYSQL2_PASSWORD_DEVELOPMENT=
MYSQL2_HOST_DEVELOPMENT=
MYSQL2_DATABASE_TEST=
MYSQL2_USERNAME_TEST=
MYSQL2_PASSWORD_TEST=
MYSQL2_HOST_TEST=
development:
adapter: mysql2
encoding: utf8
collation: utf8_unicode_ci
database: <%= ENV['MYSQL2_DATABASE_DEVELOPMENT'] %>
username: <%= ENV['MYSQL2_USERNAME_DEVELOPMENT'] %>
password: <%= ENV['MYSQL2_PASSWORD_DEVELOPMENT'] %>
host: <%= ENV['MYSQL2_HOST_DEVELOPMENT'] %>
test:
adapter: mysql2
encoding: utf8
collation: utf8_unicode_ci
database: <%= ENV['MYSQL2_DATABASE_TEST'] %>
username: <%= ENV['MYSQL2_USERNAME_TEST'] %>
password: <%= ENV['MYSQL2_PASSWORD_TEST'] %>
host: <%= ENV['MYSQL2_HOST_TEST'] %>
from dotenv.
I just ran into this trying to use dotenv with DATABASE_URL
...ugh, so close and yet so far. I was excited.
I notice we're talking specifically about Rails rake tasks. Perhaps a solution could involve having dotenv-rails reach in and apply a new :dotenv_test
task as a prerequisite for Rails test tasks?
from dotenv.
I was just burned by this also. I basically just have a Dotenv.overload '.env.test' if Rails.env.test?
in an initializer right now. Not ideal but it fixed the issue
from dotenv.
Still seeing this with dotenv-rails & dotenv 2.0.1. Specifically, rake spec
is picking up config from .env.development
and not .env.test
.
Removing dotenv-rails and just using dotenv 2.0.1 by manually calling Dotenv.load in spec_helper doesnโt load .env.development
or .env.test
.
from dotenv.
As already @dideler said, I solved using dotenv
gem instead of dotenv-rails
.
Simply load the environment in application.rb
and override where you need it (ex. test_helper.rb
:
# config/application.rb
# ...
Bundler.require(*Rails.groups)
# Load env variables
require 'dotenv'
Dotenv.load ".env.local", ".env.#{Rails.env}"
# ...
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require 'dotenv'
Dotenv.overload ".env.#{Rails.env}"
# ...
and for tasks which address test environment
# force test environment
ENV['RAILS_ENV'] = 'test'
require 'dotenv'
Dotenv.overload ".env.#{Rails.env}"
namespace :test do
desc "Console in test environment"
task :console do
system("bin/rails c")
end
end
tested with:
- ruby 2.3.0
- rails 5.0.0
- dotenv 2.1.1
from dotenv.
Related Issues (20)
- Just HOT 1
- Special characters are being escaped from the value of the environment variables HOT 3
- dotenv-rails loads dev environment when running system tests with Rails 7 HOT 5
- `Dotenv.load` call causes Rails app to be loaded twice HOT 2
- Not specifying required ruby version on Gemfile causes my bundler with ruby 2.5.x pull dotenv 2.8.0 HOT 6
- template is not created in working directory HOT 2
- Question on Removal of ### What other .env* files can I use? FAQ HOT 1
- README should explicitly call out the gem needed is `dotenv-rails` HOT 4
- environment specific env not loaded when using require: 'dotenv/load' HOT 1
- Rails < v6.1: LocalJumpError in tagged logging HOT 2
- autorestore: can't modify frozen Hash HOT 7
- Use existing TaggedLogger instead of re-wrapping it with `TaggedLogging` HOT 4
- Ruby warning: circular require considered harmful HOT 2
- v3.0.3 causes regression with semantic-logger v4.15.0 HOT 2
- [BUG] All logs are tagged with "dotenv" HOT 1
- Dotenv's tagged method conflicts with SemanticLogger's HOT 1
- loading multiple files are not correctly overwriting env variables HOT 1
- ActiveSupport::TestCase teardown error: undefined method `keys' for nil HOT 3
- Use ERB in env file? HOT 3
- Arbitrary code execution vulnerability (or intended behavior?)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dotenv.