laserlemon / figaro

Simple Rails app configuration
MIT License
3.77k stars 287 forks source link

Railties loading after Gem causes load order issue #250

Closed jharbert closed 7 years ago

jharbert commented 7 years ago

Bonsai Elasticsearch Rails is a simple gem that requires the environment variable ENV['BONSAI_URL'] to be set.

Though I'm listing bonsai-elasticsearch-rails above figaro in my Gemfile, the application.yml file is not parsed before Bonsai Elasticsearch Rails is loaded, and causes Bonsai Elasticsearch Rails to not find the environment variable.

I believe this is because Figaro.load is kicked off in Railties here: https://github.com/laserlemon/figaro/blob/master/lib/figaro/rails/railtie.rb#L5, and that this takes place after the gems are loaded.

So even explicitly calling:

require 'figaro'
require 'bonsai-elasticsearch-rails'

in my application.rb file does not allow Bonsai Elasticsearch Rails to use the environment variable loaded with Figaro.

laserlemon commented 7 years ago

Your assessment is totally correct. Currently, Figaro depends on some Rails constructs like Rails.env and Rails.root, which means that the before_configuration Rails hook is as early as we can load Figaro. The alternative here would be to defer requiring bonsai-elasticsearch-rails until after Figaro is loaded. For example:

# Gemfile

gem "figaro"
gem "bonsai-elasticsearch-rails", require: false
# config/application.rb

Bundler.require(*Rails.groups)

module FooBar
  class Application < Rails::Application
    require "bonsai-elasticsearch-rails"
  end
end

Not ideal but I hope that helps!