diff --git a/changelog/change_disable_parallel_with_cache_false.md b/changelog/change_disable_parallel_with_cache_false.md new file mode 100644 index 00000000000..25218647092 --- /dev/null +++ b/changelog/change_disable_parallel_with_cache_false.md @@ -0,0 +1 @@ +* [#9744](https://github.com/rubocop/rubocop/pull/9744): The parallel flag will now be automatically ignored when combined with `--cache false`. Previously, an error was raised and execution stopped. ([@rrosenblum][]) diff --git a/lib/rubocop/options.rb b/lib/rubocop/options.rb index 33866a767dc..3604944c188 100644 --- a/lib/rubocop/options.rb +++ b/lib/rubocop/options.rb @@ -297,7 +297,7 @@ def validate_compatibility # rubocop:disable Metrics/MethodLength validate_auto_gen_config validate_auto_correct validate_display_only_failed - validate_parallel + disable_parallel_when_invalid_option_combo return if incompatible_options.size <= 1 @@ -334,33 +334,27 @@ def validate_auto_correct format('--disable-uncorrectable can only be used together with --auto-correct.') end - def validate_parallel + def disable_parallel_when_invalid_option_combo return unless @options.key?(:parallel) - if @options[:cache] == 'false' - raise OptionArgumentError, '-P/--parallel uses caching to speed up ' \ - 'execution, so combining with --cache ' \ - 'false is not allowed.' - end - - disable_parallel_when_invalid_combo - end + invalid_options = [ + { name: :auto_gen_config, value: true, flag: '--auto-gen-config' }, + { name: :fail_fast, value: true, flag: '-F/--fail-fast.' }, + { name: :auto_correct, value: true, flag: '--auto-correct.' }, + { name: :cache, value: 'false', flag: '--cache false' } + ] - def disable_parallel_when_invalid_combo - combos = { - auto_gen_config: '--auto-gen-config', - fail_fast: '-F/--fail-fast.', - auto_correct: '--auto-correct.' - } - - invalid_combos = combos.select { |key, _flag| @options.key?(key) } + invalid_flags = invalid_options.each_with_object([]) do |option, flags| + # `>` rather than `>=` because `@options` will also contain `parallel: true` + flags << option[:flag] if @options > { option[:name] => option[:value] } + end - return if invalid_combos.empty? + return if invalid_flags.empty? @options.delete(:parallel) puts '-P/--parallel is being ignored because ' \ - "it is not compatible with #{invalid_combos.values.join(', ')}" + "it is not compatible with #{invalid_flags.join(', ')}" end def only_includes_redundant_disable? diff --git a/spec/rubocop/options_spec.rb b/spec/rubocop/options_spec.rb index cbbcfd53ea8..4370660837b 100644 --- a/spec/rubocop/options_spec.rb +++ b/spec/rubocop/options_spec.rb @@ -199,11 +199,11 @@ def abs(path) describe '--parallel' do context 'combined with --cache false' do - it 'fails with an error message' do - msg = ['-P/--parallel uses caching to speed up execution, so ', - 'combining with --cache false is not allowed.'].join - expect { options.parse %w[--parallel --cache false] } - .to raise_error(RuboCop::OptionArgumentError, msg) + it 'ignores parallel' do + msg = '-P/--parallel is being ignored because it is not compatible with --cache false' + options.parse %w[--parallel --cache false] + expect($stdout.string).to include(msg) + expect(options.instance_variable_get('@options').keys).not_to include(:parallel) end end