LoginSignup
3
0

More than 1 year has passed since last update.

【CircleCI】"Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError"が出た時の対処法

Posted at

はじめに

CircleCI実行時にRubocopを実行させようとした時に、発生したエラーとその対処方法について、メモしておきます。

実行環境

ruby 3.0.2
rails 6.1.4
rubocop 1.24.1
circleci version 2.1

発生したエラー

.config.ymlを以下のように記述し、CIを実行しようとしたところ、rubocopを実行する部分でエラーが発生しました。

config.yml
version: 2.1

orbs:
  ruby: circleci/ruby@1.4.0

jobs:
  build:
    docker:
      - image: cimg/ruby:3.0.2
    working_directory: ~/appname_backend
    steps:
      - checkout:
          path: ~/appname_backend
      - ruby/install-deps

  test:
    docker:
      - image: cimg/ruby:3.0.2
      - image: cimg/mysql:8.0.27
        environment:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: appname_test
          MYSQL_USER: user
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      APP_DATABASE_HOST: "127.0.0.1"
      RAILS_ENV: test
    working_directory: ~/appname_backend
    steps:
      - checkout:
          path: ~/appname_backend
      - ruby/install-deps
      - run:
          name: Wait for DB
          command: sleep 30
      - run:
          name: Database setup
          command: bundle exec rails db:create db:migrate
      - run:
          name: rubocop
          command: bundle exec rubocop
      - run:
          name: test
          command: bundle exec rspec

workflows:
  version: 2
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build

発生したエラー
Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError
/home/circleci/appname_backend/vendor/bundle/ruby/3.0.0/gems/rubocop-1.24.1/lib/rubocop/config_loader_resolver.rb:278:in `rescue in gem_config_path'
/home/circleci/appname_backend/vendor/bundle/ruby/3.0.0/gems/rubocop-1.24.1/lib/rubocop/config_loader_resolver.rb:268:in `gem_config_path'
/home/circleci/appname_backend/vendor/bundle/ruby/3.0.0/gems/rubocop-1.24.1/lib/rubocop/config_loader_resolver.rb:69:in `block (2 levels) in resolve_inheritance_from_gems'
/home/circleci/appname_backend/vendor/bundle/ruby/3.0.0/gems/rubocop-1.24.1/lib/rubocop/config_loader_resolver.rb:67:in `reverse_each'
・
・
・
/home/circleci/.rubygems/bin/bundle:23:in `load'
/home/circleci/.rubygems/bin/bundle:23:in `<main>'

Exited with code exit status 2

解決方法

.rubocop.ymlに以下を追記する

rubocop.yml
AllCops:
  Exclude:
    - 'vendor/**/*'

なぜこの記述がないとエラーになるのか

CircleCIでは、bundle installしたgemsをvendor/bundle配下にロードしているようです。

  test:
    steps:
      - checkout:
          path: ~/journey_backend
      - ruby/install-deps
      ↑ このコマンド中にbundle installを実行している

image.png

このため、rubocopがvendor配下のgemsたちまでチェックしてしていたようです。
そこで、.rubocop.ymlにvendorディレクトリ以下をrubocopの対象から外す記述をすればエラーがなくなるというわけですね。

以上、どなたかの参考になれば幸いです。

参考

https://qiita.com/kabi5/items/40ea864757162e931be1

3
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
3
0