Skip to content

Commit

Permalink
Load Rails Rake tasks only once
Browse files Browse the repository at this point in the history
In #39137, a new `Rake::Application` instance was created per Rake
command invocation.  To ensure that the Rails tasks were defined for
each `Rake::Application`, `rails/tasks.rb` was loaded per instance.
However, `Rake::Application#load_rakefile` loads the application's
Rakefile, which should invoke `Rails.application.load_tasks`, which, in
turn, also loads the Rails tasks.  When a Rake task is defined more than
once, all definition blocks are executed when the task is run.  Hence,
Rails task blocks were being executed twice.

This commit removes the unnecessary load and avoids double execution.

Fixes #40136.
  • Loading branch information
jonathanhefner committed Aug 30, 2020
1 parent 7101489 commit 85c5e82
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 0 additions & 1 deletion railties/lib/rails/commands/rake/rake_command.rb
Expand Up @@ -16,7 +16,6 @@ def perform(task, args, config)
require_rake

Rake.with_application do |rake|
load "rails/tasks.rb"
rake.init("rails", [task, *args])
rake.load_rakefile
if Rails.respond_to?(:root)
Expand Down
4 changes: 4 additions & 0 deletions railties/test/application/rake_test.rb
Expand Up @@ -25,6 +25,10 @@ def test_gems_tasks_are_loaded_first_than_application_ones
assert $task_loaded
end

test "framework tasks are evaluated only once" do
assert_equal ["Rails version"], rails("about").scan(/^Rails version/)
end

test "task backtrace is silenced" do
add_to_config <<-RUBY
rake_tasks do
Expand Down

0 comments on commit 85c5e82

Please sign in to comment.