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.

Observed Model Dependent on App Initializer Causes rake gems:install Failure

#1286

(on Rails 2.1.2 Gems) A Model that depends on an application initializer, and that has an Observer, fails to load when a Gem dependency is missing. This causes rake gems:install to fail.

In this example app:

http://github.com/Bill/observers...

we have a Model Splat that calls the class method "extend_me" which is defined on ActiveRecord::Base by the application initializer in initialize_splat.rb.

If the required Gem (arbitrary gem "Bill-route_name_for" is specified in the example) is missing (not yet installed) then rake gems:install fails before gem installation is attempted to wit:


$ rake gems:install
(in /Users/Bill/Projects/Rails/ObserversLoadedWhenGemsAreMissing)
rake aborted!
undefined local variable or method `extend_me' for Splat(Table doesn't exist):Class

This is because the application initializer in initialize_splat.rb needs to be loaded before the model in splat.rb is loaded. But that application initializer is skipped when gem dependencies are missing.

The model (Splat) has an observer in models/splat_observer.rb Rails::Initializer#prepare_dispatcher does not respect the @gem_dependencies_loaded flag so it calls Dispatcher.define_dispatcher_callbacks which in turn calls ActiveRecord::Base.instantiate_observers.

side note: Rails::Initializer#load_observers does check @gem_dependencies_loaded and it won't load observers when gems aren't loaded (good!)

Recommend modifying Rails::Initializer#prepare_dispatcher's guard condition

is:

def prepare_dispatcher return unless configuration.frameworks.include?(:action_controller) … end

should be:

def prepare_dispatcher return unless configuration.frameworks.include?(:action_controller) && @gem_dependencies_loaded … end

Since we don't need the dispatcher if Gem dependencies are missing.

Reported by Bill Burcham · October 28th, 2008 @ 02:34 PM

State: resolved
Milestone: 3.x
Assigned to: Matt Jones Matt Jones
Importance: none

Activity

  1. 20
    20
    • Tag changed from 2.1, gems, initializer, install, observer, rake to 2.1, gems, initializer, install, observer, patch, rake
    • Assigned user set to Rick

    Agreed, this does look like a problem. Solved by using preinitializer.rb to monkeypatch.

    Rails::Boot.class_eval do
      def run
        load_initializer
        Rails::Initializer.class_eval do
          def prepare_dispatcher
            return unless configuration.frameworks.include?(:action_controller) && @gems_dependencies_loaded
            require 'dispatcher' unless defined?(::Dispatcher)
            Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
            Dispatcher.new(Rails.logger).send :run_callbacks, :prepare_dispatch
          end
        end
        Rails::Initializer.run(:set_load_path)
      end
    end
    
    

    attaching patch.

    January 16th, 2009 @ 07:35 PM

  2. 20
    20

    I forgot to add... test run a-ok and i believe this makes gem dependencies work more like they're expected to (at least to me). Thanks for digging it up, Bill.

    January 16th, 2009 @ 07:40 PM

  3. Matt Jones
    Matt Jones

    Can you try the patch in #1750 (against 2.3, unfortunately)? It breaks out of the initializer entirely if gems are missing in a gem rake task.

    February 18th, 2009 @ 01:39 AM

  4. Steven Soroka
    Steven Soroka

    Honestly, I don't get why rails tries to load the environment at all without the initializers. It utterly and completely fails in my project and makes gem "management" with config.gem completely useless.

    gem tasks should not require the environment to run successfully, imho.

    February 19th, 2009 @ 05:08 PM

  5. Pratik
    Pratik
    • Assigned user changed from Rick to Matt Jones

    March 9th, 2009 @ 02:09 PM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  7. Ryan Bigg
    Ryan Bigg
    • State changed from new to resolved
    • Tag changed from 2.1, gems, initializer, install, observer, patch, rake to 2.1
    • Importance changed from to

    This ticket hasn't received an update in over a year and will be closed. If this is still an issue, please reply and I will re-open it.

    October 11th, 2010 @ 04:47 AM