`Gem::Version#<=>` drops `.0` segments inside prerelease identifiers, treating `"1.2.3-alpha.0.dev"` and `"1.2.3-alpha.dev"` as equal

Open Beginner friendly
#10,081 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

Start with lib/rubygems/version.rb, especially canonical_segments and the regular expression identified in the issue. Reproduce the comparison using the supplied Ruby command, then find the related version comparison tests and add coverage for the listed prerelease pairs. Done when zero segments inside prerelease identifiers are preserved and comparisons and requirement checks match the expected output.

Written by the indexing model from the issue text.

Description

Describe the problem as clearly as you can

Gem::Version#<=> (and therefore ==, hash, and Gem::Requirement#satisfied_by?) drops a .0 segment inside prerelease identifiers when no .0 segment precedes the first letter in the version string.

In lib/rubygems/version.rb (introduced in #7052), canonical_segments runs canonical_version.sub!(/(?<=\.|\A)[0.]+(?=[a-zA-Z])/, "") to remove trailing 0 segments before the first letter, but the regex is not anchored to precede the first letter, so when the release prefix has no trailing .0 it strips the first .0. run before a later letter instead.

Did you try upgrading RubyGems?

Yes. Reproduces on RubyGems 4.1.0 and master (5879ffe34b67, 4.2.0.dev), as well as 4.0.20. RubyGems 3.3.15 (before #7052) does not have this behavior.

Post steps to reproduce the problem

Compare prerelease versions containing a 0 segment before a subsequent letter segment using Gem::Version#<=> and Gem::Requirement#satisfied_by? with the command below.

Which command did you run?
ruby -e '
pairs = [
  ["1.a.0.b",           "1.a.b"],
  ["1.2.3-alpha.0.dev", "1.2.3-alpha.dev"],
  ["1.2-alpha.0.dev",   "1.2.0-alpha.0.dev"],
  ["1.rc0.b",           "1.rc.0.b"],
]

pairs.each do |a, b|
  va = Gem::Version.new(a)
  vb = Gem::Version.new(b)
  req = Gem::Requirement.new("= #{b}").satisfied_by?(va)
  puts "#{a.inspect} <=> #{b.inspect} => #{va <=> vb} " \
       "(canonical: #{va.canonical_segments.inspect} vs #{vb.canonical_segments.inspect}, " \
       "\"= #{b}\": #{req})"
end
'
What were you expecting to happen?

Zero segments inside prerelease identifiers are preserved (matching RubyGems 3.3.15):

"1.a.0.b" <=> "1.a.b" => 1 (canonical: [1, "a", 0, "b"] vs [1, "a", "b"], "= 1.a.b": false)
"1.2.3-alpha.0.dev" <=> "1.2.3-alpha.dev" => 1 (canonical: [1, 2, 3, "pre", "alpha", 0, "dev"] vs [1, 2, 3, "pre", "alpha", "dev"], "= 1.2.3-alpha.dev": false)
"1.2-alpha.0.dev" <=> "1.2.0-alpha.0.dev" => 0 (canonical: [1, 2, "pre", "alpha", 0, "dev"] vs [1, 2, "pre", "alpha", 0, "dev"], "= 1.2.0-alpha.0.dev": true)
"1.rc0.b" <=> "1.rc.0.b" => 0 (canonical: [1, "rc", 0, "b"] vs [1, "rc", 0, "b"], "= 1.rc.0.b": true)
What actually happened?
"1.a.0.b" <=> "1.a.b" => 0 (canonical: [1, "a", "b"] vs [1, "a", "b"], "= 1.a.b": true)
"1.2.3-alpha.0.dev" <=> "1.2.3-alpha.dev" => 0 (canonical: [1, 2, 3, "pre", "alpha", "dev"] vs [1, 2, 3, "pre", "alpha", "dev"], "= 1.2.3-alpha.dev": true)
"1.2-alpha.0.dev" <=> "1.2.0-alpha.0.dev" => -1 (canonical: [1, 2, "pre", "alpha", "dev"] vs [1, 2, "pre", "alpha", 0, "dev"], "= 1.2.0-alpha.0.dev": false)
"1.rc0.b" <=> "1.rc.0.b" => 1 (canonical: [1, "rc", 0, "b"] vs [1, "rc", "b"], "= 1.rc.0.b": false)
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/gem-home/gems
  - USER INSTALLATION DIRECTORY: /tmp/gem-user/.local/share/gem/ruby/4.0.0
  - CREDENTIALS FILE: /tmp/gem-user/.local/share/gem/credentials
  - RUBYGEMS PREFIX: /opt/rubygems-4.1.0
  - RUBY EXECUTABLE: /opt/ruby-4.0.9/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /tmp/gem-home/gems/bin
  - SPEC CACHE DIRECTORY: /tmp/gem-user/.cache/gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /opt/ruby-4.0.9/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-linux
  - GEM PATHS:
     - /tmp/gem-home/gems
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => true
     - :bulk_threshold => 1000
     - "gemhome" => "/tmp/gem-home/gems"
     - "gempath" => ["/tmp/gem-home/gems"]
  - 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.