029ae1
From 00d98eb8a3245fb93a475ecbbbc4c7ec7e6704cd Mon Sep 17 00:00:00 2001
029ae1
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
029ae1
Date: Tue, 19 May 2020 14:00:00 +0200
029ae1
Subject: [PATCH 1/5] Fix performance regression in `require`
029ae1
029ae1
Our check for `-I` paths should not go through all activated gems.
029ae1
---
029ae1
 lib/rubygems.rb                         | 10 ++++++++++
029ae1
 lib/rubygems/core_ext/kernel_require.rb |  2 +-
029ae1
 lib/rubygems/test_case.rb               |  1 +
029ae1
 test/rubygems/test_require.rb           | 11 +++++++++++
029ae1
 4 files changed, 23 insertions(+), 1 deletion(-)
029ae1
029ae1
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
029ae1
index 843cb49e4a..d1a9a1c7e1 100644
029ae1
--- a/lib/rubygems.rb
029ae1
+++ b/lib/rubygems.rb
029ae1
@@ -660,10 +660,20 @@ def self.load_path_insert_index
029ae1
     index
029ae1
   end
029ae1
 
029ae1
+  ##
029ae1
+  # The number of paths in the `$LOAD_PATH` from activated gems. Used to
029ae1
+  # prioritize `-I` and `ENV['RUBYLIB`]` entries during `require`.
029ae1
+
029ae1
+  def self.activated_gem_paths
029ae1
+    @activated_gem_paths ||= 0
029ae1
+  end
029ae1
+
029ae1
   ##
029ae1
   # Add a list of paths to the $LOAD_PATH at the proper place.
029ae1
 
029ae1
   def self.add_to_load_path(*paths)
029ae1
+    @activated_gem_paths = activated_gem_paths + paths.size
029ae1
+
029ae1
     insert_index = load_path_insert_index
029ae1
 
029ae1
     if insert_index
029ae1
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb
029ae1
index ed24111bd5..7625ce1bee 100644
029ae1
--- a/lib/rubygems/core_ext/kernel_require.rb
029ae1
+++ b/lib/rubygems/core_ext/kernel_require.rb
029ae1
@@ -47,7 +47,7 @@ def require(path)
029ae1
         load_path_insert_index = Gem.load_path_insert_index
029ae1
         break unless load_path_insert_index
029ae1
 
029ae1
-        $LOAD_PATH[0...load_path_insert_index].each do |lp|
029ae1
+        $LOAD_PATH[0...load_path_insert_index - Gem.activated_gem_paths].each do |lp|
029ae1
           safe_lp = lp.dup.tap(&Gem::UNTAINT)
029ae1
           begin
029ae1
             if File.symlink? safe_lp # for backward compatibility
029ae1
diff --git a/lib/rubygems/test_case.rb b/lib/rubygems/test_case.rb
029ae1
index a05a2898d1..53dd495aef 100644
029ae1
--- a/lib/rubygems/test_case.rb
029ae1
+++ b/lib/rubygems/test_case.rb
029ae1
@@ -379,6 +379,7 @@ def setup
029ae1
     Gem::Security.reset
029ae1
 
029ae1
     Gem.loaded_specs.clear
029ae1
+    Gem.instance_variable_set(:@activated_gem_paths, 0)
029ae1
     Gem.clear_default_specs
029ae1
     Bundler.reset!
029ae1
 
029ae1
diff --git a/test/rubygems/test_require.rb b/test/rubygems/test_require.rb
029ae1
index f36892f8cc..9f2fe3439a 100644
029ae1
--- a/test/rubygems/test_require.rb
029ae1
+++ b/test/rubygems/test_require.rb
029ae1
@@ -382,6 +382,17 @@ def test_default_gem_require_activates_just_once
029ae1
     assert_equal 0, times_called
029ae1
   end
029ae1
 
029ae1
+  def test_second_gem_require_does_not_resolve_path_manually_before_going_through_standard_require
029ae1
+    a1 = util_spec "a", "1", nil, "lib/test_gem_require_a.rb"
029ae1
+    install_gem a1
029ae1
+
029ae1
+    assert_require "test_gem_require_a"
029ae1
+
029ae1
+    stub(:gem_original_require, ->(path) { assert_equal "test_gem_require_a", path }) do
029ae1
+      require "test_gem_require_a"
029ae1
+    end
029ae1
+  end
029ae1
+
029ae1
   def test_realworld_default_gem
