From 7f1e0159370803ac7f49da9829faf67626524128 Mon Sep 17 00:00:00 2001 From: Wander Hillen Date: Wed, 10 Aug 2022 15:43:14 +0200 Subject: [PATCH] Do not fork off extra processes if only a single file needs inspection --- changelog/fix_speed_up_single_file_inspection.md | 1 + lib/rubocop/runner.rb | 4 ++++ spec/rubocop/cli/options_spec.rb | 9 ++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_speed_up_single_file_inspection.md diff --git a/changelog/fix_speed_up_single_file_inspection.md b/changelog/fix_speed_up_single_file_inspection.md new file mode 100644 index 00000000000..4a8f2847f96 --- /dev/null +++ b/changelog/fix_speed_up_single_file_inspection.md @@ -0,0 +1 @@ +* [#10903](https://github.com/rubocop/rubocop/pull/10903): Skip forking off extra processes for parallel inspection when only a single file needs to be inspected. ([@wjwh][]) diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index f123cff426e..9e3cce93bbf 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -64,6 +64,10 @@ def aborting? # instances that each inspects its allotted group of files. def warm_cache(target_files) saved_options = @options.dup + if target_files.length <= 1 + puts 'Skipping parallel inspection: only a single file needs inspection' if @options[:debug] + return + end puts 'Running parallel inspection' if @options[:debug] %i[autocorrect safe_autocorrect].each { |opt| @options[opt] = false } Parallel.each(target_files) { |target_file| file_offenses(target_file) } diff --git a/spec/rubocop/cli/options_spec.rb b/spec/rubocop/cli/options_spec.rb index 20cdb21125a..6f7dd08e686 100644 --- a/spec/rubocop/cli/options_spec.rb +++ b/spec/rubocop/cli/options_spec.rb @@ -14,6 +14,11 @@ describe '--parallel' do if RuboCop::Platform.windows? context 'on Windows' do + before do + create_file('test_1.rb', ['puts "hello world"']) + create_file('test_2.rb', ['puts "what a lovely day"']) + end + it 'prints a warning' do cli.run ['-P'] expect($stderr.string).to include('Process.fork is not supported by this Ruby') @@ -35,7 +40,9 @@ context 'on Unix-like systems' do it 'prints a message if --debug is specified' do cli.run ['--parallel', '--debug'] - expect($stdout.string).to match(/Running parallel inspection/) + expect($stdout.string).to match( + /Skipping parallel inspection: only a single file needs inspection/ + ) end it 'does not print a message if --debug is not specified' do