LoginSignup
4
2

More than 3 years have passed since last update.

RubocopがCircleCI上でのみエラーになる事象について

Posted at

事象

ローカルでは問題なくrubocopを実行できたのですが、
Githubにプッシュ、CircleCI上でrubocopを実行すると以下のようなエラーが起きました。

#!/bin/bash -eo pipefail
mkdir -p /tmp/rubocop-results
bundle exec rubocop . --out /tmp/rubocop-results/check-results.xml --format progress
/home/circleci/electronote/.rubocop.yml: Warning: no department given for AsciiComments.
404 "Not Found" while downloading remote config file http://shopify.github.io/ruby-style-guide/rubocop.yml
/usr/local/lib/ruby/2.7.0/net/http/response.rb:124:in `error!'

結論

原因

ローカルとCIで、bundle installのインストール先パスが異なることが原因でした。

ローカル:
/usr/local/bundle

CI:
{プロジェクトディレクトリ}/vendor/bundle

CIではプロジェクトディレクトリ下にインストールされるため、
rubocop実行時に、このディレクトリ内にある.rubocop.ymlを読み込んでいたものと思われます。

対策

vendorディレクトリをrubocopのチェック対象外に追加すると解決しました。

project_dir/.rubocop.yml
inherit_from: .rubocop_todo.yml

AllCops:
  Exclude:
    - 'db/**/*'
    - 'node_modules/**/*'
    - 'bin/*'
    - 'log/**/*'
    - 'public/**/*'
    - '.git/**/*'
    - 'tmp/**/*'
    - 'vendor/**/*'

解決までに試したこと

いずれも効果ありませんでしたが、以下の方法を試してみました。

(1) CircleCI設定ファイル書き換え

変更前:

circleci/config.yml
version: 2.1
orbs:
  ruby: circleci/ruby@1.1.0
  ...

jobs:
  test:
    ...
    steps:
      ...
      - ruby/rubocop-check
      ...

変更後:

circleci/config.yml
version: 2.1
orbs:
  ruby: circleci/ruby@1.1.0
  ...

jobs:
  test:
    ...
    steps:
      ...
      - run:
          name: Rubocop check
          command: bundle exec rubocop
      ...

結果:

変化なし

(2) 不要なrubocop.ymlをコメントアウト

上述のエラー文を見ると、

404 "Not Found" while downloading remote config file http://shopify.github.io/ruby-style-guide/rubocop.yml

「何でshopifyが出てくんねん!」

となったので、このURLがどこから湧いて出てきたのか調べてみました。


$ grep -r http://shopify.github.io/ruby-style-guide/rubocop.yml ./*
./vendor/bundle/ruby/2.7.0/gems/bootsnap-1.4.5/.rubocop.yml:#   - http://shopify.github.io/ruby-style-guide/rubocop.yml

どうやらbootsnap-1.4.5のディレクトリにもRubocopの設定ファイルがあり、そこで呼び出されているらしい。
というわけで、呼び出している箇所をコメントアウト。

変更前:

project_dir/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.4.5/.rubocop.yml
inherit_from:
  - http://shopify.github.io/ruby-style-guide/rubocop.yml

AllCops:
  Exclude:
    - 'vendor/**/*'
    - 'tmp/**/*'
  TargetRubyVersion: '2.2'

...

変更後:

project_dir/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.4.5/.rubocop.yml
# inherit_from:
#   - http://shopify.github.io/ruby-style-guide/rubocop.yml

AllCops:
  Exclude:
    - 'vendor/**/*'
    - 'tmp/**/*'
  TargetRubyVersion: '2.2'

...

変更してからCI環境にSSH接続し、rubocopを実行。

参考:
https://qiita.com/yu-croco/items/41d2114c94e3a6ea748a

結果:

何か違うエラーが出てきた😇

$ bundle exec rubocop
/home/circleci/electronote/.rubocop.yml: Warning: no department given for AsciiComments.
Error: The `Lint/HandleExceptions` cop has been renamed to `Lint/SuppressedException`.
(obsolete configuration found in vendor/bundle/ruby/2.7.0/gems/bootsnap-1.4.5/.rubocop.yml, please update it)

(3) vendorディレクトリをrubocopのチェック対象外に追加

上述の通り、上手く動きました。
めでたしめでたし。

参考

CircleCIでのみrubocopがエラーになる
https://qiita.com/kabi5/items/40ea864757162e931be1

4
2
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
4
2