6daba0
From e80e7a3d0b3d72f7af7286b935702b3fab117008 Mon Sep 17 00:00:00 2001
6daba0
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
6daba0
Date: Wed, 8 Dec 2021 21:12:24 +0100
6daba0
Subject: [PATCH 1/5] More explicit require
6daba0
6daba0
This class does not use `rubygems/deprecate`. It uses
6daba0
`rubygems/version`, which in turn uses `rubygems/deprecate`. Make this
6daba0
explicit.
6daba0
---
6daba0
 lib/rubygems/requirement.rb | 2 +-
6daba0
 1 file changed, 1 insertion(+), 1 deletion(-)
6daba0
6daba0
diff --git a/lib/rubygems/requirement.rb b/lib/rubygems/requirement.rb
6daba0
index d2e28fab5b4..9edd6aa7d3c 100644
6daba0
--- a/lib/rubygems/requirement.rb
6daba0
+++ b/lib/rubygems/requirement.rb
6daba0
@@ -1,5 +1,5 @@
6daba0
 # frozen_string_literal: true
6daba0
-require_relative "deprecate"
6daba0
+require_relative "version"
6daba0
 
6daba0
 ##
6daba0
 # A Requirement is a set of one or more version restrictions. It supports a
6daba0
6daba0
From 4e46dcc17ee5cabbde43b8a34063b8ab042536f9 Mon Sep 17 00:00:00 2001
6daba0
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
6daba0
Date: Wed, 8 Dec 2021 21:17:30 +0100
6daba0
Subject: [PATCH 2/5] Remove ineffective autoloads
6daba0
6daba0
These files are loaded on startup unconditionally, so we can require
6daba0
them relatively when needed.
6daba0
---
6daba0
 lib/rubygems.rb               | 4 +---
6daba0
 lib/rubygems/specification.rb | 2 ++
6daba0
 2 files changed, 3 insertions(+), 3 deletions(-)
6daba0
6daba0
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
6daba0
index f803e47628e..b8747409304 100644
6daba0
--- a/lib/rubygems.rb
6daba0
+++ b/lib/rubygems.rb
6daba0
@@ -1310,19 +1310,17 @@ def default_gem_load_paths
6daba0
   autoload :Licenses,           File.expand_path('rubygems/util/licenses', __dir__)
6daba0
   autoload :NameTuple,          File.expand_path('rubygems/name_tuple', __dir__)
6daba0
   autoload :PathSupport,        File.expand_path('rubygems/path_support', __dir__)
6daba0
-  autoload :Platform,           File.expand_path('rubygems/platform', __dir__)
6daba0
   autoload :RequestSet,         File.expand_path('rubygems/request_set', __dir__)
6daba0
-  autoload :Requirement,        File.expand_path('rubygems/requirement', __dir__)
6daba0
   autoload :Resolver,           File.expand_path('rubygems/resolver', __dir__)
6daba0
   autoload :Source,             File.expand_path('rubygems/source', __dir__)
6daba0
   autoload :SourceList,         File.expand_path('rubygems/source_list', __dir__)
6daba0
   autoload :SpecFetcher,        File.expand_path('rubygems/spec_fetcher', __dir__)
6daba0
-  autoload :Specification,      File.expand_path('rubygems/specification', __dir__)
6daba0
   autoload :Util,               File.expand_path('rubygems/util', __dir__)
6daba0
   autoload :Version,            File.expand_path('rubygems/version', __dir__)
6daba0
 end
6daba0
 
6daba0
 require_relative 'rubygems/exceptions'
6daba0
+require_relative 'rubygems/specification'
6daba0
 
6daba0
 # REFACTOR: This should be pulled out into some kind of hacks file.
6daba0
 begin
6daba0
diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb
6daba0
index d3b96491a28..dc5e5ba0138 100644
6daba0
--- a/lib/rubygems/specification.rb
6daba0
+++ b/lib/rubygems/specification.rb
6daba0
@@ -9,6 +9,8 @@
6daba0
 require_relative 'deprecate'
6daba0
 require_relative 'basic_specification'
6daba0
 require_relative 'stub_specification'
6daba0
+require_relative 'platform'
6daba0
+require_relative 'requirement'
6daba0
 require_relative 'specification_policy'
6daba0
 require_relative 'util/list'
6daba0
 
6daba0
6daba0
From 96b6b3e04e8e4fec17f63079a0caf999a2709d71 Mon Sep 17 00:00:00 2001
6daba0
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
6daba0
Date: Wed, 8 Dec 2021 21:45:16 +0100
6daba0
Subject: [PATCH 3/5] Load `operating_system.rb` customizations before setting
6daba0
 up default gems
