Add Rake/RequireOutsideTask cop - #64
Open
corsonknowles wants to merge 1 commit into
Open
Conversation
corsonknowles
added a commit
to corsonknowles/rubocop-rake
that referenced
this pull request
Jul 1, 2026
corsonknowles
force-pushed
the
add-require-outside-task-cop
branch
from
July 1, 2026 21:13
0576947 to
7953ad5
Compare
corsonknowles
marked this pull request as ready for review
July 2, 2026 04:48
Adds a new cop, `Rake/RequireOutsideTask`, that flags `require`/`require_relative`
calls not inside a task block.
```ruby
require 'foo'
task :foo do
Foo.do_something
end
namespace :foo do
require 'foo'
task :bar do
Foo.do_something
end
end
task :foo do
require 'foo'
Foo.do_something
end
Rake::Task['db:seed'].enhance do
require 'foo'
Foo.do_something
end
```
A rake file's top level - including the body of a `namespace` block - is
evaluated every time the file is loaded (i.e. on every `rake` invocation), not
only when the task runs. A require placed there is paid on every load. Moving it
into the task (or a `Rake::Task#enhance` block) loads the dependency lazily, only
when the task that needs it runs. This complements the existing
`Rake/MethodDefinitionInTask` / `Rake/ClassDefinitionInTask` family.
Rakefiles are excluded by default, since they are the canonical place to require
task libraries at the top (e.g. `require "bundler/gem_tasks"`,
`require "rspec/core/rake_task"`). Added as `Enabled: pending` per the guidance
for new cops.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
corsonknowles
force-pushed
the
add-require-outside-task-cop
branch
from
August 10, 2026 11:45
7953ad5 to
7b7d947
Compare
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.
What
Adds a new cop,
Rake/RequireOutsideTask, that flagsrequire/require_relativecalls not inside a task block.Why
A rake file's top level — including the body of a
namespaceblock — is evaluated every time the file is loaded (i.e. on everyrakeinvocation), not only when the task runs. A require placed there is paid on every load. Moving it into the task (or aRake::Task#enhanceblock) loads the dependency lazily, only when the task that needs it runs. This complements the existingRake/MethodDefinitionInTask/Rake/ClassDefinitionInTask"the block scope is misleading" family.Known tradeoff (why
Enabled: pending, and the open question)Some top-level requires must stay at load time because they feed task-defining DSLs —
require "bundler/gem_tasks",require 'rspec/core/rake_task'(thenRSpec::Core::RakeTask.new),require 'rubocop/rake_task'(thenRuboCop::RakeTask.new).This repo's own
Rakefilehas two such requires; We could mark them with inline# rubocop:disable Rake/RequireOutsideTask+ a comment to demonstrate the escape hatch, but sinceRakefileis the preferred place to do this over individual rake tasks, we exclude it from the rule by default.Open questions for maintainers:
Includebe**/*.rakeonly (notRakefile), since Rakefiles are the canonical place to require task libraries at the top? (currently, yes)Origin:
extracted and generalized from a custom cop thst has run for over a year at Gusto.