Skip to content

Commit

Permalink
Disregard Ruby patch level for modern rubies
Browse files Browse the repository at this point in the history
It's no longer meaningful since Ruby 2.0.0.

Now the resolution error when the Ruby version does not match is better
because it does not include a weird 4th segment.
  • Loading branch information
deivid-rodriguez committed Apr 15, 2022
1 parent 616c866 commit 21c145c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 58 deletions.
13 changes: 2 additions & 11 deletions bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,8 @@ def metadata_dependencies

def ruby_version_requirements(ruby_version)
return [] unless ruby_version
if ruby_version.patchlevel
[ruby_version.to_gem_version_with_patchlevel]
else
ruby_version.versions.map do |version|
requirement = Gem::Requirement.new(version)
if requirement.exact?
"~> #{version}.0"
else
requirement
end
end
ruby_version.versions.map do |version|
Gem::Requirement.new(version)
end
end

Expand Down
8 changes: 0 additions & 8 deletions bundler/lib/bundler/ruby_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ def self.system
@ruby_version ||= RubyVersion.new(ruby_version, patchlevel, ruby_engine, ruby_engine_version)
end

def to_gem_version_with_patchlevel
@gem_version_with_patch ||= begin
Gem::Version.create("#{@gem_version}.#{@patchlevel}")
rescue ArgumentError
@gem_version
end
end

def exact?
return @exact if defined?(@exact)
@exact = versions.all? {|v| Gem::Requirement.create(v).exact? }
Expand Down
17 changes: 17 additions & 0 deletions bundler/lib/bundler/rubygems_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ def gem_dir
full_gem_path
end

unless const_defined?(:LATEST_RUBY_WITHOUT_PATCH_VERSIONS)
LATEST_RUBY_WITHOUT_PATCH_VERSIONS = Gem::Version.new("2.1")

alias_method :rg_required_ruby_version=, :required_ruby_version=
def required_ruby_version=(req)
self.rg_required_ruby_version = req

@required_ruby_version.requirements.map! do |op, v|
if v >= LATEST_RUBY_WITHOUT_PATCH_VERSIONS && v.release.segments.size == 4
[op == "~>" ? "=" : op, Gem::Version.new(v.segments.tap {|s| s.delete_at(3) }.join("."))]
else
[op, v]
end
end
end
end

def groups
@groups ||= []
end
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/source/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Source
class Metadata < Source
def specs
@specs ||= Index.build do |idx|
idx << Gem::Specification.new("Ruby\0", RubyVersion.system.to_gem_version_with_patchlevel)
idx << Gem::Specification.new("Ruby\0", RubyVersion.system.gem_version)
idx << Gem::Specification.new("RubyGems\0", Gem::VERSION) do |s|
s.required_rubygems_version = Gem::Requirement.default
end
Expand Down
26 changes: 0 additions & 26 deletions bundler/spec/bundler/ruby_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,31 +498,5 @@
end
end
end

describe "#to_gem_version_with_patchlevel" do
shared_examples_for "the patchlevel is omitted" do
it "does not include a patch level" do
expect(subject.to_gem_version_with_patchlevel.to_s).to eq(version)
end
end

context "with nil patch number" do
let(:patchlevel) { nil }

it_behaves_like "the patchlevel is omitted"
end

context "with negative patch number" do
let(:patchlevel) { -1 }

it_behaves_like "the patchlevel is omitted"
end

context "with a valid patch number" do
it "uses the specified patchlevel as patchlevel" do
expect(subject.to_gem_version_with_patchlevel.to_s).to eq("#{version}.#{patchlevel}")
end
end
end
end
end
2 changes: 1 addition & 1 deletion bundler/spec/install/gems/resolving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
end

let(:ruby_requirement) { %("#{RUBY_VERSION}") }
let(:error_message_requirement) { "~> #{RUBY_VERSION}.0" }
let(:error_message_requirement) { "= #{RUBY_VERSION}" }

shared_examples_for "ruby version conflicts" do
it "raises an error during resolution" do
Expand Down
4 changes: 1 addition & 3 deletions lib/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,7 @@ def self.ruby_version
return @ruby_version if defined? @ruby_version
version = RUBY_VERSION.dup

if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1
version << ".#{RUBY_PATCHLEVEL}"
elsif defined?(RUBY_DESCRIPTION)
if defined?(RUBY_DESCRIPTION)
if RUBY_ENGINE == "ruby"
desc = RUBY_DESCRIPTION[/\Aruby #{Regexp.quote(RUBY_VERSION)}([^ ]+) /, 1]
else
Expand Down
10 changes: 10 additions & 0 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ def rdoc_options
@rdoc_options ||= []
end

LATEST_RUBY_WITHOUT_PATCH_VERSIONS = Gem::Version.new("2.1")

##
# The version of Ruby required by this gem. The ruby version can be
# specified to the patch-level:
Expand All @@ -683,6 +685,14 @@ def rdoc_options

def required_ruby_version=(req)
@required_ruby_version = Gem::Requirement.create req

@required_ruby_version.requirements.map! do |op, v|
if v >= LATEST_RUBY_WITHOUT_PATCH_VERSIONS && v.release.segments.size == 4
[op == "~>" ? "=" : op, Gem::Version.new(v.segments.tap {|s| s.delete_at(3) }.join("."))]
else
[op, v]
end
end
end

##
Expand Down
8 changes: 0 additions & 8 deletions test/rubygems/test_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1114,14 +1114,6 @@ def test_self_ruby_version_with_patchlevel_less_ancient_rubies
util_restore_RUBY_VERSION
end

def test_self_ruby_version_with_release
util_set_RUBY_VERSION '1.8.6', 287

assert_equal Gem::Version.new('1.8.6.287'), Gem.ruby_version
ensure
util_restore_RUBY_VERSION
end

def test_self_ruby_version_with_non_mri_implementations
util_set_RUBY_VERSION '2.5.0', 0, 60928, 'jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.171-b11 on 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11 [linux-x86_64]'

Expand Down

0 comments on commit 21c145c

Please sign in to comment.