セットプチフォッカ

勉強したアウトプット、ときどきフォッカチオ作っていました

Rubocop実行時のparserに関する警告を解消する

環境

  • macOS:BigSur
  • RubyMine:2020.3
  • Ruby:2.7.1
  • Rubocop:1.4.2
  • parser:2.7.2.0

Rubocopからの警告

Rubocop実行時にいつの間にか、以下の警告が出るようになっていました。

warning: parser/current is loading parser/ruby27, which recognizes
warning: 2.7.2-compliant syntax, but you are running 2.7.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.

please seeのURLに飛んで、いろいろ調べてみたところ

  • RubocopはRuby構文解析whitequark/parser: A Ruby parser.を使用する
  • Rubyはパッチリリースごとに構文解析へのバックポート*1を盛り込むことがある。構文解析を担うparserはこれに対応する必要がある。
  • 上記に対応するため、parserは1つのバージョンで複数のマイナーバージョンをサポートできないそう(parser2.7.2.0では、Ruby2.7.2のみをサポートしていて、2.7.1をサポートしていない)
  • そのため、Rubyのバージョン(2.7.1)とparserの対応しているバージョン(2.7.2)が異なると警告が出る。

今回事象が発生している環境では特にRubocopのバージョンもしていなかったので、parserも最新版が入っているみたいでした。

# Gemfile.lock
parser (2.7.2.0)

参考記事

parserのバージョンを下げる

Rubyに合わせてparserを2.7.2.0未満に下げることでエラーを解消します。

1.Gemfileにparserのバージョンを追記する

# 2.7.2.0未満の最新版をインストールする
gem 'parser', '< 2.7.2.0'

2.parserをupdateする Gemfile.lockで既にparserの2.7.2.0が指定されているので、updateしていく。

% bundle update parser

3.Rubocopを実行する

% bundle exec rubocop
Inspecting 20 files
....................

20 files inspected, no offenses detected

見事エラー解消!

実は(今回は)無視してもいい警告

* Bump 2.7 branch to 2.7.2 by koic · Pull Request #748 · whitequark/parser

Ruby 2.7.2 has been released. https://www.ruby-lang.org/en/news/2020/10/02/ruby-2-7-2-released/
It seems like no changes have been made to ruby27.y where Parser gem should be changed (backported). ruby/ruby@v2_7_1...v2_7_2

特にRuby2.7.1と2.7.2の間に変化はないっぽいので、今回はこの警告を無視してもRubocopの実行結果は変わらなそうでした。

でも、ドキュメント読んだりエラー解消したり、いろいろと勉強になったのでよしとしましょう!

以下、調査過程のメモ

原文

whitequark/parser: A Ruby parser.

Unfortunately, Ruby MRI often changes syntax in patchlevel versions. This has happened, at least, for every release since 1.9; for example, commits c5013452 and 04bb9d6b were backported all the way from HEAD to 1.9. Moreover, there is no simple way to track these changes.

This policy makes it all but impossible to make Parser precisely compatible with the Ruby MRI parser. Indeed, at September 2014, it would be necessary to maintain and update ten different parsers together with their lexer quirks in order to be able to emulate any given released Ruby MRI version.

As a result, Parser chooses a different path: the parser/rubyXY parsers recognize the syntax of the latest minor version of Ruby MRI X.Y at the time of the gem release.

単語・語句・構文

  • Ruby MRI:CRuby(Matz' Ruby Implementation)
  • backport:あるバージョンのソフトウェアに追加した機能やセキュリティ対策などの改良を、それより以前のバージョンのソフトウェアにも取り込むこと。
  • c501345204bb9d6b:いずれもparse.yにおけるバックポートの例。
  • lexer quirks:lexerが字句解析。字句解析の特徴といったところ?

内容に関する知識

疑問点と思考過程

訳文(deepLと少し自分)

残念なことに、Ruby MRI はパッチレベルのバージョンで構文を変更することがよくあります。例えば、コミット c5013452 や 04bb9d6b は HEAD から 1.9 までバックポートされています。さらに、これらの変更を追跡する簡単な方法はありません。

このポリシーにより、ParserをRubyのMRIパーサーと正確に互換性を持たせることは不可能になっています。実際、2014年9月時点では、リリースされたRuby MRIバージョンをエミュレートするためには、10種類の異なるパーサを、それらのlexerの癖と一緒に維持し、更新する必要があります。

その結果、Parserは別の道を選択します: parser/rubyXYパーサーはgemのリリース時に最新のマイナーバージョンのRuby MRI X.Yの構文を認識します。

*1:あるバージョンのソフトウェアに追加した機能やセキュリティ対策などの改良を、それより以前のバージョンのソフトウェアにも取り込むこと。