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

Raise exception if nested file will never be found #19537

Merged
merged 3 commits into from
Nov 20, 2019

Conversation

Fryguy
Copy link
Member

@Fryguy Fryguy commented Nov 20, 2019

It is possible to give autoload an invalid path, and that issue will
never be found until the associated constant is vivified. This causes
issues where people forget to remove the require_nested calls when they
remove the files. Now, it's not possible to forget, because an
exception will be raised as soon as the parent file is loaded.

This PR is built upon some commits to clarify the code and remove dead code, which came up as part of my research on how this method works.

See also #5270 , where the commented code was added. Basically, Rails has trouble with matching nested and top-level const names, and the code in question gets around that by loading both the nested and top-level at the same time.

@carbonin Please review.

@@ -6,23 +6,19 @@ def require_nested(name)
filename = "#{self}::#{name}".underscore
filename = name.to_s.underscore if self == Object
if Rails.application.config.cache_classes
raise LoadError, "No such file to load -- #{filename}" unless ActiveSupport::Dependencies.search_for_file(filename)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main part of the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to do this in development and test only?
to not incur the hit to the disk?

Or do we consider the fstat calls across the load path minor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's the current issue @kbrock, right now we're setting cache_classes = true in test and prod which is causing this issue to not be found in test, but simultaneously I would think we want test to be as close to prod as possible in this regard.

FWIW the direct call to require_dependency in the else branch will fail in the case addressed by this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbonin aah, so this check will never be run in development. thanks (not what I wanted, but more clear)

@Fryguy
Copy link
Member Author

Fryguy commented Nov 20, 2019

Had to rebase due to #19538

Fryguy added a commit to Fryguy/manageiq-cross_repo-tests that referenced this pull request Nov 20, 2019
It is possible to give autoload an invalid path, and that issue will
never be found until the associated constant is vivified.  This causes
issues where people forget to remove the require_nested calls when they
remove the files.  Now, it's not possible to forget, because an
exception will be raised as soon as the parent file is loaded.
@miq-bot
Copy link
Member

miq-bot commented Nov 20, 2019

Checked commits Fryguy/manageiq@01d3dbc~...2a116ec with ruby 2.5.5, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
1 file checked, 1 offense detected

lib/extensions/require_nested.rb

@carbonin carbonin merged commit f650f32 into ManageIQ:master Nov 20, 2019
@carbonin carbonin added the core label Nov 20, 2019
@carbonin carbonin added this to the Sprint 125 Ending Nov 25, 2019 milestone Nov 20, 2019
@Fryguy Fryguy deleted the require_nested_fix branch November 20, 2019 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants