`gem install` crashes with `Gem::DependencyError: Unresolved dependency found during sorting` when a transitive gem pulls in a prerelease shared with a non-prerelease dependency

Open
#10,085 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ruby
Domain
backend, devtools

Research direction

Start with lib/rubygems/request_set.rb:537, where Gem::RequestSet#tsort_each_child rejects the resolved prerelease dependency; compare its matching check with the prerelease handling described for Gem::Resolver#versions_for. Run the supplied gem install reproduction and gem install --explain case. Done when both commands handle the resolved bb-1.1.0.rc1, aa-1.0.0, and cc-1.0.0 without the sorting error.

Written by the indexing model from the issue text.

Description

Describe the problem as clearly as you can

When gem cc depends on aa and bb >= 1.0, and aa depends on bb >= 1.1.0.rc1, running gem install cc without --pre resolves {cc 1.0.0, aa 1.0.0, bb 1.1.0.rc1} via Gem::Resolver, then crashes during install-order sorting with Gem::DependencyError: Unresolved dependency found during sorting - bb (requested by cc-1.0.0). gem install cc --explain crashes with the same error, whereas bundle install on Bundler 4.1.0 installs all three gems cleanly.

Gem::Resolver#versions_for admits prerelease versions per constraint via range_admits_prerelease? even when @prerelease is false, so PubGrub resolves bb-1.1.0.rc1 for the combined requirement. Afterwards, Gem::RequestSet#tsort_each_child (lib/rubygems/request_set.rb:537) checks dep.match?(r.spec.name, r.spec.version, r.spec.is_a?(Gem::Resolver::InstalledSpecification) || @prerelease) with @prerelease == false, which rejects bb-1.1.0.rc1 for cc's non-prerelease bb >= 1.0 dependency and raises Gem::DependencyError.

Did you try upgrading RubyGems?

Yes. Reproduced on RubyGems 4.1.0 (9af9267146) and ruby/rubygems master (5879ffe34b, 4.2.0.dev) on Ruby 4.0.9.

Post steps to reproduce the problem
mkdir /tmp/repro-gem41-sort && cd /tmp/repro-gem41-sort
mkdir -p repo/gems repo/quick/Marshal.4.8

ruby - << 'RUBY'
require "rubygems/package"
require "zlib"

specs = [
  Gem::Specification.new do |s|
    s.name = "cc"; s.version = "1.0.0"; s.summary = "cc"; s.authors = ["test"]
    s.add_dependency "aa"
    s.add_dependency "bb", ">= 1.0"
  end,
  Gem::Specification.new do |s|
    s.name = "aa"; s.version = "1.0.0"; s.summary = "aa"; s.authors = ["test"]
    s.add_dependency "bb", ">= 1.1.0.rc1"
  end,
  Gem::Specification.new do |s|
    s.name = "bb"; s.version = "1.0.0"; s.summary = "bb 1.0"; s.authors = ["test"]
  end,
  Gem::Specification.new do |s|
    s.name = "bb"; s.version = "1.1.0.rc1"; s.summary = "bb rc1"; s.authors = ["test"]
  end,
]

specs.each do |s|
  gem_file = Gem::Package.build(s, true)
  File.rename(gem_file, "repo/gems/" + gem_file)
  File.binwrite("repo/quick/Marshal.4.8/" + s.original_name + ".gemspec.rz",
    Zlib::Deflate.deflate(Marshal.dump(s)))
end

rel, pre = specs.partition { |s| !s.version.prerelease? }
File.binwrite("repo/specs.4.8.gz",
  Zlib.gzip(Marshal.dump(rel.map { |s| [s.name, s.version, "ruby"] })))
File.binwrite("repo/prerelease_specs.4.8.gz",
  Zlib.gzip(Marshal.dump(pre.map { |s| [s.name, s.version, "ruby"] })))
RUBY

gem install cc -i "$PWD/gemhome" --clear-sources --source "file://$PWD/repo" --no-document --backtrace
Which command did you run?

gem install cc -i "$PWD/gemhome" --clear-sources --source "file://$PWD/repo" --no-document --backtrace

What were you expecting to happen?

gem install cc installs bb-1.1.0.rc1, aa-1.0.0, and cc-1.0.0 in topological order without raising an internal sorting error, matching bundle install on Bundler 4.1.0:

Successfully installed bb-1.1.0.rc1
Successfully installed aa-1.0.0
Successfully installed cc-1.0.0
3 gems installed
What actually happened?
ERROR:  While executing gem ... (Gem::DependencyError)
    Unresolved dependency found during sorting - bb (requested by cc-1.0.0) (Gem::DependencyError)
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:543:in 'block in Gem::RequestSet#tsort_each_child'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:533:in 'Array#each'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:533:in 'Gem::RequestSet#tsort_each_child'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:418:in 'Method#call'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:418:in 'Gem::TSort.each_strongly_connected_component_from'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:352:in 'block in Gem::TSort.each_strongly_connected_component'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:529:in 'Array#each'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:529:in 'Gem::RequestSet#tsort_each_node'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:350:in 'Method#call'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:350:in 'Gem::TSort.each_strongly_connected_component'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:284:in 'Enumerator#each'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:284:in 'Enumerable#to_a'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:284:in 'Gem::TSort.strongly_connected_components'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/vendor/tsort/lib/tsort.rb:260:in 'Gem::TSort#strongly_connected_components'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:513:in 'Gem::RequestSet#sorted_requests'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:274:in 'Gem::RequestSet#install_into'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/request_set.rb:155:in 'Gem::RequestSet#install'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/commands/install_command.rb:212:in 'Gem::Commands::InstallCommand#install_gem'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/commands/install_command.rb:230:in 'block in Gem::Commands::InstallCommand#install_gems'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/commands/install_command.rb:223:in 'Array#each'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/commands/install_command.rb:223:in 'Gem::Commands::InstallCommand#install_gems'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/commands/install_command.rb:163:in 'Gem::Commands::InstallCommand#execute'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/command.rb:326:in 'Gem::Command#invoke_with_build_args'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/command_manager.rb:248:in 'Gem::CommandManager#invoke_command'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/command_manager.rb:189:in 'Gem::CommandManager#process_args'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/command_manager.rb:151:in 'Gem::CommandManager#run'
	/opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0/rubygems/gem_runner.rb:56:in 'Gem::GemRunner#run'
	/opt/ruby-4.0.9/bin/gem:12:in '<main>'
Run gem env and paste the output below
RubyGems Environment:
  - RUBYGEMS VERSION: 4.1.0
  - RUBY VERSION: 4.0.9 (2026-10-23 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /tmp/repro-gem41-sort/gemhome
  - USER INSTALLATION DIRECTORY: /home/user/.local/share/gem/ruby/4.0.0
  - CREDENTIALS FILE: /home/user/.local/share/gem/credentials
  - RUBYGEMS PREFIX: /opt/ruby-4.0.9/lib/ruby/site_ruby/4.0.0
  - RUBY EXECUTABLE: /opt/ruby-4.0.9/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /tmp/repro-gem41-sort/gemhome/bin
  - SPEC CACHE DIRECTORY: /home/user/.cache/gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /opt/ruby-4.0.9/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-linux
  - GEM PATHS:
     - /tmp/repro-gem41-sort/gemhome
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /opt/ruby-4.0.9/bin
     - /usr/bin
     - /bin

Credit: https://ucsbplse.github.io/

Dominant language
Ruby
Stars
4k
Forks
2k
Avg merge
1d 20h
Merged PRs (30d)
75

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby/rubygems

All issues in ruby/rubygems

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.