LoginSignup
19
6

More than 5 years have passed since last update.

RuboCopをアップデートしたら対象ファイル数が2つだけになった話

Last updated at Posted at 2018-05-22

概要

公式での記載の通り、RuboCopの'0.56.0'版が2018/05/14にリリースされました。
今まで'0.55.0'版を使用しており、'0.56.0'版をインストールしたところ対象ファイルが14→2に激減したので、原因を調査しました。

目的

本投稿では'0.55.0'と'0.56.0'の両者を同じ状況下で動きが異なることを示し、それに対して筆者の理解範囲内での原因解説を目的としております。

目次

1. 現象
2. 使用した.rubocop.ymlの提示
3. RuboCop'0.55.0'版使用時のコンソール上での実行結果
4. Rubocop'0.56.0'版使用時のコンソール上での実行結果
5. 原因
6. 結論

現象

RuboCopの'0.55.0'版では以下のとおり13ファイルがrubocopの対象となっています。

$ rubocop
Inspecting 13 files
.............

13 files inspected, no offenses detected
——
$ rubocop -v
0.55.0
——

これがRuboCopの'0.56.0'版では2ファイルになってしまったので調査しました。

$ rubocop
Inspecting 2 files
..

2 files inspected, no offenses detected
——
$ rubocop -v
0.56.0
——

使用した.rubocop.yml

以下が私が使用した.rubocop.ymlの内容です。これに関しては今までのマニュアルに沿った書き方なので、この書き方で書いている方は多いはず。(今回の記事に関連する部分のみを抽出し、その他の部分に関しては省略しております。)

.rubocop.yml
AllCops:
  TargetRubyVersion: 2.4.4
  Include:
    - '**/Rakefile'
    - '**/config.ru'
  Exclude:
    - 'db/**/*'
    - 'config/**/*'
    - 'script/**/*'
    - 'node_modules/**/*'
    - 'test/**/*'
    - 'spec/**/*'
    - 'client/**/*'
    - 'bin/**/*'
    - 'vendor/**/*'
    - !ruby/regexp /old_and_unused\.rb$/

RuboCop'0.55.0'版使用時のコンソール上での実行結果

console
Inspecting 14 files
..............

14 files inspected, no offenses detected

.rubocop.ymlのinclude内では2つのファイル種類しか明示されていないにも関わらず、14件のファイルがrubocopによって対象ファイルとみなされていることが見て取れます。

RuboCop'0.56.0'版使用時のコンソール上での実行結果

Inspecting 2 files
..

2 files inspected, no offenses detected

'0.56.0'版では、include内に明示した2つのファイル種類しか読み込まれず、2件のファイルのみ対象とされていることがわかります。

原因

  • '0.55.0'を使用した際に14件のファイルが対象とされた件に関して

こちらは、'0.55.0'までの仕様で、inculde内で明示されいない場合にも、以下のrubocop/config/default.yml内の要素はデフォルトで読み込まれるようになっているからです。

rubocop/config/default.yml

 AllCops: 
:--
   RubyInterpreters: 
     - ruby 
     - macruby 
     - rake 
     - jruby
     - rbx 
   # Include common Ruby source files. 
   Include: 
     - '**/*.rb' 
     - '**/*.arb' 
     - '**/*.axlsx' 
     - '**/*.builder' 
     - '**/*.fcgi' 
     - '**/*.gemfile' 
     - '**/*.gemspec' 
     - '**/*.god' 
     - '**/*.jb' 
     - '**/*.jbuilder' 
     - '**/*.mspec' 
     - '**/*.opal' 
     - '**/*.pluginspec' 
     - '**/*.podspec' 
     - '**/*.rabl' 
     - '**/*.rake' 
     - '**/*.rbuild' 
     - '**/*.rbw' 
     - '**/*.rbx' 
     - '**/*.ru' 
     - '**/*.ruby' 
     - '**/*.spec' 
     - '**/*.thor' 
     - '**/*.watchr' 
     - '**/.irbrc' 
     - '**/.pryrc' 
     - '**/buildfile' 
     - '**/config.ru' 
     - '**/Appraisals' 
     - '**/Berksfile' 
     - '**/Brewfile' 
     - '**/Buildfile' 
     - '**/Capfile' 
     - '**/Cheffile'
     - '**/Dangerfile' 
     - '**/Deliverfile' 
     - '**/Fastfile' 
     - '**/*Fastfile' 
     - '**/Gemfile' 
     - '**/Guardfile' 
     - '**/Jarfile' 
     - '**/Mavenfile' 
     - '**/Podfile' 
     - '**/Puppetfile' 
     - '**/Rakefile' 
     - '**/Snapfile'
     - '**/Thorfile' 
     - '**/Vagabondfile' 
     - '**/Vagrantfile' 
  • 0.56.0'版で2件のファイルのみ対象とされた件に関して

'0.56.0'においては.rubocomp.ymlにincludeを明示した際にはその内容がrubocop/coonfig/default.ymlの内容をオーバーライトする仕様に変更されたようです。その結果、明示したファイル種類しか対象とされませんでした。

結論

以下を本投稿の結論とします。

  • rubocop'0.55.0'での対象ファイル
    .rubocop.yml + rubocop/config/default.yml

  • rubocop'0.56.0'(.rubocop.ymlにてincludeを明示した場合)
    .rubocop.yml

  • rubocop'0.56.0'(rubocop.ymlにてincludeを明示しない場合)
    rubocop/config/default.yml

なので、rubocop.ymlの記載が以下のままの場合、0.56にアップデートされたら今回の現象が再現されますので、以下を削除してください。(マニュアルからも削除されています

  Include:
    - '**/Rakefile'
    - '**/config.ru'

なお、今回の修正のもともとの目的は

Remove hard-coded file patterns and use only Include, Exclude and the new RubyInterpreters parameters for file selection.(RuboCop Changelogより抜粋)

対象ファイルの選択時には今までハードコーディングしていたものを全て取り除いて、設定ファイルのinclude,excludeで指定したものだけ対象とする仕様変更のようです。


本投稿はGitHub上のRuboCopを参考にしております。詳細は以下のChangelogよりご確認ください。
RuboCop Changelog: https://github.com/bbatsov/rubocop/blob/master/CHANGELOG.md

19
6
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
19
6