Skip to content

Commit a32ff0e

Browse files
Exclude application Rake tasks from help
Prior to this commit, `RakeCommand::rake_tasks` (and thus `RakeCommand::printing_commands`) excluded tasks defined in the application's `Rakefile`, but tasks defined in `lib/tasks/*.rake` were still included. This commit modifies `rake_tasks` to exclude any task defined solely by the application. And, as a result, the tasks can now be loaded via `Rake::Application#load_rakefile`, which matches `RakeCommand::perform`.
1 parent 078a1d6 commit a32ff0e

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

railties/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* `bin/rails --help` will now list only framework and plugin commands. Rake
2+
tasks defined in `lib/tasks/*.rake` files will no longer be included. For a
3+
list of those tasks, use `rake -T`.
4+
5+
*Jonathan Hefner*
6+
17
* Allow calling `bin/rails restart` outside of app directory.
28

39
The following would previously fail with a "No Rakefile found" error.

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,27 @@ def perform(task, args, config)
2222
raise UnrecognizedCommandError.new(unrecognized_task)
2323
end
2424

25-
if Rails.respond_to?(:root)
26-
rake.options.suppress_backtrace_pattern = /\A(?!#{Regexp.quote(Rails.root.to_s)})/
27-
end
25+
rake.options.suppress_backtrace_pattern = non_app_file_pattern
2826
rake.standard_exception_handling { rake.top_level }
2927
end
3028
end
3129

3230
private
31+
def non_app_file_pattern
32+
/\A(?!#{Regexp.quote Rails::Command.root.to_s})/
33+
end
34+
3335
def rake_tasks
3436
require_rake
3537

3638
return @rake_tasks if defined?(@rake_tasks)
3739

38-
require_application!
39-
4040
Rake::TaskManager.record_task_metadata = true
4141
Rake.application.instance_variable_set(:@name, "rails")
42-
load_tasks
43-
@rake_tasks = Rake.application.tasks.select(&:comment)
42+
Rake.application.load_rakefile
43+
@rake_tasks = Rake.application.tasks.select do |task|
44+
task.comment && task.locations.any?(non_app_file_pattern)
45+
end
4446
end
4547

4648
def formatted_rake_tasks

railties/test/command/help_integration_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,18 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
2020

2121
assert_equal help, output
2222
end
23+
24+
test "excludes application Rake tasks from command listing" do
25+
app_file "Rakefile", <<~RUBY, "a"
26+
desc "my_task"
27+
task :my_task_1
28+
RUBY
29+
30+
app_file "lib/tasks/my_task.rake", <<~RUBY
31+
desc "my_task"
32+
task :my_task_2
33+
RUBY
34+
35+
assert_no_match "my_task", rails("--help")
36+
end
2337
end

0 commit comments

Comments
 (0)