6daba0
6daba0
It's very common for packagers to configure gem paths in this file, for
6daba0
example, `Gem.default_dir`. Also, setting up default gems requires these
6daba0
paths to be set, so that we know which default gems need to be setup.
6daba0
6daba0
If we setup default gems before loading `operatin_system.rb`
6daba0
customizations, the wrong default gems will be setup.
6daba0
6daba0
Unfortunately, default gems loaded by `operating_system.rb` can't be
6daba0
upgraded if we do this, but it seems much of a smaller issue. I wasn't
6daba0
even fully sure it was the right thing to do when I added that, and it
6daba0
was not the culprit of the end user issue that led to making that
6daba0
change.
6daba0
---
6daba0
 lib/rubygems.rb                | 32 ++++++++++++++++----------------
6daba0
 test/rubygems/test_rubygems.rb | 23 +++++++++++++++++++++++
6daba0
 2 files changed, 39 insertions(+), 16 deletions(-)
6daba0
6daba0
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
6daba0
index b8747409304..11474b6554c 100644
6daba0
--- a/lib/rubygems.rb
6daba0
+++ b/lib/rubygems.rb
6daba0
@@ -1323,22 +1323,6 @@ def default_gem_load_paths
6daba0
 require_relative 'rubygems/specification'
6daba0
 
6daba0
 # REFACTOR: This should be pulled out into some kind of hacks file.
6daba0
-begin
6daba0
-  ##
6daba0
-  # Defaults the Ruby implementation wants to provide for RubyGems
6daba0
-
6daba0
-  require "rubygems/defaults/#{RUBY_ENGINE}"
6daba0
-rescue LoadError
6daba0
-end
6daba0
-
6daba0
-##
6daba0
-# Loads the default specs.
6daba0
-Gem::Specification.load_defaults
6daba0
-
6daba0
-require_relative 'rubygems/core_ext/kernel_gem'
6daba0
-require_relative 'rubygems/core_ext/kernel_require'
6daba0
-require_relative 'rubygems/core_ext/kernel_warn'
6daba0
-
6daba0
 begin
6daba0
   ##
6daba0
   # Defaults the operating system (or packager) wants to provide for RubyGems.
6daba0
@@ -1354,3 +1338,19 @@ def default_gem_load_paths
6daba0
     "the problem and ask for help."
6daba0
   raise e.class, msg
6daba0
 end
6daba0
+
6daba0
+begin
6daba0
+  ##
6daba0
+  # Defaults the Ruby implementation wants to provide for RubyGems
6daba0
+
6daba0
+  require "rubygems/defaults/#{RUBY_ENGINE}"
6daba0
+rescue LoadError
6daba0
+end
6daba0
+
6daba0
+##
6daba0
+# Loads the default specs.
6daba0
+Gem::Specification.load_defaults
6daba0
+
6daba0
+require_relative 'rubygems/core_ext/kernel_gem'
6daba0
+require_relative 'rubygems/core_ext/kernel_require'
6daba0
+require_relative 'rubygems/core_ext/kernel_warn'
6daba0
diff --git a/test/rubygems/test_rubygems.rb b/test/rubygems/test_rubygems.rb
6daba0
index 493b9fdf4a3..fa77a299322 100644
6daba0
--- a/test/rubygems/test_rubygems.rb
6daba0
+++ b/test/rubygems/test_rubygems.rb
6daba0
@@ -22,6 +22,29 @@ def test_operating_system_other_exceptions
6daba0
     "the problem and ask for help."
6daba0
   end
6daba0
 
6daba0
+  def test_operating_system_customizing_default_dir
6daba0
+    pend "does not apply to truffleruby" if RUBY_ENGINE == 'truffleruby'
6daba0
+    pend "loads a custom defaults/jruby file that gets in the middle" if RUBY_ENGINE == 'jruby'
6daba0
+
6daba0
+    # On a non existing default dir, there should be no gems
6daba0
+
6daba0
+    path = util_install_operating_system_rb <<-RUBY
6daba0
+      module Gem
6daba0
+        def self.default_dir
6daba0
+          File.expand_path("foo")
6daba0
+        end
6daba0
+      end
6daba0
+    RUBY
6daba0
+
6daba0
+    output = Gem::Util.popen(
6daba0
+      *ruby_with_rubygems_and_fake_operating_system_in_load_path(path),
6daba0
+      '-e',
6daba0
+      "require \"rubygems\"; puts Gem::Specification.stubs.map(&:full_name)",
6daba0
+      {:err => [:child, :out]}
6daba0
+    ).strip
6daba0
+    assert_empty output
6daba0
+  end
6daba0
+
6daba0
   private
6daba0
 
6daba0
   def util_install_operating_system_rb(content)
6daba0
6daba0
From 52cfdd14fd1213a97aac12f01177e27779de9035 Mon Sep 17 00:00:00 2001
6daba0
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
6daba0
Date: Thu, 9 Dec 2021 06:08:31 +0100
6daba0
Subject: [PATCH 4/5] Install default fiddle on latest ruby on specs that need
6daba0
 it
