diff options
Diffstat (limited to 'lib/rubygems/core_ext/kernel_require.rb')
| -rw-r--r--[-rwxr-xr-x] | lib/rubygems/core_ext/kernel_require.rb | 188 |
1 files changed, 99 insertions, 89 deletions
diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb index d3df9d85f9..3a9bdbdc9d 100755..100644 --- a/lib/rubygems/core_ext/kernel_require.rb +++ b/lib/rubygems/core_ext/kernel_require.rb @@ -1,25 +1,22 @@ # frozen_string_literal: true + #-- # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. # All rights reserved. # See LICENSE.txt for permissions. #++ -require 'monitor' +require "monitor" module Kernel - RUBYGEMS_ACTIVATION_MONITOR = Monitor.new # :nodoc: - if defined?(gem_original_require) then - # Ruby ships with a custom_require, override its require - remove_method :require - else - ## - # The Kernel#require from before RubyGems was loaded. - - alias gem_original_require require + # Make sure we have a reference to Ruby's original Kernel#require + unless defined?(gem_original_require) + # :stopdoc: + alias_method :gem_original_require, :require private :gem_original_require + # :startdoc: end ## @@ -36,107 +33,120 @@ module Kernel # The normal <tt>require</tt> functionality of returning false if # that file has already been loaded is preserved. - def require path - RUBYGEMS_ACTIVATION_MONITOR.enter + def require(path) # :doc: + return gem_original_require(path) unless Gem.discover_gems_on_require - path = path.to_path if path.respond_to? :to_path + RUBYGEMS_ACTIVATION_MONITOR.synchronize do + path = File.path(path) - if spec = Gem.find_unresolved_default_spec(path) - Gem.remove_unresolved_default_spec(spec) - begin - Kernel.send(:gem, spec.name) - rescue Exception - RUBYGEMS_ACTIVATION_MONITOR.exit - raise - end - end + # If +path+ belongs to a default gem, we activate it and then go straight + # to normal require - # If there are no unresolved deps, then we can use just try - # normal require handle loading a gem from the rescue below. + if spec = Gem.find_default_spec(path) + name = spec.name - if Gem::Specification.unresolved_deps.empty? then - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - end + next if Gem.loaded_specs[name] - # If +path+ is for a gem that has already been loaded, don't - # bother trying to find it in an unresolved gem, just go straight - # to normal require. - #-- - # TODO request access to the C implementation of this to speed up RubyGems + # Ensure -I beats a default gem + resolved_path = begin + rp = nil + load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths + Gem.suffixes.find do |s| + $LOAD_PATH[0...load_path_check_index].find do |lp| + if File.symlink? lp # for backward compatibility + next + end - if Gem::Specification.find_active_stub_by_path(path) - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - end + full_path = File.expand_path(File.join(lp, "#{path}#{s}")) + rp = full_path if File.file?(full_path) + end + end + rp + end - # Attempt to find +path+ in any unresolved gems... - - found_specs = Gem::Specification.find_in_unresolved path - - # If there are no directly unresolved gems, then try and find +path+ - # in any gems that are available via the currently unresolved gems. - # For example, given: - # - # a => b => c => d - # - # If a and b are currently active with c being unresolved and d.rb is - # requested, then find_in_unresolved_tree will find d.rb in d because - # it's a dependency of c. - # - if found_specs.empty? then - found_specs = Gem::Specification.find_in_unresolved_tree path - - found_specs.each do |found_spec| - found_spec.activate - end + next if resolved_path - # We found +path+ directly in an unresolved gem. Now we figure out, of - # the possible found specs, which one we should activate. - else + Kernel.send(:gem, name, Gem::Requirement.default_prerelease) - # Check that all the found specs are just different - # versions of the same gem - names = found_specs.map(&:name).uniq + Gem.load_bundler_extensions(Gem.loaded_specs[name].version) if name == "bundler" - if names.size > 1 then - RUBYGEMS_ACTIVATION_MONITOR.exit - raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ', '}" + next end - # Ok, now find a gem that has no conflicts, starting - # at the highest version. - valid = found_specs.find { |s| !s.has_conflicts? } + # If there are no unresolved deps, then we can use just try + # normal require handle loading a gem from the rescue below. - unless valid then - le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate" - le.name = names.first - RUBYGEMS_ACTIVATION_MONITOR.exit - raise le + if Gem::Specification.unresolved_deps.empty? + next end - valid.activate - end + # If +path+ is for a gem that has already been loaded, don't + # bother trying to find it in an unresolved gem, just go straight + # to normal require. + #-- + # TODO request access to the C implementation of this to speed up RubyGems + + if Gem::Specification.find_active_stub_by_path(path) + next + end - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - rescue LoadError => load_error - RUBYGEMS_ACTIVATION_MONITOR.enter + # Attempt to find +path+ in any unresolved gems... - begin - if load_error.message.start_with?("Could not find") or - (load_error.message.end_with?(path) and Gem.try_activate(path)) then - require_again = true + found_specs = Gem::Specification.find_in_unresolved path + + # If there are no directly unresolved gems, then try and find +path+ + # in any gems that are available via the currently unresolved gems. + # For example, given: + # + # a => b => c => d + # + # If a and b are currently active with c being unresolved and d.rb is + # requested, then find_in_unresolved_tree will find d.rb in d because + # it's a dependency of c. + # + if found_specs.empty? + found_specs = Gem::Specification.find_in_unresolved_tree path + + found_specs.each(&:activate) + + # We found +path+ directly in an unresolved gem. Now we figure out, of + # the possible found specs, which one we should activate. + else + + # Check that all the found specs are just different + # versions of the same gem + names = found_specs.map(&:name).uniq + + if names.size > 1 + raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ", "}" + end + + # Ok, now find a gem that has no conflicts, starting + # at the highest version. + valid = found_specs.find {|s| !s.has_conflicts? } + + unless valid + le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate" + le.name = names.first + raise le + end + + valid.activate end - ensure - RUBYGEMS_ACTIVATION_MONITOR.exit end - return gem_original_require(path) if require_again + begin + gem_original_require(path) + rescue LoadError => load_error + if load_error.path == path && + RUBYGEMS_ACTIVATION_MONITOR.synchronize { Gem.try_activate(path) } + + return gem_original_require(path) + end - raise load_error + raise load_error + end end private :require - end |
