spree / deface

Rails plugin that allows you to customize ERB views in a Rails application without editing the underlying view.
MIT License
520 stars 127 forks source link

Override paths being loaded multiple times #234

Open pelargir opened 2 years ago

pelargir commented 2 years ago

TLDR: When a gem in the Gemfile has no Railtie defined (dotenv for example), Rails defaults the railtie.root value to the application's root directory. This causes Deface to load the application's root directory as an override path twice when Rails.application.config.cache_classes is false.

This bit of code is to blame:

# check all railties / engines / extensions / application for overrides
app.railties._all.dup.push(app).each do |railtie|
  next unless railtie.respond_to? :root
  load_overrides(railtie)
end

Appending Rails.application to the list of Railties works fine when no other gems are using the default railtie.root path which is identical to the Rails.application.root path. This results in the overrides being loaded twice, leading to "already initialized constant" warnings when the app boots in development mode (cache_classes = false).

One possible solution would be to append each Railtie root to an array before loading, and return early if the Railtie root already exists in the array. Something like this:

class Environment::Overrides
  def initialize
    @all = {}
    @loaded = []
  end

  def enumerate_and_load(paths, root)
    return if @loaded.include?(root)
    @loaded << root
    # ...
  end
end