LoginSignup
0
0

More than 1 year has passed since last update.

【CircleCI】『Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError』が出てRuboCopが失敗する際の解決方法

Posted at

はじめに

本記事では、CI(RuboCop、RSpec)のみ動作させています。
設定ファイルの細かい説明については、分かりやすい記事がたくさんあるのでそちらにお任せします。
参考にさせていただいた記事↓

最後に私の.circleci/config.ymlの設定ファイルを参考までに記載します。

CircleCI上でRuboCopがエラーになる

CircleCIでRuboCopを動かすと次のようなエラーが発生しました。

Unable to find gem rubocop-discourse; is the gem installed? Gem::MissingSpecError
...(以下ディレクトリ/ファイル名の羅列)

rubocop-discourseが見つからないと言われています。
ローカルではrubocop-discourseがなくても問題なく動いているのになぜ..

解決方法1

言われた通りに、Gemfileに追記し、bundle installを実行。

Gemfile
gem 'rubocop-discourse'

rubocop-discourseのGitHubを確認しても、情報が少なく何をしているGemなのかはよく分かりませんでした。(discorseの意味から、rubocopとやりとりするために必要なGemなのではないかと思われます。ローカル環境とCircleCI環境ではファイル構成に違いがあるため、それが原因でエラーが起こっているんでしょうか...)

GitHubに記載のある通り.rubocop.ymlに下記を追記しました。

.rubocop.yml
inherit_gem:
  rubocop-discourse: default.yml

解決方法2

CircleCIを動かすと、今度はエラー内容が変わりました。

Error: RuboCop found unsupported Ruby version 2.4 in `TargetRubyVersion` parameter (in vendor/bundle/ruby/2.6.0/gems/msgpack-1.4.2/.rubocop.yml). 2.4-compatible analysis was dropped after version 1.12.

vendor以下の.rubocop.ymlでRubyのバージョンがサポート外だよ〜というような内容が記述されています。

.rubocop.ymlExcludeにvendor以下を追記します。

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

再度CircleCIを起動させると無事に動かすことができました!!

最後に

私の場合は解決方法1・2のどちらかでも欠けているとエラーが出てしまいました。

また、参考までに最終的なCircleCIの設定ファイルを記載しておきます。
基本的には「はじめに」で参考にさせていただいた記事に則っています。
JavaScriptを読み込むために、yarnが必要なため、その部分を追記している点が主な変更点かと思います。

yml.circleci/config.yml
version: 2.1
jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.7-node-browsers
        environment:
          - BUNDLER_VERSION: 2.2.29
          - RAILS_ENV: 'test'
      - image: circleci/mysql:8.0
        environment:
          - MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            - v1-dependencies-
      - run:
          name: install dependencies
          command: |
            gem install bundler -v 2.2.29
            bundle install --jobs=4 --retry=3 --path vendor/bundle
      - run: yarn install #ここを追記
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      - run: mv config/database.yml.ci config/database.yml 
      - run: bundle exec rails db:create
      - run: bundle exec rails db:schema:load
      - run:
          name: Rubocop
          command: bundle exec rubocop
      - run:
          name: run tests
          command: |
            mkdir /tmp/test-results
            TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
              circleci tests split --split-by=timings)"
            bundle exec rspec \
              --format progress \
              --format RspecJunitFormatter \
              --out /tmp/test-results/rspec.xml \
              --format progress \
              $TEST_FILES
      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results
0
0
1

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