Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Gem plugins, autoloading, and dev mode reloading

#1524

I'm not sure if this is really a bug, but I'm documenting it so that others that run across it won't be confused...

For a gem plugin, there is a difference between using config.gem in the environment file and manually including the gem in an intializer (with gem 'name' / require 'name'). In the latter case, anything that's autoloaded from the gem is reloaded on each request. This is unexpected, as using config.gem doesn't exhibit this behavior.

I encountered this bug when a gem plugin that monkey-patched ActionController was failing on the second request in dev mode with a "Stack too deep" error.

Reported by Matt Jones · December 7th, 2008 @ 06:24 PM

State: resolved
Milestone: 3.0.2
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: none

Activity

  1. Matt Jones
    Matt Jones

    Putting this code in initializers/gem_hook.rb

    
    module GemHook
      def self.included(base) #:nodoc:
        base.instance_eval do
          unless defined? gem_without_load_once_paths
            alias_method_chain :gem, :load_once_paths
          end
        end
      end
    
      def gem_with_load_once_paths(gem_name, *version_requirements)
        old_load_paths = $LOAD_PATH.dup
        result = gem_without_load_once_paths(gem_name, *version_requirements)
        new_load_paths = $LOAD_PATH.dup
        added_paths = new_load_paths - old_load_paths
        ActiveSupport::Dependencies.load_paths |= added_paths
        ActiveSupport::Dependencies.load_once_paths |= added_paths
        result
      end
    end
    
    Kernel.instance_eval { include GemHook }
    

    makes the behavior consistent. It would be straightforward to incorporate this into dependencies.rb, but how would this function be tested?

    December 7th, 2008 @ 08:24 PM

  2. Matt Jones
    Matt Jones
    • Tag changed from 2.2-stable, autoload, dependencies, gems to 2.2-stable, autoload, bug, dependencies, gems
    • Title changed from Gem plugins and dev mode reloading to Gem plugins, autoloading, and dev mode reloading

    On further investigation, this is a more serious problem - loadable_constants_for_path won't find constants defined in gems that aren't loaded with config.gem. This should either be fixed (similar to above) or documented clearly.

    December 7th, 2008 @ 08:33 PM

  3. DHH
    DHH
    • Assigned user set to josh

    December 8th, 2008 @ 11:32 PM

  4. josh
    josh
    • Assigned user changed from josh to Jeremy Kemper

    December 9th, 2008 @ 08:15 PM

  5. Michael Koziarski
  6. Yehuda Katz (wycats)
  7. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • Assigned user changed from Jeremy Kemper to Yehuda Katz (wycats)

    We're going to take a fresh look at reloading and gem plugins in general in 3.0. We'll make sure to address all open concerns about them.

    June 22nd, 2009 @ 04:24 PM

  8. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to resolved

    Gem loading has been re-examined :)

    February 23rd, 2010 @ 10:13 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM