There's an echo in my head

日々のメモ。

ERROR While executing gem - wrong number of arguments (given 1, expected 0)

ちょっと古いrubygemsで発生する下記のエラーが、普通にコマンドを叩くだけでは回避できなかった。

ERROR While executing gem - wrong number of arguments (given 1, expected 0) · Issue #2224 · rubygems/rubygems · GitHub

$ gem install bundler -v 1.16.2 --debug
Exception `LoadError' at /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/2.5.0/resolv.rb:170 - LoadError
Exception `Errno::EEXIST' at /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/2.5.0/fileutils.rb:232 - File exists @ dir_s_mkdir - /Users/foobar/.gem/specs/api.rubygems.org%443
Exception `ArgumentError' at /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:46 - wrong number of arguments (given 1, expected 0)
ERROR:  While executing gem ... (ArgumentError)
    wrong number of arguments (given 1, expected 0)
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:46:in `require'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/installer.rb:108:in `initialize'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/request_set.rb:138:in `new'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/request_set.rb:138:in `block in install'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/request_set.rb:126:in `each'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/request_set.rb:126:in `install'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/dependency_installer.rb:389:in `install'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/commands/install_command.rb:219:in `install_gem'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/commands/install_command.rb:263:in `block in install_gems'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/commands/install_command.rb:259:in `each'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/commands/install_command.rb:259:in `install_gems'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/commands/install_command.rb:171:in `execute'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/command.rb:305:in `invoke_with_build_args'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/command_manager.rb:167:in `process_args'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/command_manager.rb:137:in `run'
    /Users/foobar/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/gem_runner.rb:54:in `run'
    /Users/foobar/.rbenv/versions/2.5.3/bin/gem:21:in `<main>'

どうやら Kernel からincludeした gem メソッドが手元の定義でオーバーライドされているのが原因らしい。

github.com

gem update --system すれば直りそうなんだけど、それを実行するのにも上記の require を通るため、如何ともし難い。というわけで上記差分のようにlib/rubygems/core_ext/kernel_require.rb を直接書き換えたところ、 gem update --system で修正版をインストールすることができた。

--- a/lib/rubygems/core_ext/kernel_require.rb
+++ b/lib/rubygems/core_ext/kernel_require.rb
@@ -43,7 +43,7 @@ module Kernel
 
     if spec = Gem.find_unresolved_default_spec(path)
       Gem.remove_unresolved_default_spec(spec)
-      gem(spec.name)
+      Kernel.send(:gem, spec.name)
     end
このブログに出てくるコードスニペッツは、引用あるいは断りがない限りMITライセンスです。