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 tasks fail when cache_classes=true

#1548

If I have a model like:


# MyModel.rb
require 'fastercsv'
class MyModel < ActiveRecord::Base; end

and in my config file I have


# ....
config.cache_classes = true
config.gem 'fastercsv'

and I don't have the fastercsv gem installed, the rake gems tasks cannot run.

In initializer.rb, cache_classes=true ends up kicking off the class preloading, which then ends up trying to require a gem that's not yet been installed, ... (another gems:install catch-22).

The easy fix is fairly straightforward (will provide a patch & tests), but load_application_classes needs to just check for $rails_gem_installer to be set, and if so don't try and do the preloading.

It kind of seems like the $rails_gem_installer flag is a bit of a hack, and that really rake gems:install should be setting what initializer steps get ran (seems like very few of them), but that's a bigger fix.

Here's a workaround you can drop into environment.rb in the mean-time


# this is a workaround for a Rails issue where cache_classing can lead to not yet installed
# gems to cause the gem:install to fail
module Rails
  class Initializer
    def load_application_classes_with_check_for_gem_loading
      load_application_classes_without_check_for_gem_loading unless $rails_gem_installer
    end
    alias_method :load_application_classes_without_check_for_gem_loading, :load_application_classes
    alias_method :load_application_classes, :load_application_classes_with_check_for_gem_loading
  end
end

Reported by Richie Vos · December 10th, 2008 @ 06:31 PM

State: stale
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Matt Jones
    Matt Jones

    Don't use "require 'fastercsv'" in your model - config.gem will do it for you if the gem is available.

    December 20th, 2008 @ 09:16 PM

  2. dstrelau
    dstrelau
    • Tag changed from 2.2.2, bug, cache_classes, initializer, load_application_classes, railties to bug, cache_classes, edge, gems, initializer, load_application_classes, patch, railties, rake

    Actually, this is still a problem, even when not using require in the model. Initializer#load_application_classes runs, even when executing a gems task, which means that if the gem isn't installed, you end up with errors. Note this only happens in production or when config.cache_classes = true.

    Here is how you can replicate: 1) Uninstall 'thoughtbot-paperclip' if you have it installed 2) New rails project, with vendored rails 3) config.gem 'thoughtbot-paperclip', :lib => 'paperclip', :source => 'http://gems.github.com' 4) script/generate model foo 5) class Foo < AR; has_attached_file :image; end 6) RAILS_ENV=production rake gems

    Patch against master attached, which just doesn't loaded classes if $rails_gem_installer is set:

    
    def load_application_classes
      if configuration.cache_classes && !$rails_gem_installer
    

    PS: I'd love to see this backported into 2.2 as well. :)

    January 7th, 2009 @ 05:50 PM

  3. dstrelau
    dstrelau
    • Tag changed from bug, cache_classes, edge, gems, initializer, load_application_classes, patch, railties, rake to bug, gems, initializer, patch, railties, rake, tiny

    I'm going to bump this, in the hopes that someone might take a look and care, seeing as this bug essentially makes rake gems:install useless for satisfying gem dependencies on a production server.

    January 29th, 2009 @ 04:22 PM

  4. Matt Jones
    Matt Jones

    Take a look at #1750 - I've attached a patch that basically bails out of the initializer if rake gems tasks are being run and gems are missing. This should stop anything like this from happening.

    Also see #802, which has probably handled this case already by turning off eager loading in all rake tasks.

    February 18th, 2009 @ 01:15 AM

  5. Pratik
    Pratik
    • State changed from new to stale

    Please reopen if this is still an issue.

    August 8th, 2009 @ 08:54 PM