Skip to content

Conversation

@adamstegman
Copy link
Contributor

@adamstegman adamstegman commented Oct 9, 2025

abort is very similar to exit in Ruby (docs), but it takes a string message or Exception. Rails applications should avoid its usage for the same reasons as they avoid exit and exit!. Since they are used in the same way, I thought it belonged in the same cop, but am glad to move it to a new one if that's preferred.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@adamstegman adamstegman force-pushed the rails-exit-abort branch 2 times, most recently from f49fff4 to 5690b95 Compare October 9, 2025 19:18
@adamstegman adamstegman marked this pull request as ready for review October 9, 2025 19:26

MSG = 'Do not use `exit` in Rails applications.'
RESTRICT_ON_SEND = %i[exit exit!].freeze
MSG = 'Do not use `exit` or `abort` in Rails applications.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to limit it to methods that are actually used, for example as shown below.
The test code should also be updated accordingly to reflect this change.

diff --git a/lib/rubocop/cop/rails/exit.rb b/lib/rubocop/cop/rails/exit.rb
index fd1901970..5517f1454 100644
--- a/lib/rubocop/cop/rails/exit.rb
+++ b/lib/rubocop/cop/rails/exit.rb
@@ -26,12 +26,16 @@ module RuboCop
       class Exit < Base
         include ConfigurableEnforcedStyle
 
-        MSG = 'Do not use `exit` or `abort` in Rails applications.'
+        MSG = 'Do not use `%<current>s` in Rails applications.'
         RESTRICT_ON_SEND = %i[exit exit! abort].freeze
         EXPLICIT_RECEIVERS = %i[Kernel Process].freeze
 
         def on_send(node)
-          add_offense(node.loc.selector) if offending_node?(node)
+          return unless offending_node?(node)
+
+          message = format(MSG, current: node.method_name)
+
+          add_offense(node.loc.selector, message: message)
         end
 
         private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pointer to how to accomplish this! I've updated the PR with this suggestion.

`abort` is very similar to `exit` in Ruby
([docs](https://docs.ruby-lang.org/en/3.4/Kernel.html#method-i-abort)),
but it takes a string message or Exception. Rails applications should
avoid its usage for the same reasons as they avoid `exit` and `exit!`.
Since they are used in the same way, I thought it belonged in the same
cop, but am glad to move it to a new one if that's preferred.
@koic koic merged commit 9e925de into rubocop:master Oct 14, 2025
15 checks passed
@koic
Copy link
Member

koic commented Oct 14, 2025

Thanks!

@adamstegman adamstegman deleted the rails-exit-abort branch October 14, 2025 14:00
@mjankowski
Copy link
Contributor

Curious about this rule RE: a scenario like "we need certain ENV vars to be present and set a certain way to function properly". Using abort somewhere in config (probably config/initializers specifically) to exit app boot, display a message to user/admin, and return non-zero exit code seems sort of useful -- though I understand and agree with the spirit of this cop in terms of generally wanting to avoid abort/exit and prefer inline raise or fail/break/etc in app code.

Curious what the recommendation would be for that "detecting stuff during app boot". Should this cop ignore config or config/initializers? Should those cases just do inline disable (or disable in the app config)?

@adamstegman
Copy link
Contributor Author

@mjankowski I think raise would accomplish the same thing, but I understand it's messier with the stacktrace. I think if you prefer the experience of abort then excluding specific files where you're using it makes sense. You may not want to exclude all initializers since they might be e.g. creating callbacks that shouldn't use exit or abort.

@mjankowski
Copy link
Contributor

Yeah - I think for developer-facing environment/config issues doing a swap over to raise is totally fine for reasons here.

For packaged/distributed apps where you potentially have admins/operators (who are not devs and may not be sure what to do with a backtrace, but who DO understand exit codes and console warnings), it's less ideal. Maybe that's an edge case and the disable is fine.

Agreed on point re: inline vs full directory to not let any sneak through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants