You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SETTINGS__SECTION__SERVER_SIZE=1 SETTINGS__SECTION__SERVER=google.com SETTINGS__SECTION__SSL_ENABLED=false rails c
Settings.section.server_size# => nilSettings.section.server# => nilSettings.section.ssl_enabled# => nil# They are accessible this wayENV['SETTINGS__SECTION__SERVER_SIZE']#=> "1"ENV['SETTINGS__SECTION__SERVER']#=> "google.com"ENV['SETTINGS__SECTION__SSL_ENABLED']#=> "false"
After review I believe this is expected behavior as the gem is just trying to read from ENV namespace if things are loaded into ENV by Heroku or AWS or some other means. It doesn't intend to write environment variables into the ENV namespace itself, it's just reading from it.
I agree that what @deepakmahakale posted should work. @deepakmahakale if you're able to reproduce the issue on a new rails app, we can take a deeper look into what's going on.
@jfadgen, @deepakmahakale, I ran into a similar issue in a recent rails project and figured out that the problem is that ENV variables haven't been loaded yet when the config initializer runs if you're using .env files.
specifically, if you're loading from a .env file, you will likely need load the .env files before the initializers run.
dotenv is initialized in your Rails app during the before_configuration callback, which is fired when the Application constant is defined in config/application.rb with class Application < Rails::Application. If you need it to be initialized sooner, you can manually call Dotenv::Railtie.load.
# config/application.rb
Bundler.require(*Rails.groups)
# Load dotenv only in development or test environment
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end
note that if you're using something like heroku, where the env vars are set on the servers, they will likely be available for the initializers. so you may only need to do this for your dev and test environments.
@pkuczynski I think you guys should think about adding support for .env files - if they're detected, your gem should automatically try to load the contents of .env before config/initializers/config.rb runs
The whole concept of this gem is to store config in the per env configuration files. Using .env and gems like dotenv is a different approach for doing the same thing. They kind of contradictory to each other. So I am not sure if we should support this. @cjlarose what do you think?
yeah, I see what you're saying, but the issue is that a lot of teams use .env files for dev/test environments, but manage environment variables directly on staging/prod servers (e.g. thru Heroku config vars). pretty much every project I've worked on has used this hybrid approach. I honestly haven't seen .env.production really used, certainly not with things like heroku.
so, you're kind of leaving the door open to a lot of confusion if you don't support env vars being preloaded thru something like dotenv, which is a very popular tool.
for me, I wanted to use this gem primarily to store certain configs in a yml file format, which allows for nesting keys and other advantages over long, messy .env files. however, I also wanted to be able to override default values in that yml file thru env vars (i.e. on heroku). this allows me to change these configs without committing new yml files. I think that's a legitimate use case.
but in any case, you guys should at least add a disclaimer about this scenario in the README, since I'm sure others have ran into this issue
this will ensure that dotenv files are loaded before initializers and solve this issue. I think modifying the gemfile is preferable to messing with application.rb in the manner I showed above.
For anyone else coming into this issue and seeing @nick-reshetnikov 's solution, you'll get a warning w/ latest version that dotenv/rails-now is deprecated and instead to use dotenv/load.
gem "dotenv-rails", require: "dotenv/load"
edit: it appears that if your gem is in development/test group, if you try to do a production build it will still try to do the require even though it's in that group, resulting in a LoadError (this might be a bundler bug with specific versions...). So you can either add to application initializer config per the comment above
Or also you can create a file in your /config/initializers that begins with letter after "d", eg "reload_config.rb" and do:
::Config.reload! if Rails.env.development? || Rails.env.test?
to force Config gem to reload and pickup the ENV values now populated from .env file after dotenv is autoloaded.
Activity
harrisjb commentedon Jun 13, 2022
Similar issue here:
Contents of
config/initializers/config.rbrails console does not load into ENV namespace:
harrisjb commentedon Jun 16, 2022
After review I believe this is expected behavior as the gem is just trying to read from
ENVnamespace if things are loaded intoENVby Heroku or AWS or some other means. It doesn't intend to write environment variables into theENVnamespace itself, it's just reading from it.pkuczynski commentedon Jun 16, 2022
Yes @harrisjb we do not populate things back into ENV, we only read from it. So your reported issue is not valid.
pkuczynski commentedon Jun 16, 2022
However what @deepakmahakale posted should work. @cjlarose any idea why it does not?
cjlarose commentedon Jun 16, 2022
I agree that what @deepakmahakale posted should work. @deepakmahakale if you're able to reproduce the issue on a new rails app, we can take a deeper look into what's going on.
jfadgen commentedon Jul 5, 2022
I am having the same issue as @deepakmahakale , in a new Rails app:
.env
I tried downgrading to config gem 2.2, but without luck. I have another project using that version (with Rails 6.1.4.1, Ruby 2.7.4.) successfully.
Happy to try anything if you have any recommendations, thank you.
nick-reshetnikov commentedon Sep 1, 2022
@jfadgen, @deepakmahakale, I ran into a similar issue in a recent rails project and figured out that the problem is that ENV variables haven't been loaded yet when the config initializer runs if you're using
.envfiles.specifically, if you're loading from a
.envfile, you will likely need load the .env files before the initializers run.according to https://github.com/bkeepers/dotenv:
note that if you're using something like heroku, where the env vars are set on the servers, they will likely be available for the initializers. so you may only need to do this for your dev and test environments.
@pkuczynski I think you guys should think about adding support for .env files - if they're detected, your gem should automatically try to load the contents of
.envbeforeconfig/initializers/config.rbrunspkuczynski commentedon Sep 1, 2022
The whole concept of this gem is to store config in the per env configuration files. Using .env and gems like dotenv is a different approach for doing the same thing. They kind of contradictory to each other. So I am not sure if we should support this. @cjlarose what do you think?
nick-reshetnikov commentedon Sep 1, 2022
yeah, I see what you're saying, but the issue is that a lot of teams use
.envfiles for dev/test environments, but manage environment variables directly on staging/prod servers (e.g. thru Heroku config vars). pretty much every project I've worked on has used this hybrid approach. I honestly haven't seen.env.productionreally used, certainly not with things like heroku.so, you're kind of leaving the door open to a lot of confusion if you don't support env vars being preloaded thru something like dotenv, which is a very popular tool.
for me, I wanted to use this gem primarily to store certain configs in a yml file format, which allows for nesting keys and other advantages over long, messy .env files. however, I also wanted to be able to override default values in that yml file thru env vars (i.e. on heroku). this allows me to change these configs without committing new yml files. I think that's a legitimate use case.
but in any case, you guys should at least add a disclaimer about this scenario in the README, since I'm sure others have ran into this issue
jfadgen commentedon Sep 1, 2022
Worth noting that the issue went away for me after changing my Gemfile to bundle
gem "dotenv-rails"instead of
gem "dotenv".nick-reshetnikov commentedon Sep 1, 2022
@jfadgen I in fact saw this issue while using
"dotenv-rails"nick-reshetnikov commentedon Sep 1, 2022
actually, I just discovered that you can require
dotenv/rails-now(https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/rails-now.rb) in your gemfile to solve the issue:this will ensure that dotenv files are loaded before initializers and solve this issue. I think modifying the gemfile is preferable to messing with
application.rbin the manner I showed above.kmordan24 commentedon Oct 3, 2024
For anyone else coming into this issue and seeing @nick-reshetnikov 's solution, you'll get a warning w/ latest version that
dotenv/rails-nowis deprecated and instead to usedotenv/load.gem "dotenv-rails", require: "dotenv/load"edit: it appears that if your gem is in development/test group, if you try to do a production build it will still try to do the require even though it's in that group, resulting in a LoadError (this might be a bundler bug with specific versions...). So you can either add to application initializer config per the comment above
Or also you can create a file in your /config/initializers that begins with letter after "d", eg "reload_config.rb" and do:
to force Config gem to reload and pickup the ENV values now populated from .env file after dotenv is autoloaded.