6daba0
6daba0
Otherwise first OS customizations load and activate that fiddle version,
6daba0
but then when we change to `Gem.default_dir`, that fiddle version is no
6daba0
longer there.
6daba0
---
6daba0
 spec/bundler/commands/clean_spec.rb          | 2 +-
6daba0
 spec/bundler/install/gems/standalone_spec.rb | 2 +-
6daba0
 2 files changed, 2 insertions(+), 2 deletions(-)
6daba0
6daba0
diff --git a/spec/bundler/commands/clean_spec.rb b/spec/bundler/commands/clean_spec.rb
6daba0
index ffaf22dbb32..65231b35fac 100644
6daba0
--- a/spec/bundler/commands/clean_spec.rb
6daba0
+++ b/spec/bundler/commands/clean_spec.rb
6daba0
@@ -638,7 +638,7 @@ def should_not_have_gems(*gems)
6daba0
       s.executables = "irb"
6daba0
     end
6daba0
 
6daba0
-    realworld_system_gems "fiddle --version 1.0.6", "tsort --version 0.1.0", "pathname --version 0.1.0", "set --version 1.0.1"
6daba0
+    realworld_system_gems "fiddle --version 1.0.8", "tsort --version 0.1.0", "pathname --version 0.1.0", "set --version 1.0.1"
6daba0
 
6daba0
     install_gemfile <<-G
6daba0
       source "#{file_uri_for(gem_repo2)}"
6daba0
diff --git a/spec/bundler/install/gems/standalone_spec.rb b/spec/bundler/install/gems/standalone_spec.rb
6daba0
index db16a1b0e13..faefda25f45 100644
6daba0
--- a/spec/bundler/install/gems/standalone_spec.rb
6daba0
+++ b/spec/bundler/install/gems/standalone_spec.rb
6daba0
@@ -113,7 +113,7 @@
6daba0
       skip "does not work on rubygems versions where `--install_dir` doesn't respect --default" unless Gem::Installer.for_spec(loaded_gemspec, :install_dir => "/foo").default_spec_file == "/foo/specifications/default/bundler-#{Bundler::VERSION}.gemspec" # Since rubygems 3.2.0.rc.2
6daba0
       skip "does not work on old rubies because the realworld gems that need to be installed don't support them" if RUBY_VERSION < "2.7.0"
6daba0
 
6daba0
-      realworld_system_gems "fiddle --version 1.0.6", "tsort --version 0.1.0"
6daba0
+      realworld_system_gems "fiddle --version 1.0.8", "tsort --version 0.1.0"
6daba0
 
6daba0
       necessary_system_gems = ["optparse --version 0.1.1", "psych --version 3.3.2", "yaml --version 0.1.1", "logger --version 1.4.3", "etc --version 1.2.0", "stringio --version 3.0.0"]
6daba0
       necessary_system_gems += ["shellwords --version 0.1.0", "base64 --version 0.1.0", "resolv --version 0.2.1"] if Gem.rubygems_version < Gem::Version.new("3.3.3.a")
6daba0
6daba0
From c6a9c81021092c9157f5616a2bbe1323411a5bf8 Mon Sep 17 00:00:00 2001
6daba0
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
6daba0
Date: Thu, 9 Dec 2021 12:46:23 +0100
6daba0
Subject: [PATCH 5/5] Resolve symlinks in LOAD_PATH when activating
6daba0
 pre-required default gems
6daba0
6daba0
Some double load issues were reported a while ago by OS packagers where
6daba0
if a gem has been required before rubygems, and then after, rubygems
6daba0
require would cause a double load.
6daba0
6daba0
We avoid this issue by activating the corresponding gem if we detect
6daba0
that a file in the default LOAD_PATH that belongs to a default gem has
6daba0
already been required when rubygems registers default gems.
6daba0
6daba0
However, the fix does not take into account that the default LOAD_PATH
6daba0
could potentially include symlinks. This change fixes the same double
6daba0
load issue described above but for situations where the default
6daba0
LOAD_PATH includes symlinks.
6daba0
---
6daba0
 lib/rubygems.rb | 7 ++++++-
6daba0
 1 file changed, 6 insertions(+), 1 deletion(-)
6daba0
6daba0
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
6daba0
index 11474b6554c..b7dda38d522 100644
6daba0
--- a/lib/rubygems.rb
6daba0
+++ b/lib/rubygems.rb
6daba0
@@ -1293,7 +1293,12 @@ def already_loaded?(file)
6daba0
     end
6daba0
 
6daba0
     def default_gem_load_paths
6daba0
-      @default_gem_load_paths ||= $LOAD_PATH[load_path_insert_index..-1]
6daba0
+      @default_gem_load_paths ||= $LOAD_PATH[load_path_insert_index..-1].map do |lp|
6daba0
+        expanded = File.expand_path(lp)
6daba0
+        next expanded unless File.exist?(expanded)
6daba0
+
6daba0
+        File.realpath(expanded)
6daba0
+      end
6daba0
     end
6daba0
   end
6daba0