何が起きたか
以下の2つのファイルを用意してGitHub ActionsでRuboCopを実行した。
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
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の対象から外す。
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が読み込まれる。
inherit_gem:
rubocop-discourse: default.yml
inherit_mode:
merge:
- Exclude
inherit_gemにより外部のGemに含まれる.rubocop.ymlの設定が引き継がれるようになる。しかしrubocop-discourse GemはGemfile.lockに記載されておらずインストールされていないため、冒頭のエラーが発生した。
参考