Load cops lazily - #66
Merged
Merged
Conversation
bquorning
approved these changes
Aug 5, 2026
Follow-up to rubocop/rubocop#15436. Replace the 5 cop requires in `lib/rubocop/cop/rake_cops.rb` with a `Rake` department module (`lib/rubocop/cop/rake.rb`) that registers every cop with the global registry through `RuboCop::Cop::LazyLoader#register_cop` without loading the cop files. A cop's file is loaded only when the cop class is needed, which for an inspection run means only the enabled cops. This follows the same mechanism RuboCop core adopted in rubocop/rubocop#15436 and requires RuboCop 1.89.0+. The `register_cop` directives keep the former require order, because the registration order defines the cop execution order. The 5 helper modules move from eager requires to `autoload` directives in `lib/rubocop/cop/rake/helper.rb`, following the mixin autoloads in rubocop/rubocop-rspec#2215: a helper file is loaded on first constant reference, which happens when a cop class body evaluates. `lib/rubocop/cop/rake_cops.rb` is kept as a deprecated compatibility shim for code that requires it directly. The `new_cop` rake task switches from `inject_require` to `inject_registration`, which core provides for maintaining the `register_cop` directives. A new check in `spec/project_spec.rb` requires every cop file in `lib/rubocop/cop/rake` to be registered exactly once, and `spec/lazy_loading_spec.rb` asserts the lazy behavior itself in subprocesses: no cop or helper files loaded after `require 'rubocop-rake'`, helper constants resolved through their autoloads, and no double registration when a cop file is required directly.
bquorning
approved these changes
Aug 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to rubocop/rubocop#15436.
Replace the 5 cop requires in
lib/rubocop/cop/rake_cops.rbwith aRakedepartment module (lib/rubocop/cop/rake.rb) that registers every cop with the global registry throughRuboCop::Cop::LazyLoader#register_copwithout loading the cop files. A cop's file is loaded only when the cop class is needed, which for an inspection run means only the enabled cops. This follows the same mechanism RuboCop core adopted in rubocop/rubocop#15436 and requires RuboCop 1.89.0+.The
register_copdirectives keep the former require order, because the registration order defines the cop execution order. The 5 helper modules move from the gem entry point to the top of the department module and stay eagerly required: cops include them at class-body evaluation time without requiring them themselves.lib/rubocop/cop/rake_cops.rbis kept as a deprecated compatibility shim for code that requires it directly. Thenew_coprake task switches frominject_requiretoinject_registration, which core provides for maintaining theregister_copdirectives.A new check in
spec/project_spec.rbrequires every cop file inlib/rubocop/cop/raketo be registered exactly once, andspec/lazy_loading_spec.rbasserts the lazy behavior itself in subprocesses: no cop files loaded afterrequire 'rubocop-rake', and no double registration when a cop file is required directly.