-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project?Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib/commands/*_command.rb
should load custom Thor commands
#50193
Comments
I looked at this with@matthewdand we think this will work: diff --git a/railties/lib/rails/command.rb b/railties/lib/rails/command.rb
--- a/railties/lib/rails/command.rb
+++ b/railties/lib/rails/command.rb
@@ -65,7 +65,12 @@
full_namespace = full_namespace.to_s
namespace, command_name = split_namespace(full_namespace)
-command = find_by_namespace(namespace, command_name)
with_argv(args) do
+command = find_by_namespace(namespace, command_name)
+unless command && command.all_commands[command_name]
+require_application!
+command = find_by_namespace(namespace, command_name)
+end
+
if command && command.all_commands[command_name]
command.perform(command_name, args, config) ...now to write some tests. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still on my todo, but open if anyone else gets to it. |
In the meantime, adding this to $LOAD_PATH <<File.join(File.dirname(__dir__),'lib') Although initialization of the application is not performed before command is executed Edit: just addboot_application!to the command |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Expected behavior
I should be able to create custom Rails Thor commands for my application in a way that is analogous to adding custom Rake tasks:
Rails.root/lib/tasks/custom.task
Rails.root/lib/commands/custom_command.rb
For example, this should create the custom command
$ bin/rails custom:something
with all of the Thor niceties:This behavior already works for Rails Engines (aside: maybe the engine generator should automatically create a
lib/commands
directory in addition tolib/tasks
). Here's where those lookups happen:rails/railties/lib/rails/command.rb
Lines 163 to 169 in 139c567
These are loaded via the
$LOAD_PATH
,for a specific command:rails/railties/lib/rails/command/behavior.rb
Lines 36 to 45 in 139c567
... or all commands for
help
:rails/railties/lib/rails/command/behavior.rb
Lines 55 to 60 in 139c567
Actual behavior
The
Rails.root/lib
directory does not get added to the $LOAD_PATH before those lookups happen, so they're never loaded and the commands are not found.I think the simple solution here is to make sure that
Rails.root/lib
is added to the$LOAD_PATH
when the$ rails
command is initially run. Thatnormallyhappens when the Application is required and there's a custom command method to do that:require_application!
,but I imagine there could be reasons why that's done selectively. Hence opening an issue in case someone has better ideas.The "add lib to $LOAD_PATH" behavior was most recently solidified in#48596
System configuration
Rails version:7.1.1
The text was updated successfully, but these errors were encountered: