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.

Models fail to autoload in Rake files if config.threadsafe!

#5718

if config.threadsafe!, rake tasks in lib/tasks fail to autoload models.

Either eager loading OR autoloading would be great, currently we have neither.

Reported by stefanpenner · September 28th, 2010 @ 05:33 AM

State: stale
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. David Trasbo
    David Trasbo
    • State changed from new to incomplete
    • Importance changed from to Low

    Confirmed on edge Rails:

    $ script/rails g model foo
    ...
    $ rake db:migrate
    (in /Users/dtrasbo/Documents/Code/rails/railties/tmp/ticket_5718)
    ...
    

    lib/tasks/test.rake:

    task :test do
      Foo
    end
    
    $ rake hello
    (in /Users/dtrasbo/Documents/Code/rails/railties/tmp/ticket_5718)
    rake aborted!
    uninitialized constant Object::Foo
    
    (See full trace by running task with --trace)
    

    Please provide a patch (https://rails.lighthouseapp.com/projects/8994/sending-patches) with a fix and/or a failing test.

    October 9th, 2010 @ 03:58 PM

  2. José Valim
    José Valim
    • State changed from incomplete to invalid

    You need to have a dependency on the environment task:

    @@@ruby task :test => :environment do
    Foo end

    
    This will load the environment and eager load your models.
    

    November 7th, 2010 @ 04:54 PM

  3. stefanpenner
    stefanpenner

    José,

    unfortunately with config.threadsafe! enabled loading the environment dependency does not eager load models.

        $ bundle exec rails g model foo 
        ...
        rake db:migrate
        ...
    
        task :foo => :environment do
          Foo
        end
    
        $ bundle exec rake foo 
        rake aborted!
        uninitialized constant Foo
        /Users/stefan/tmp/threadsafe/Rakefile:9
    

    November 7th, 2010 @ 05:26 PM

  4. José Valim
    José Valim
    • State changed from invalid to open
    • Assigned user set to José Valim

    Oh snap! Reopening this ticket then.

    November 7th, 2010 @ 06:44 PM

  5. Hudson
    Hudson

    I have noticed the same behaviour with migrations. If I want to use models from my app in migrations, I need to explicitly require the model and any of it's dependencies at the top of the migration. This is only necessary for environments where config.threadsafe! is set.

    January 18th, 2011 @ 06:27 PM

  6. Roger Leite
    Roger Leite

    I know this strange, but, Isn't an expected behaviour ?

    Copied from "http://guides.rubyonrails.org/configuring.html#configuring-action-c...:

    config.action_controller.allow_concurrency should be set to true to allow concurrent (threadsafe) action processing. Set to false by default. You probably don’t want to call this one directly, though, because a series of other adjustments need to be made for threadsafe mode to work properly. Instead, you should simply call config.threadsafe! inside your production.rb file, which makes all the necessary adjustments.

    WARNING:

    Threadsafe operation is incompatible with the normal workings of development mode Rails. In particular, automatic dependency loading and class reloading are automatically disabled when you call config.threadsafe!.

    January 28th, 2011 @ 05:12 PM

  7. Robin Salkeld
    Robin Salkeld

    The problem is actually that eager loading of application classes is disabled for rake tasks. From initializer.rb:

    def load_application_classes

    return if $rails_rake_task
    
    ...
    

    end

    I think this is intended to avoid catch-22s where you have a model class that borks if its database table doesn't exist, but you can't run the migration to create it because loading the Rails environment loads the class.

    The issue is that the threadsafe! option still disables dependencies loading, so you can't refer to any of those classes implicitly if you actually want to.

    I think the fix (perhaps instead of the existing guard in load_application_classes?) might be to ignore the threadsafe! and/or cache_classes options in rake tasks. After all, you're never accepting any controller requests within a rake runtime, so why would you care anyway? :)

    February 5th, 2011 @ 01:10 AM

  8. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 7th, 2011 @ 01:00 AM

  9. rails
    rails
    • State changed from open to stale

    May 7th, 2011 @ 01:00 AM