From 5250210aa9f632a415e90deaac424ff69c37fb3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 26 Apr 2022 14:53:55 +0200 Subject: [PATCH] [rubygems/rubygems] Fix missing required rubygems version when using old APIs A while ago, we fixed resolution when using old dependency endpoints to also consider metadata dependencies, by requesting the full gemspec from the marsahaled index, which includes this information as opposed to these old APIs. This has made resolution slower, but correct, but also introduced the issue that some old marshaled gemspecs don't include the `required_rubygems_version` field because they were created with a RubyGems version that predates its addition. Use a default value in this case. https://github.com/rubygems/rubygems/commit/5dc94afcc0 Co-authored-by: Ilya Dudarenko --- lib/bundler/endpoint_specification.rb | 5 ++- spec/bundler/install/gemfile/sources_spec.rb | 41 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/bundler/endpoint_specification.rb b/lib/bundler/endpoint_specification.rb index f3260a38e6..e9aa366b41 100644 --- a/lib/bundler/endpoint_specification.rb +++ b/lib/bundler/endpoint_specification.rb @@ -26,8 +26,11 @@ module Bundler @required_ruby_version ||= _remote_specification.required_ruby_version end + # A fallback is included because the original version of the specification + # API didn't include that field, so some marshalled specs in the index have it + # set to +nil+. def required_rubygems_version - @required_rubygems_version ||= _remote_specification.required_rubygems_version + @required_rubygems_version ||= _remote_specification.required_rubygems_version || Gem::Requirement.default end def fetch_platform diff --git a/spec/bundler/install/gemfile/sources_spec.rb b/spec/bundler/install/gemfile/sources_spec.rb index 26ecb840c7..62cad6800f 100644 --- a/spec/bundler/install/gemfile/sources_spec.rb +++ b/spec/bundler/install/gemfile/sources_spec.rb @@ -1496,4 +1496,45 @@ RSpec.describe "bundle install with gems on multiple sources" do L end end + + context "when default source uses the old API and includes old gems with nil required_rubygems_version" do + before do + build_repo4 do + build_gem "pdf-writer", "1.1.8" + end + + path = "#{gem_repo4}/#{Gem::MARSHAL_SPEC_DIR}/pdf-writer-1.1.8.gemspec.rz" + spec = Marshal.load(Bundler.rubygems.inflate(File.binread(path))) + spec.instance_variable_set(:@required_rubygems_version, nil) + File.open(path, "wb") do |f| + f.write Gem.deflate(Marshal.dump(spec)) + end + + gemfile <<~G + source "https://localgemserver.test" + + gem "pdf-writer", "= 1.1.8" + G + end + + it "handles that fine" do + bundle "install --verbose", :artifice => "endpoint", :env => { "BUNDLER_SPEC_GEM_REPO" => gem_repo4.to_s } + + expect(lockfile).to eq <<~L + GEM + remote: https://localgemserver.test/ + specs: + pdf-writer (1.1.8) + + PLATFORMS + #{specific_local_platform} + + DEPENDENCIES + pdf-writer (= 1.1.8) + + BUNDLED WITH + #{Bundler::VERSION} + L + end + end end