Skip to content

Commit 68a3f67

Browse files
Support reentrant calls to RakeCommand.perform
Rake stores the current top-level task and arguments in global state. Invoking another top-level task within the same process requires overwriting this state. Doing so indiscriminately can cause incorrect behavior, such as infinitely repeating the original task. In particular, this is a problem when running one task from another via `rails_command "...", inline: true`. The solution is to save and restore the global state in each call to `RakeCommand.perform` using the `Rake.with_application` method. Fixes rails#39128.
1 parent 4c724df commit 68a3f67

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

railties/lib/rails/commands/rake/rake_command.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ def printing_commands
1515
def perform(task, args, config)
1616
require_rake
1717

18-
ARGV.replace([task, *args]) # set up ARGV for Rake
19-
20-
Rake.application.standard_exception_handling do
21-
Rake.application.init("rails")
22-
Rake.application.load_rakefile
23-
Rake.application.top_level
18+
Rake.with_application do |rake|
19+
load "rails/tasks.rb"
20+
rake.init("rails", [task, *args])
21+
rake.load_rakefile
22+
rake.standard_exception_handling { rake.top_level }
2423
end
2524
end
2625

railties/lib/rails/tasks/statistics.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# While global constants are bad, many 3rd party tools depend on this one (e.g
44
# rspec-rails & cucumber-rails). So a deprecation warning is needed if we want
55
# to remove it.
6-
STATS_DIRECTORIES = [
6+
STATS_DIRECTORIES ||= [
77
%w(Controllers app/controllers),
88
%w(Helpers app/helpers),
99
%w(Jobs app/jobs),

0 commit comments

Comments
 (0)