Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails console reload! does not eager load when told to #24819

Closed
sirwolfgang opened this issue May 2, 2016 · 16 comments
Closed

Rails console reload! does not eager load when told to #24819

sirwolfgang opened this issue May 2, 2016 · 16 comments
Labels

Comments

@sirwolfgang
Copy link
Contributor

sirwolfgang commented May 2, 2016

Steps to reproduce

Using the following STI class structure, with eager loading on.

# config/development.rb
config.eager_load = true

# app/models/class_a.rb
class ClassA < ActiveRecord::Base
  def self.types
    descendants.map(&:name)
  end
end

# app/models/class_b.rb
class ClassB < ClassA
end
rails c
Loading development environment (Rails 4.2.6)
irb(main):001:0> ClassA.types
=> ["ClassB"]
irb(main):002:0> reload!
Reloading...
=> true
irb(main):004:0> ClassA.types
=> []

Expected behavior

The reload! command should of reloaded all of the eager loaded content.

Actual behavior

The reload! command does not seem to reload all the eager loaded content.

System configuration

Rails version: Rails 4.2.6
Ruby version: ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

@maclover7
Copy link
Contributor

Can you create an executable test script using one of the templates here, that would demonstrate the problem you are seeing? I think there are some configuration switches you have on, that we can't see :)

@mctaylorpants
Copy link
Contributor

I'm able to reproduce this issue on a new Rails 4.2.6 project following the steps above. (Although I'm not sure how to write an isolated test for it since it seems to rely on having loading a whole Rails app).

@maclover7
Copy link
Contributor

cc @fxn, this probably has to do with active_support/dependencies

@sgrif
Copy link
Contributor

sgrif commented May 19, 2016

I don't think we support using reload! in the console if eager_load is set to true.

@matthewd
Copy link
Member

As long as cache_classes is false, it should work... but it does sound plausible that it'll ignore eager_load, and just unload things, leaving them to autoload when mentioned.

If that's the case, it should be fairly straight-forward to plug the desired behaviour into the Reloader. PRs welcome. 😄

@davidakachaos
Copy link

ping @fxn did you have a chance to look at this?

@fxn
Copy link
Member

fxn commented Jul 12, 2016

What @matthewd said.

Have you had a look at http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-and-sti?

@davidakachaos
Copy link

@sirwolfgang Do you have enough info?

@stale stale bot added the stale label Mar 27, 2017
@stale
Copy link

stale bot commented Mar 27, 2017

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 team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@stale stale bot closed this as completed Apr 3, 2017
@longtengz
Copy link

anyone knows if reload! doesn't load the eager-loaded stuff, what does it actually do?

@rails-bot rails-bot bot removed the stale label Mar 9, 2018
@xathanex
Copy link

Digging through ActiveSupport::Reloader docs and Autoloading and Reloading Constants guide, I've ended up with the following:

# config/initializers/eager_reloading.rb
Rails.application.reloader.after_class_unload do  
    Rails.application.eager_load! if Rails.application.config.eager_load
end

Seems to work like a charm for me - both for console reload! and development code reloading.
Rails 5.2.1 / Ruby 2.5.1-p57

@quentindemetz
Copy link

I am still able to reproduce on 6.1.4.6

fxn added a commit that referenced this issue Apr 12, 2022
@fxn
Copy link
Member

fxn commented Apr 12, 2022

This is fixed by 9971f77.

I recall this was not possible in classic because eager loading was a recursive require, but in Zeitwerk eager loading is a recursive autoloading, so to speak, so you can eager load again after reload.

fxn added a commit that referenced this issue Apr 12, 2022
@fxn
Copy link
Member

fxn commented Apr 13, 2022

I recall this was not possible in classic because eager loading was a recursive require

No, I was mistaken, it used require_dependency.

@eileencodes
Copy link
Member

@fxn the change made to fix this is causing Railties tests to fail https://buildkite.com/rails/rails/builds/85890

@fxn
Copy link
Member

fxn commented Apr 13, 2022

This patch was unfortunate. Normally I work on a branch and wait for CI, I believed I was working on a branch as usual, but wasn't and pushed directly. And then, missed CI was red.

It's fixed in main now. CI remains red, but failure seems unrelated to this patch. I'll update 7-0-stable now, thanks for the message @eileencodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests