[ruby-core:125028] [Ruby Bug#21954] NoMethodError instead of Gem::LoadError on gem activation problem in Ruby 4.0.2
Issue #21954 has been reported by jeremyevans0 (Jeremy Evans). ---------------------------------------- Bug #21954: NoMethodError instead of Gem::LoadError on gem activation problem in Ruby 4.0.2 https://bugs.ruby-lang.org/issues/21954 * Author: jeremyevans0 (Jeremy Evans) * Status: Open * ruby -v: ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-linux] * Backport: 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED ---------------------------------------- I'm seeing a regression in Ruby 4.0.2 when there is a gem activation problem. Install two versions of a gem, where one version has a file the other doesn't. One example here is rack: ``` $ gem install rack --version '< 3' $ gem install rack ``` Force activation of the version that doesn't have the file, and then require the file and rescue `LoadError`: ```ruby gem 'rack', '< 3' begin require 'rack/headers' rescue LoadError end ``` In previous versions of Ruby, the `Gem::LoadError` would be correctly rescued, and you would get no output. In Ruby 4.0.2, you get a `NoMethodError` raised: ``` /usr/local/lib/ruby/4.0/rubygems.rb:196:in 'Gem.try_activate': undefined method 'name' for nil (NoMethodError) spec ||= Gem::Specification.find_by_name(spec.name) ^^^^^ from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /usr/local/lib/ruby/4.0/rubygems/specification.rb:2170:in 'Gem::Specification#check_version_conflict': can't activate rack-3.2.4, already activated rack-2.2.21 (Gem::LoadError) from /usr/local/lib/ruby/4.0/rubygems/specification.rb:1379:in 'Gem::Specification#activate' from /usr/local/lib/ruby/4.0/rubygems.rb:193:in 'Gem.try_activate' from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require': cannot load such file -- rack/headers (LoadError) from /home/jeremy/gem-activate-problem.rb:4:in '<main>' ``` Same behavior on both OpenBSD and Linux. -- https://bugs.ruby-lang.org/
Issue #21954 has been updated by hsbt (Hiroshi SHIBATA). @jeremyevans0 Thank you for your report. I confirmed this. This issue was introduced RubyGems [4.0.5](https://github.com/ruby/rubygems/releases/tag/v4.0.5). I will fix this at https://github.com/ruby/rubygems/pull/9404 and release RubyGems 4.0.9 with this fix at next week. ---------------------------------------- Bug #21954: NoMethodError instead of Gem::LoadError on gem activation problem in Ruby 4.0.2 https://bugs.ruby-lang.org/issues/21954#change-116728 * Author: jeremyevans0 (Jeremy Evans) * Status: Assigned * Assignee: hsbt (Hiroshi SHIBATA) * ruby -v: ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-linux] * Backport: 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED ---------------------------------------- I'm seeing a regression in Ruby 4.0.2 when there is a gem activation problem. Install two versions of a gem, where one version has a file the other doesn't. One example here is rack: ``` $ gem install rack --version '< 3' $ gem install rack ``` Force activation of the version that doesn't have the file, and then require the file and rescue `LoadError`: ```ruby gem 'rack', '< 3' begin require 'rack/headers' rescue LoadError end ``` In previous versions of Ruby, the `Gem::LoadError` would be correctly rescued, and you would get no output. In Ruby 4.0.2, you get a `NoMethodError` raised: ``` /usr/local/lib/ruby/4.0/rubygems.rb:196:in 'Gem.try_activate': undefined method 'name' for nil (NoMethodError) spec ||= Gem::Specification.find_by_name(spec.name) ^^^^^ from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /usr/local/lib/ruby/4.0/rubygems/specification.rb:2170:in 'Gem::Specification#check_version_conflict': can't activate rack-3.2.4, already activated rack-2.2.21 (Gem::LoadError) from /usr/local/lib/ruby/4.0/rubygems/specification.rb:1379:in 'Gem::Specification#activate' from /usr/local/lib/ruby/4.0/rubygems.rb:193:in 'Gem.try_activate' from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require': cannot load such file -- rack/headers (LoadError) from /home/jeremy/gem-activate-problem.rb:4:in '<main>' ``` Same behavior on both OpenBSD and Linux. -- https://bugs.ruby-lang.org/
Issue #21954 has been updated by hsbt (Hiroshi SHIBATA). Status changed from Assigned to Closed Fixed at https://github.com/ruby/ruby/commit/8ecf28f9384207590be73d5f2a95859cff9c526a ---------------------------------------- Bug #21954: NoMethodError instead of Gem::LoadError on gem activation problem in Ruby 4.0.2 https://bugs.ruby-lang.org/issues/21954#change-116733 * Author: jeremyevans0 (Jeremy Evans) * Status: Closed * Assignee: hsbt (Hiroshi SHIBATA) * ruby -v: ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-linux] * Backport: 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED ---------------------------------------- I'm seeing a regression in Ruby 4.0.2 when there is a gem activation problem. Install two versions of a gem, where one version has a file the other doesn't. One example here is rack: ``` $ gem install rack --version '< 3' $ gem install rack ``` Force activation of the version that doesn't have the file, and then require the file and rescue `LoadError`: ```ruby gem 'rack', '< 3' begin require 'rack/headers' rescue LoadError end ``` In previous versions of Ruby, the `Gem::LoadError` would be correctly rescued, and you would get no output. In Ruby 4.0.2, you get a `NoMethodError` raised: ``` /usr/local/lib/ruby/4.0/rubygems.rb:196:in 'Gem.try_activate': undefined method 'name' for nil (NoMethodError) spec ||= Gem::Specification.find_by_name(spec.name) ^^^^^ from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /usr/local/lib/ruby/4.0/rubygems/specification.rb:2170:in 'Gem::Specification#check_version_conflict': can't activate rack-3.2.4, already activated rack-2.2.21 (Gem::LoadError) from /usr/local/lib/ruby/4.0/rubygems/specification.rb:1379:in 'Gem::Specification#activate' from /usr/local/lib/ruby/4.0/rubygems.rb:193:in 'Gem.try_activate' from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require': cannot load such file -- rack/headers (LoadError) from /home/jeremy/gem-activate-problem.rb:4:in '<main>' ``` Same behavior on both OpenBSD and Linux. -- https://bugs.ruby-lang.org/
Issue #21954 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED to 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ruby_4_0 commit:fda2bc4ee3045a17ca52ca6075cdd955a813fbc1 merged revision(s) commit:8ecf28f9384207590be73d5f2a95859cff9c526a. ---------------------------------------- Bug #21954: NoMethodError instead of Gem::LoadError on gem activation problem in Ruby 4.0.2 https://bugs.ruby-lang.org/issues/21954#change-116921 * Author: jeremyevans0 (Jeremy Evans) * Status: Closed * Assignee: hsbt (Hiroshi SHIBATA) * ruby -v: ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-linux] * Backport: 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ---------------------------------------- I'm seeing a regression in Ruby 4.0.2 when there is a gem activation problem. Install two versions of a gem, where one version has a file the other doesn't. One example here is rack: ``` $ gem install rack --version '< 3' $ gem install rack ``` Force activation of the version that doesn't have the file, and then require the file and rescue `LoadError`: ```ruby gem 'rack', '< 3' begin require 'rack/headers' rescue LoadError end ``` In previous versions of Ruby, the `Gem::LoadError` would be correctly rescued, and you would get no output. In Ruby 4.0.2, you get a `NoMethodError` raised: ``` /usr/local/lib/ruby/4.0/rubygems.rb:196:in 'Gem.try_activate': undefined method 'name' for nil (NoMethodError) spec ||= Gem::Specification.find_by_name(spec.name) ^^^^^ from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /usr/local/lib/ruby/4.0/rubygems/specification.rb:2170:in 'Gem::Specification#check_version_conflict': can't activate rack-3.2.4, already activated rack-2.2.21 (Gem::LoadError) from /usr/local/lib/ruby/4.0/rubygems/specification.rb:1379:in 'Gem::Specification#activate' from /usr/local/lib/ruby/4.0/rubygems.rb:193:in 'Gem.try_activate' from /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require' from /home/jeremy/gem-activate-problem.rb:4:in '<main>' /home/jeremy/gem-activate-problem.rb:4:in 'Kernel#require': cannot load such file -- rack/headers (LoadError) from /home/jeremy/gem-activate-problem.rb:4:in '<main>' ``` Same behavior on both OpenBSD and Linux. -- https://bugs.ruby-lang.org/
participants (3)
-
hsbt (Hiroshi SHIBATA) -
jeremyevans0 (Jeremy Evans) -
k0kubun (Takashi Kokubun)