0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RuboCopをGitHub Actionsで実行したときに発生したエラーについて

Posted at

何が起きたか

以下の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 installbundle 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に記載されておらずインストールされていないため、冒頭のエラーが発生した。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?