029ae1
     testing_ruby_repo = !ENV["GEM_COMMAND"].nil?
029ae1
     skip "this test can't work under ruby-core setup" if testing_ruby_repo || java_platform?
029ae1
029ae1
From ae95885dff6189c5ac59bbdf685cb4ec4751fdef Mon Sep 17 00:00:00 2001
029ae1
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
029ae1
Date: Tue, 19 May 2020 14:08:19 +0200
029ae1
Subject: [PATCH 2/5] Refactor `Gem.load_path_insert_index`
029ae1
029ae1
---
029ae1
 lib/rubygems.rb                         | 13 +++----------
029ae1
 lib/rubygems/core_ext/kernel_require.rb |  5 +----
029ae1
 2 files changed, 4 insertions(+), 14 deletions(-)
029ae1
029ae1
diff --git a/lib/rubygems.rb b/lib/rubygems.rb
029ae1
index d1a9a1c7e1..ca80326459 100644
029ae1
--- a/lib/rubygems.rb
029ae1
+++ b/lib/rubygems.rb
029ae1
@@ -657,7 +657,7 @@ def self.load_path_insert_index
029ae1
 
029ae1
     index = $LOAD_PATH.index RbConfig::CONFIG['sitelibdir']
029ae1
 
029ae1
-    index
029ae1
+    index || 0
029ae1
   end
029ae1
 
029ae1
   ##
029ae1
@@ -674,15 +674,8 @@ def self.activated_gem_paths
029ae1
   def self.add_to_load_path(*paths)
029ae1
     @activated_gem_paths = activated_gem_paths + paths.size
029ae1
 
029ae1
-    insert_index = load_path_insert_index
029ae1
-
029ae1
-    if insert_index
029ae1
-      # gem directories must come after -I and ENV['RUBYLIB']
029ae1
-      $LOAD_PATH.insert(insert_index, *paths)
029ae1
-    else
029ae1
-      # we are probably testing in core, -I and RUBYLIB don't apply
029ae1
-      $LOAD_PATH.unshift(*paths)
029ae1
-    end
029ae1
+    # gem directories must come after -I and ENV['RUBYLIB']
029ae1
+    $LOAD_PATH.insert(Gem.load_path_insert_index, *paths)
029ae1
   end
029ae1
 
029ae1
   @yaml_loaded = false
029ae1
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb
029ae1
index 7625ce1bee..decf4829f1 100644
029ae1
--- a/lib/rubygems/core_ext/kernel_require.rb
029ae1
+++ b/lib/rubygems/core_ext/kernel_require.rb
029ae1
@@ -44,10 +44,7 @@ def require(path)
029ae1
     resolved_path = begin
029ae1
       rp = nil
029ae1
       Gem.suffixes.each do |s|
029ae1
-        load_path_insert_index = Gem.load_path_insert_index
029ae1
-        break unless load_path_insert_index
029ae1
-
029ae1
-        $LOAD_PATH[0...load_path_insert_index - Gem.activated_gem_paths].each do |lp|
029ae1
+        $LOAD_PATH[0...Gem.load_path_insert_index - Gem.activated_gem_paths].each do |lp|
029ae1
           safe_lp = lp.dup.tap(&Gem::UNTAINT)
029ae1
           begin
029ae1
             if File.symlink? safe_lp # for backward compatibility
029ae1
029ae1
From da1492e9d7b28d068fbfbb0ba1cafcc516681567 Mon Sep 17 00:00:00 2001
029ae1
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
029ae1
Date: Tue, 19 May 2020 14:32:12 +0200
029ae1
Subject: [PATCH 3/5] Extract a local outside the loop
029ae1
029ae1
---
029ae1
 lib/rubygems/core_ext/kernel_require.rb | 3 ++-
029ae1
 1 file changed, 2 insertions(+), 1 deletion(-)
029ae1
029ae1
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb
029ae1
index decf4829f1..6a7faaf2d1 100644
029ae1
--- a/lib/rubygems/core_ext/kernel_require.rb
029ae1
+++ b/lib/rubygems/core_ext/kernel_require.rb
029ae1
@@ -43,8 +43,9 @@ def require(path)
029ae1
     # https://github.com/rubygems/rubygems/pull/1868
029ae1
     resolved_path = begin
029ae1
       rp = nil
029ae1
+      load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths
029ae1
       Gem.suffixes.each do |s|
029ae1
-        $LOAD_PATH[0...Gem.load_path_insert_index - Gem.activated_gem_paths].each do |lp|
029ae1
+        $LOAD_PATH[0...load_path_check_index].each do |lp|
029ae1
           safe_lp = lp.dup.tap(&Gem::UNTAINT)
029ae1
           begin
029ae1
             if File.symlink? safe_lp # for backward compatibility
029ae1
029ae1
From 22ad5717c38feda2375b53628d15ae3db2195684 Mon Sep 17 00:00:00 2001
029ae1
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
029ae1
Date: Thu, 21 May 2020 15:20:57 +0200
029ae1
Subject: [PATCH 4/5] Fix `$LOADED_FEATURES` cache sometimes not respected
029ae1
029ae1
In the cases where the initial manually `-I` path resolution succeeded,
029ae1
we were passing a full path to the original require effectively skipping
029ae1
the `$LOADED_FEATURES` cache. With this change, we _only_ do the
029ae1
resolution when a matching requirable path is found in a default gem. In
029ae1
that case, we skip activation of the default gem if we detect that the
029ae1
required file will be picked up for a `-I` path.
029ae1
---
029ae1
 lib/rubygems/core_ext/kernel_require.rb | 53 +++++++++++--------------
029ae1
 test/rubygems/test_require.rb           | 29 ++++++++++++++
029ae1
 2 files changed, 53 insertions(+), 29 deletions(-)
029ae1
029ae1
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb
029ae1
index 6a7faaf2d1..81e37b98bf 100644
029ae1
--- a/lib/rubygems/core_ext/kernel_require.rb
029ae1
+++ b/lib/rubygems/core_ext/kernel_require.rb
029ae1
@@ -39,46 +39,41 @@ def require(path)
029ae1
 
029ae1
     path = path.to_path if path.respond_to? :to_path
029ae1
 
029ae1
-    # Ensure -I beats a default gem
029ae1
-    # https://github.com/rubygems/rubygems/pull/1868
029ae1
-    resolved_path = begin
029ae1
-      rp = nil
029ae1
-      load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths
029ae1
-      Gem.suffixes.each do |s|
029ae1
-        $LOAD_PATH[0...load_path_check_index].each do |lp|
029ae1
-          safe_lp = lp.dup.tap(&Gem::UNTAINT)
029ae1
-          begin
029ae1
-            if File.symlink? safe_lp # for backward compatibility
029ae1
-              next
029ae1
+    if spec = Gem.find_unresolved_default_spec(path)
029ae1
+      # Ensure -I beats a default gem
029ae1
+      # https://github.com/rubygems/rubygems/pull/1868
029ae1
+      resolved_path = begin
029ae1
+        rp = nil
029ae1
+        load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths
029ae1
+        Gem.suffixes.each do |s|
029ae1
+          $LOAD_PATH[0...load_path_check_index].each do |lp|
029ae1
+            safe_lp = lp.dup.tap(&Gem::UNTAINT)
029ae1
+            begin
029ae1
+              if File.symlink? safe_lp # for backward compatibility
029ae1
+                next
029ae1
+              end
029ae1
+            rescue SecurityError
029ae1
+              RUBYGEMS_ACTIVATION_MONITOR.exit
029ae1
+              raise
029ae1
             end
029ae1
-          rescue SecurityError
029ae1
-            RUBYGEMS_ACTIVATION_MONITOR.exit
029ae1
-            raise
029ae1
-          end
029ae1
 
029ae1
-          full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}"))
029ae1
-          if File.file?(full_path)
029ae1
-            rp = full_path
029ae1
-            break
029ae1
+            full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}"))
029ae1
+            if File.file?(full_path)
029ae1
+              rp = full_path
029ae1
+              break
029ae1
+            end
029ae1
           end
029ae1
+          break if rp
029ae1
         end
029ae1
-        break if rp
029ae1
+        rp
029ae1
       end
029ae1
-      rp
029ae1
-    end
029ae1
 
029ae1
-    if resolved_path
029ae1
-      RUBYGEMS_ACTIVATION_MONITOR.exit
029ae1
-      return gem_original_require(resolved_path)
029ae1
-    end
029ae1
-
029ae1
-    if spec = Gem.find_unresolved_default_spec(path)
029ae1
       begin
029ae1
         Kernel.send(:gem, spec.name, Gem::Requirement.default_prerelease)
029ae1
       rescue Exception
029ae1
         RUBYGEMS_ACTIVATION_MONITOR.exit
029ae1
         raise
029ae1
-      end
029ae1
+      end unless resolved_path
029ae1
     end
029ae1
 
029ae1
     # If there are no unresolved deps, then we can use just try
029ae1
diff --git a/test/rubygems/test_require.rb b/test/rubygems/test_require.rb
029ae1
index 9f2fe3439a..2b11e26dfe 100644
029ae1
--- a/test/rubygems/test_require.rb
029ae1
+++ b/test/rubygems/test_require.rb
029ae1
@@ -45,6 +45,35 @@ def refute_require(path)
029ae1
     refute require(path), "'#{path}' was not yet required"
029ae1
   end
029ae1
 
029ae1
+  def test_respect_loaded_features_caching_like_standard_require
029ae1
+    dir = Dir.mktmpdir("test_require", @tempdir)
029ae1
+
029ae1
+    lp1 = File.join dir, 'foo1'
029ae1
+    foo1 = File.join lp1, 'foo.rb'
029ae1
+
029ae1
+    FileUtils.mkdir_p lp1
029ae1
+    File.open(foo1, 'w') { |f| f.write "class Object; HELLO = 'foo1' end" }
029ae1
+
029ae1
+    lp = $LOAD_PATH.dup
029ae1
+
029ae1
+    $LOAD_PATH.unshift lp1
029ae1
+    assert_require 'foo'
029ae1
+    assert_equal "foo1", ::Object::HELLO
029ae1
+
029ae1
+    lp2 = File.join dir, 'foo2'
029ae1
+    foo2 = File.join lp2, 'foo.rb'
029ae1
+
029ae1
+    FileUtils.mkdir_p lp2
029ae1
+    File.open(foo2, 'w') { |f| f.write "class Object; HELLO = 'foo2' end" }
029ae1
+
029ae1
+    $LOAD_PATH.unshift lp2
029ae1
+    refute_require 'foo'
029ae1
+    assert_equal "foo1", ::Object::HELLO
029ae1
+  ensure
029ae1
+    $LOAD_PATH.replace lp
029ae1
+    Object.send :remove_const, :HELLO if Object.const_defined? :HELLO
029ae1
+  end
029ae1
+
029ae1
   # Providing -I on the commandline should always beat gems
029ae1
   def test_dash_i_beats_gems
029ae1
     a1 = util_spec "a", "1", {"b" => "= 1"}, "lib/test_gem_require_a.rb"
029ae1
029ae1
From db872c7a18d616f4447bdcca3130be6db9e5cb03 Mon Sep 17 00:00:00 2001
029ae1
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@riseup.net>
029ae1
Date: Sat, 23 May 2020 20:18:41 +0200
029ae1
Subject: [PATCH 5/5] Remove direct reference to PR
029ae1
029ae1
The code is quite different now, so I think the link might be even
029ae1
confusing. If you want to know more, use git history.
029ae1
---
029ae1
 lib/rubygems/core_ext/kernel_require.rb | 1 -
029ae1
 1 file changed, 1 deletion(-)
029ae1
029ae1
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb
029ae1
index 81e37b98bf..115ae0cb50 100644
029ae1
--- a/lib/rubygems/core_ext/kernel_require.rb
029ae1
+++ b/lib/rubygems/core_ext/kernel_require.rb
029ae1
@@ -41,7 +41,6 @@ def require(path)
029ae1
 
029ae1
     if spec = Gem.find_unresolved_default_spec(path)
029ae1
       # Ensure -I beats a default gem
029ae1
-      # https://github.com/rubygems/rubygems/pull/1868
029ae1
       resolved_path = begin
029ae1
         rp = nil
029ae1
         load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths