Read more

Fast rubocop autocorrection alias

Michael Leimstädtner
June 19, 2023Software engineer at makandra GmbH

Parallel autocorrection is now possible and the default since Rubocop 1.32

https://github.com/rubocop/rubocop/releases/tag/v1.32.0

The rubocop binary has a few interesting flags:

  • rubocop (using the --parallel default ) scans the current repository for linting issues while using multiple CPU cores
  • rubocop -a (or --autocorrect) safely corrects most offenses while doing a sequential scan
  • rubocop -A (or --autocorrect-all) also tries to correct unsafe suggestions
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Autocorrection takes significantly longer on large projects because of the sequential nature.
To speed things up, you can use the following alias. It first checks in parallel if any files need to be corrected, then passes them to the autocorrection command:

alias fixcops="bundle exec rubocop -p | grep -P \"\A[^:]+(?=:\d+:\d+: )\" --only-matching | xargs --no-run-if-empty bundle exec rubocop -a"
  • bundle exec rubocop -p quickly scans for issues, listing each affected file
  • grep -P \"\A[^:]+(?=:\d+:\d+: )\" --only-matching searches for those file names
  • xargs --no-run-if-empty bundle exec rubocop -a only runs autocorrection on selected files if there are any
Posted by Michael Leimstädtner to makandra dev (2023-06-19 10:49)