Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single file skip parallel #10903

Merged
merged 1 commit into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions 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][])
4 changes: 4 additions & 0 deletions lib/rubocop/runner.rb
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Btw, shouldn't this be the first line of the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we make this the first line of the method then the ensure clause needs to have a separate begin block, otherwise it will replace @options with nil. I opted for this way because it looked slightly cleaner to my eyes and needed less changes.

Would you like me to rewrite it to skip the .dup?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. I didn't notice the ensure. Probably it would have been nicer to have here if/else instead of if + return, although I assume RuboCop forced you to write the code like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following passes tests and rubocop inspection:

    # Warms up the RuboCop cache by forking a suitable number of RuboCop
    # instances that each inspects its allotted group of files.
    def warm_cache(target_files)
      if target_files.length <= 1
        puts 'Skipping parallel inspection: only a single file needs inspection' if @options[:debug]
      else
        begin
          saved_options = @options.dup
          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) }
        ensure
          @options = saved_options
        end
      end
    end

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) }
Expand Down
9 changes: 8 additions & 1 deletion spec/rubocop/cli/options_spec.rb
Expand Up @@ -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')
Expand All @@ -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
Expand Down