This project is archived and is in readonly mode.

#1286 ✓resolved
Bill Burcham

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

Reported by Bill Burcham | October 28th, 2008 @ 02:34 PM | in 3.x

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

Comments and changes to this ticket

  • 20

    20 January 16th, 2009 @ 07:35 PM

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

  • 20

    20 January 16th, 2009 @ 07:40 PM

    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.

  • Matt Jones

    Matt Jones February 18th, 2009 @ 01:39 AM

    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.

  • Steven Soroka

    Steven Soroka February 19th, 2009 @ 05:08 PM

    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.

  • Pratik

    Pratik March 9th, 2009 @ 02:09 PM

    • Assigned user changed from “Rick” to “Matt Jones”
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 04:47 AM

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

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>

Attachments

Tags

Pages