This project is archived and is in readonly mode.

#1524 ✓resolved
Matt Jones

Gem plugins, autoloading, and dev mode reloading

Reported by Matt Jones | December 7th, 2008 @ 06:24 PM | in 3.0.2

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.

Comments and changes to this ticket

  • Matt Jones

    Matt Jones December 7th, 2008 @ 08:24 PM

    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?

  • Matt Jones

    Matt Jones December 7th, 2008 @ 08:33 PM

    • 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.

  • DHH

    DHH December 8th, 2008 @ 11:32 PM

    • Assigned user set to “josh”
  • josh

    josh December 9th, 2008 @ 08:15 PM

    • Assigned user changed from “josh” to “Jeremy Kemper”
  • Michael Koziarski
  • Yehuda Katz (wycats)
  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) June 22nd, 2009 @ 04:24 PM

    • 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.

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) February 23rd, 2010 @ 10:13 PM

    • State changed from “new” to “resolved”

    Gem loading has been re-examined :)

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “”
  • klkk

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages