ruby-on-railsrubyrubocop

Rubocop autocorrect for FrozenStringLiteralComment not working


I have a Rails 6 project and am trying to use RuboCop's auto-correct option to fix the many Style/FrozenStringLiteralComment errors (mostly in generated files).

No matter how many times I run bundle exec rubocop --auto-correct --only Style/FrozenStringLiteralComment from the project root, Rubocop will only report the errors, but not auto-correct them:

app/models/product.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.

From running with the --show-cops option I can see that this cop 'Supports --auto-correct,' so am not sure what's wrong.

My .rubocop.yml file has only one config setting: require: rubocop-rails.

I've also run bundle exec rubocop -a and it has corrected other errors in config and similar files (single-quoting, mostly).

Any idea why auto-correct won't work in this case? Thanks.


Solution

  • This is a breaking change introduced in the version 0.87 (see the issue).

    Quoting from there:

    rubocop -a does all autocorrections, including unsafe ones. One has to add --safe-auto-correct to exclude unsafe ones.

    You should use the -A flag now to safe and/or unsafe autocorrect your files:

    rubocop -a / --autocorrect no longer run unsafe corrections; rubocop -A / --autocorrect-all run both safe and unsafe corrections. Options --safe-autocorrect is deprecated

    It should work for you as:

    bundle exec rubocop -A --only Style/FrozenStringLiteralComment