Search code examples
ruby-on-railsgit-commitpre-commit-hookrubocopruby-overcommit

Rails: "overcommit --run" is passing but pre-commit hooks fail


I am experiencing an issue with pre-commit hooks in overcommit, which is configured to run rubocop and rails_best_practices.

In short, all three commands listed below passed but overcommit wouldn't let me commit to git. Any advice on why/how to circumvent this issue would be appreciated.

# These passed
rubocop -a
rails_best_practices .
overcommit --run
# Git commit failed
$ overcommit --run
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] OK
Analyze with RuboCop........................................[RuboCop] OK

✓ All pre-commit hooks passed


$ git commit -m 'Ensure lower case with Attr API'
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:2 - remove unused methods (LowerCaseString#cast)

Analyze with RuboCop........................................[RuboCop] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:3:3: C: Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.

✗ One or more pre-commit hooks failed

Now, I read here that overcommit --run doesn't trigger the hooks in the same ways that it does when you actually try to commit (whole project vs only changes to be committed). However, I'm not sure how this would affect my case, especially since rubocop and rails_best_practices both passed individually as well. By the way, the errors raised here are both false alarms. rails_best_practices shouldn't be checking for unused methods in the first place, since that check has been turned off in config/rails_best_practices.yml.


Solution

  • After a few weeks of working with these gems, I think I've figured it out. I'll leave some pointers here for anyone who encounters similar issues.

    1. Stage your changes before doing overcommit --run with git add <file>

    As this comment and docs say, the --run command does not check untracked files.

    2. Add gemfile option to .overcommit.yml

    One issue I had was that overcommit was failing to read my config/rails_best_practices.yml (it wasn't supposed to be checking for unused methods in the first place, since that check was turned off in the config file).

    It seems that when I was trying to git commit, overcommit was using gems installed in my system (not in the Gemfile), and as a result somehow failing to read my config file. So I added the gemfile option as recommended in the docs, and made sure overcommit uses the Gemfile/bundler version. I haven't gotten the subject error ever since. As the docs say:

    If you are using Bundler to manage your Ruby gem dependencies, you'll likely want to use the gemfile option to control which gem versions are available during your hook runs.

    My .overcommit.yml file for reference:

    gemfile: Gemfile
    
    PreCommit:
     RuboCop:
       enabled: true
       on_warn: fail
    
     RailsBestPractices:
       enabled: true
       on_warn: fail
       command: ['bundle', 'exec', 'rails_best_practices', '-c', 'config/rails_best_practices.yml']
    

    As a side note - I haven't tried this, but apparently you can also create a separate Gemfile just for overcommit purposes, if you feel that loading your original Gemfile slows down the hook execution (docs).