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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)