bkeepers / dotenv

A Ruby gem to load environment variables from `.env`.
MIT License
6.61k stars 505 forks source link

Solve circular require warning. #491

Closed martijnbolhuis closed 9 months ago

martijnbolhuis commented 9 months ago

See issue https://github.com/bkeepers/dotenv/issues/490.

bkeepers commented 9 months ago

Thanks @martijnbolhuis! I'll get this released soon.

Burgestrand commented 8 months ago

Hi! This change caused me some issues, so I'll write it here in lack of a better place in case somebody comes across the same problem.

It manifested as my test suite not being able to run because environment variables from .env.test.local weren't loading. I was confused, but we very clearly were loading dotenv-rails, and I assumed that would also load dotenv/rails, which it no longer does.

The cause of events that lead to this issue is:

The reasoning is:

Unfortunately inside of spec/spec_helper.rb we're also using dotenv, so the series of events were:

  1. Load a Rails-running test, which does require "rails_helper"
  2. ... rails_helper then does require "spec_helper"
  3. ... spec_helper then does require "dotenv", at this point Rails::Railtie is not defined, since Rails isn't loaded yet.
  4. ... some time later config/application.rb is loaded
  5. ... which does Bundler.require(*Rails.groups)
  6. ... which does require "dotenv-rails" (we now want the Railtie)
  7. ... and dotenv-rails simply does require "dotenv", but since dotenv is already loaded it stops here.

The end-result is that dotenv/rails is never required, even though we did require dotenv-rails.

The fix for us is to adjust our Gemfile:

gem "dotenv", require: %w[dotenv dotenv/rails]