何が起きたか
以下の2つのファイルを用意してGitHub ActionsでRuboCopを実行した。
.github/workflows/rubocop.yml
name: RuboCop
on: push
jobs:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true
- run: bundle exec rubocop
.rubocop.yml
require:
- rubocop-factory_bot
- rubocop-performance
- rubocop-rails
- rubocop-rspec
- rubocop-rspec_rails
AllCops:
Exclude:
- 'bin/**/*'
- 'db/migrate/*'
- 'db/schema.rb'
NewCops: enable
RSpec/NestedGroups:
Max: 4
Style/Documentation:
Enabled: false
すると次のようなエラーが発生して、RuboCopを実行することができなかった。
Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError
解決法
vendor/bundle配下をRuboCopの対象から外す。
.rubocop.yml
require:
- rubocop-factory_bot
- rubocop-performance
- rubocop-rails
- rubocop-rspec
- rubocop-rspec_rails
AllCops:
Exclude:
- 'bin/**/*'
- 'db/migrate/*'
- 'db/schema.rb'
+ - 'vendor/bundle/**/*'
NewCops: enable
RSpec/NestedGroups:
Max: 4
Style/Documentation:
Enabled: false
解説
ruby/setup-rubyを使うとbundle install
する前にbundle config --local deployment true
が実行される。
するとbundle install
はbundle install --deployment
と同じ動作をする。このときGemはvendor/bundle配下にインストールされる。
このリポジトリではRuby on Railsを入れていて、その依存関係としてGemfile.lockに記載されていたmini_mimeというGemもインストールされた。するとRuboCop実行時にこのGemに含まれている.rubocop.ymlが読み込まれる。
.rubocop.yml
inherit_gem:
rubocop-discourse: default.yml
inherit_mode:
merge:
- Exclude
inherit_gem
により外部のGemに含まれる.rubocop.ymlの設定が引き継がれるようになる。しかしrubocop-discourse GemはGemfile.lockに記載されておらずインストールされていないため、冒頭のエラーが発生した。
参考