diff --git a/changelog/new_add_option_to_raise_cop_errors.md b/changelog/new_add_option_to_raise_cop_errors.md new file mode 100644 index 00000000000..4f71809187e --- /dev/null +++ b/changelog/new_add_option_to_raise_cop_errors.md @@ -0,0 +1 @@ +* [#11001](https://github.com/rubocop/rubocop/pull/11001): Add option to raise cop errors `--raise-cop-error`. ([@wildmaples][]) diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index cbcc9add760..c0aed7653fd 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -10,6 +10,9 @@ enforce different coding conventions. You can also load xref:extensions.adoc#custom-cops[custom cops]. +Cop-related failures are silenced by default but can be turned on using the +`--raise-cop-errors` option. + == Style Style cops check for stylistic consistency of your code. Many of the them are diff --git a/docs/modules/ROOT/pages/usage/basic_usage.adoc b/docs/modules/ROOT/pages/usage/basic_usage.adoc index ad198638ae6..1cae5d09664 100644 --- a/docs/modules/ROOT/pages/usage/basic_usage.adoc +++ b/docs/modules/ROOT/pages/usage/basic_usage.adoc @@ -240,6 +240,9 @@ $ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName | `--[no-]parallel` | Use available CPUs to execute inspection in parallel. Default is parallel. +| `--raise-cop-error` +| Raise cop-related errors with cause and location. This is used to prevent cops from failing silently. Default is false. + | `-r/--require` | Require Ruby file (see xref:extensions.adoc#loading-extensions[Loading Extensions]). diff --git a/lib/rubocop/cop/commissioner.rb b/lib/rubocop/cop/commissioner.rb index 87fbf5bea12..50d5018d978 100644 --- a/lib/rubocop/cop/commissioner.rb +++ b/lib/rubocop/cop/commissioner.rb @@ -159,9 +159,11 @@ def invoke(callback, cops, *args) def with_cop_error_handling(cop, node = nil) yield rescue StandardError => e - raise e if @options[:raise_error] + raise e if @options[:raise_error] # For internal testing err = ErrorWithAnalyzedFileLocation.new(cause: e, node: node, cop: cop) + raise err if @options[:raise_cop_error] # From user-input option + @errors << err end end diff --git a/lib/rubocop/options.rb b/lib/rubocop/options.rb index 48afbb016f4..32ac07e5422 100644 --- a/lib/rubocop/options.rb +++ b/lib/rubocop/options.rb @@ -65,7 +65,7 @@ def define_options end def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength - section(opts, 'Basic Options') do + section(opts, 'Basic Options') do # rubocop:disable Metrics/BlockLength option(opts, '-l', '--lint') do @options[:only] ||= [] @options[:only] << 'Lint' @@ -90,6 +90,7 @@ def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLen option(opts, '--force-default-config') option(opts, '-s', '--stdin FILE') option(opts, '-P', '--[no-]parallel') + option(opts, '--raise-cop-error') add_severity_option(opts) end end @@ -589,7 +590,10 @@ module OptionsHelp restart_server: 'Restart server process.', start_server: 'Start server process.', stop_server: 'Stop server process.', - server_status: 'Show server status.' + server_status: 'Show server status.', + raise_cop_error: ['Raise cop-related errors with cause and location.', + 'This is used to prevent cops from failing silently.', + 'Default is false.'] }.freeze end end diff --git a/spec/rubocop/cop/commissioner_spec.rb b/spec/rubocop/cop/commissioner_spec.rb index 78ffa5f4249..66d8668d868 100644 --- a/spec/rubocop/cop/commissioner_spec.rb +++ b/spec/rubocop/cop/commissioner_spec.rb @@ -114,6 +114,16 @@ def method end end + context 'when passed :raise_cop_error option' do + let(:options) { { raise_cop_error: true } } + + it 're-raises the exception received while processing' do + allow(cop).to receive(:on_int) { raise RuboCop::ErrorWithAnalyzedFileLocation } + + expect { offenses }.to raise_error(RuboCop::ErrorWithAnalyzedFileLocation) + end + end + context 'when given a force' do let(:force) { instance_double(RuboCop::Cop::Force).as_null_object } let(:forces) { [force] } diff --git a/spec/rubocop/options_spec.rb b/spec/rubocop/options_spec.rb index 6cb1c5f9f50..9556c8ae821 100644 --- a/spec/rubocop/options_spec.rb +++ b/spec/rubocop/options_spec.rb @@ -66,6 +66,9 @@ def abs(path) reports. This is useful for editor integration. -P, --[no-]parallel Use available CPUs to execute inspection in parallel. Default is true. + --raise-cop-error Raise cop-related errors with cause and location. + This is used to prevent cops from failing silently. + Default is false. --fail-level SEVERITY Minimum severity for exit with error code. [A] autocorrect [I] info @@ -375,6 +378,13 @@ def abs(path) end end + describe '--raise-cop-error' do + it 'raises cop errors' do + results = options.parse %w[--raise-cop-error] + expect(results).to eq([{ raise_cop_error: true }, []]) + end + end + describe '--require' do let(:required_file_path) { './path/to/required_file.rb' }