Skip to content

Load cops lazily - #66

Merged
koic merged 1 commit into
rubocop:masterfrom
koic:lazy_load_cops
Aug 7, 2026
Merged

Load cops lazily#66
koic merged 1 commit into
rubocop:masterfrom
koic:lazy_load_cops

Conversation

@koic

@koic koic commented Aug 4, 2026

Copy link
Copy Markdown
Member

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 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.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 files loaded after require 'rubocop-rake', and no double registration when a cop file is required directly.

Comment thread lib/rubocop/cop/rake.rb Outdated
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.
@koic
koic merged commit 1b1183d into rubocop:master Aug 7, 2026
9 checks passed
@koic
koic deleted the lazy_load_cops branch August 7, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants