From f0f138aa5d5857fb435bfaef95201d530f6d35da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 21 May 2020 15:20:57 +0200 Subject: [rubygems/rubygems] Fix `$LOADED_FEATURES` cache sometimes not respected In the cases where the initial manually `-I` path resolution succeeded, we were passing a full path to the original require effectively skipping the `$LOADED_FEATURES` cache. With this change, we _only_ do the resolution when a matching requirable path is found in a default gem. In that case, we skip activation of the default gem if we detect that the required file will be picked up for a `-I` path. https://github.com/rubygems/rubygems/commit/22ad5717c3 --- lib/rubygems/core_ext/kernel_require.rb | 53 +++++++++++++++------------------ 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb index 6a7faaf2d1..81e37b98bf 100644 --- a/lib/rubygems/core_ext/kernel_require.rb +++ b/lib/rubygems/core_ext/kernel_require.rb @@ -39,46 +39,41 @@ module Kernel path = path.to_path if path.respond_to? :to_path - # Ensure -I beats a default gem - # https://github.com/rubygems/rubygems/pull/1868 - resolved_path = begin - rp = nil - load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths - Gem.suffixes.each do |s| - $LOAD_PATH[0...load_path_check_index].each do |lp| - safe_lp = lp.dup.tap(&Gem::UNTAINT) - begin - if File.symlink? safe_lp # for backward compatibility - next + if spec = Gem.find_unresolved_default_spec(path) + # Ensure -I beats a default gem + # https://github.com/rubygems/rubygems/pull/1868 + resolved_path = begin + rp = nil + load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths + Gem.suffixes.each do |s| + $LOAD_PATH[0...load_path_check_index].each do |lp| + safe_lp = lp.dup.tap(&Gem::UNTAINT) + begin + if File.symlink? safe_lp # for backward compatibility + next + end + rescue SecurityError + RUBYGEMS_ACTIVATION_MONITOR.exit + raise end - rescue SecurityError - RUBYGEMS_ACTIVATION_MONITOR.exit - raise - end - full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}")) - if File.file?(full_path) - rp = full_path - break + full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}")) + if File.file?(full_path) + rp = full_path + break + end end + break if rp end - break if rp + rp end - rp - end - if resolved_path - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(resolved_path) - end - - if spec = Gem.find_unresolved_default_spec(path) begin Kernel.send(:gem, spec.name, Gem::Requirement.default_prerelease) rescue Exception RUBYGEMS_ACTIVATION_MONITOR.exit raise - end + end unless resolved_path end # If there are no unresolved deps, then we can use just try -- cgit v1.2.3