diff options
Diffstat (limited to 'lib/rubygems.rb')
-rw-r--r-- | lib/rubygems.rb | 334 |
1 files changed, 179 insertions, 155 deletions
diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 80708e2aa0..ad7ab10756 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -1,22 +1,23 @@ # frozen_string_literal: true + #-- # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. # All rights reserved. # See LICENSE.txt for permissions. #++ -require 'rbconfig' +require "rbconfig" module Gem - VERSION = "3.3.0.dev".freeze + VERSION = "3.6.0.dev" end # Must be first since it unloads the prelude from 1.9.2 -require_relative 'rubygems/compatibility' +require_relative "rubygems/compatibility" -require_relative 'rubygems/defaults' -require_relative 'rubygems/deprecate' -require_relative 'rubygems/errors' +require_relative "rubygems/defaults" +require_relative "rubygems/deprecate" +require_relative "rubygems/errors" ## # RubyGems is the Ruby standard for publishing and managing third party @@ -48,7 +49,7 @@ require_relative 'rubygems/errors' # special location and loaded on boot. # # For an example plugin, see the {Graph gem}[https://github.com/seattlerb/graph] -# which adds a `gem graph` command. +# which adds a <tt>gem graph</tt> command. # # == RubyGems Defaults, Packaging # @@ -112,16 +113,7 @@ require_relative 'rubygems/errors' # -The RubyGems Team module Gem - RUBYGEMS_DIR = File.dirname File.expand_path(__FILE__) - - # Taint support is deprecated in Ruby 2.7. - # This allows switching ".untaint" to ".tap(&Gem::UNTAINT)", - # to avoid deprecation warnings in Ruby 2.7. - UNTAINT = RUBY_VERSION < '2.7' ? :untaint.to_sym : proc{} - - # When https://bugs.ruby-lang.org/issues/17259 is available, there is no need to override Kernel#warn - KERNEL_WARN_IGNORES_INTERNAL_ENTRIES = RUBY_ENGINE == "truffleruby" || - (RUBY_ENGINE == "ruby" && RUBY_VERSION >= '3.0') + RUBYGEMS_DIR = __dir__ ## # An Array of Regexps that match windows Ruby platforms. @@ -163,16 +155,6 @@ module Gem specifications/default ].freeze - ## - # Exception classes used in a Gem.read_binary +rescue+ statement - - READ_BINARY_ERRORS = [Errno::EACCES, Errno::EROFS, Errno::ENOSYS, Errno::ENOTSUP].freeze - - ## - # Exception classes used in Gem.write_binary +rescue+ statement - - WRITE_BINARY_ERRORS = [Errno::ENOSYS, Errno::ENOTSUP].freeze - @@win_platform = nil @configuration = nil @@ -195,6 +177,8 @@ module Gem @default_source_date_epoch = nil + @discover_gems_on_require = true + ## # Try to activate a gem containing +path+. Returns true if # activation succeeded or wasn't needed because it was already @@ -222,7 +206,7 @@ module Gem end end - return true + true end def self.needs @@ -272,9 +256,6 @@ module Gem unless spec = specs.first msg = "can't find gem #{dep} with executable #{exec_name}" - if dep.filters_bundler? && bundler_message = Gem::BundlerVersionFinder.missing_version_message - msg = bundler_message - end raise Gem::GemNotFoundException, msg end @@ -306,14 +287,14 @@ module Gem # The mode needed to read a file as straight binary. def self.binary_mode - 'rb' + "rb" end ## # The path where gem executables are to be installed. def self.bindir(install_dir=Gem.dir) - return File.join install_dir, 'bin' unless + return File.join install_dir, "bin" unless install_dir.to_s == Gem.default_dir.to_s Gem.default_bindir end @@ -322,7 +303,7 @@ module Gem # The path were rubygems plugins are to be installed. def self.plugindir(install_dir=Gem.dir) - File.join install_dir, 'plugins' + File.join install_dir, "plugins" end ## @@ -353,20 +334,10 @@ module Gem end ## - # The path to the data directory specified by the gem name. If the - # package is not available as a gem, return nil. - - def self.datadir(gem_name) - spec = @loaded_specs[gem_name] - return nil if spec.nil? - spec.datadir - end - - ## # A Zlib::Deflate.deflate wrapper def self.deflate(data) - require 'zlib' + require "zlib" Zlib::Deflate.deflate data end @@ -388,7 +359,7 @@ module Gem target = {} env.each_pair do |k,v| case k - when 'GEM_HOME', 'GEM_PATH', 'GEM_SPEC_CACHE' + when "GEM_HOME", "GEM_PATH", "GEM_SPEC_CACHE" case v when nil, String target[k] = v @@ -453,9 +424,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.ensure_subdirectories(dir, mode, subdirs) # :nodoc: old_umask = File.umask - File.umask old_umask | 002 - - require 'fileutils' + File.umask old_umask | 0o002 options = {} @@ -464,6 +433,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} subdirs.each do |name| subdir = File.join dir, name next if File.exist? subdir + + require "fileutils" + begin FileUtils.mkdir_p subdir, **options rescue SystemCallError @@ -478,7 +450,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # distinction as extensions cannot be shared between the two. def self.extension_api_version # :nodoc: - if 'no' == RbConfig::CONFIG['ENABLE_SHARED'] + if RbConfig::CONFIG["ENABLE_SHARED"] == "no" "#{ruby_api_version}-static" else ruby_api_version @@ -512,14 +484,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # the spec dirs directly, so we prune. files.uniq! if check_load_path - return files + files end def self.find_files_from_load_path(glob) # :nodoc: glob_with_suffixes = "#{glob}#{Gem.suffix_pattern}" $LOAD_PATH.map do |load_path| Gem::Util.glob_files_in_dir(glob_with_suffixes, load_path) - end.flatten.select {|file| File.file? file.tap(&Gem::UNTAINT) } + end.flatten.select {|file| File.file? file } end ## @@ -547,7 +519,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # the spec dirs directly, so we prune. files.uniq! if check_load_path - return files + files end ## @@ -588,14 +560,14 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} return i if path.instance_variable_defined?(:@gem_prelude_index) end - index = $LOAD_PATH.index RbConfig::CONFIG['sitelibdir'] + index = $LOAD_PATH.index RbConfig::CONFIG["sitelibdir"] index || 0 end ## - # The number of paths in the `$LOAD_PATH` from activated gems. Used to - # prioritize `-I` and `ENV['RUBYLIB`]` entries during `require`. + # The number of paths in the +$LOAD_PATH+ from activated gems. Used to + # prioritize +-I+ and <code>ENV['RUBYLIB']</code> entries during +require+. def self.activated_gem_paths @activated_gem_paths ||= 0 @@ -619,22 +591,24 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.load_yaml return if @yaml_loaded - begin - # Try requiring the gem version *or* stdlib version of psych. - require 'psych' - rescue ::LoadError - # If we can't load psych, that's fine, go on. - else - require_relative 'rubygems/psych_additions' - require_relative 'rubygems/psych_tree' - end + require "psych" + require_relative "rubygems/psych_tree" - require 'yaml' - require_relative 'rubygems/safe_yaml' + require_relative "rubygems/safe_yaml" @yaml_loaded = true end + @safe_marshal_loaded = false + + def self.load_safe_marshal + return if @safe_marshal_loaded + + require_relative "rubygems/safe_marshal" + + @safe_marshal_loaded = true + end + ## # The file name and line number of the caller of the caller of this method. # @@ -761,9 +735,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.prefix prefix = File.dirname RUBYGEMS_DIR - if prefix != File.expand_path(RbConfig::CONFIG['sitelibdir']) and - prefix != File.expand_path(RbConfig::CONFIG['libdir']) and - 'lib' == File.basename(RUBYGEMS_DIR) + if prefix != File.expand_path(RbConfig::CONFIG["sitelibdir"]) && + prefix != File.expand_path(RbConfig::CONFIG["libdir"]) && + File.basename(RUBYGEMS_DIR) == "lib" prefix end end @@ -779,40 +753,42 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Safely read a file in binary mode on all platforms. def self.read_binary(path) - File.open path, 'rb+' do |f| - f.flock(File::LOCK_EX) - f.read - end - rescue *READ_BINARY_ERRORS - File.open path, 'rb' do |f| - f.read - end - rescue Errno::ENOLCK # NFS - if Thread.main != Thread.current - raise - else - File.open path, 'rb' do |f| - f.read - end - end + open_file(path, "rb+", &:read) + rescue Errno::EACCES, Errno::EROFS + open_file(path, "rb", &:read) end ## # Safely write a file in binary mode on all platforms. def self.write_binary(path, data) - File.open(path, 'wb') do |io| - begin - io.flock(File::LOCK_EX) - rescue *WRITE_BINARY_ERRORS - end + open_file(path, "wb") do |io| io.write data end + rescue Errno::ENOSPC + # If we ran out of space but the file exists, it's *guaranteed* to be corrupted. + File.delete(path) if File.exist?(path) + raise + end + + ## + # Open a file with given flags, and on Windows protect access with flock + + def self.open_file(path, flags, &block) + File.open(path, flags) do |io| + if !java_platform? && win_platform? + begin + io.flock(File::LOCK_EX) + rescue Errno::ENOSYS, Errno::ENOTSUP + end + end + yield io + end rescue Errno::ENOLCK # NFS if Thread.main != Thread.current raise else - File.open(path, 'wb') do |io| - io.write data + File.open(path, flags) do |io| + yield io end end end @@ -824,7 +800,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} if @ruby.nil? @ruby = RbConfig.ruby - @ruby = "\"#{@ruby}\"" if @ruby =~ /\s/ + @ruby = "\"#{@ruby}\"" if /\s/.match?(@ruby) end @ruby @@ -834,13 +810,13 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Returns a String containing the API compatibility version of Ruby def self.ruby_api_version - @ruby_api_version ||= RbConfig::CONFIG['ruby_version'].dup + @ruby_api_version ||= RbConfig::CONFIG["ruby_version"].dup end def self.env_requirement(gem_name) @env_requirements_by_name ||= {} @env_requirements_by_name[gem_name] ||= begin - req = ENV["GEM_REQUIREMENT_#{gem_name.upcase}"] || '>= 0'.freeze + req = ENV["GEM_REQUIREMENT_#{gem_name.upcase}"] || ">= 0" Gem::Requirement.create(req) end end @@ -854,7 +830,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} fetcher = Gem::SpecFetcher.fetcher spec_tuples, = fetcher.spec_for_dependency dependency - spec, = spec_tuples.first + spec, = spec_tuples.last spec end @@ -863,16 +839,15 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Returns the latest release version of RubyGems. def self.latest_rubygems_version - latest_version_for('rubygems-update') or - raise "Can't find 'rubygems-update' in any repo. Check `gem source list`." + latest_version_for("rubygems-update") || + raise("Can't find 'rubygems-update' in any repo. Check `gem source list`.") end ## # Returns the version of the latest release-version of gem +name+ def self.latest_version_for(name) - spec = latest_spec_for name - spec and spec.version + latest_spec_for(name)&.version end ## @@ -882,9 +857,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} return @ruby_version if defined? @ruby_version version = RUBY_VERSION.dup - if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 - version << ".#{RUBY_PATCHLEVEL}" - elsif defined?(RUBY_DESCRIPTION) + if RUBY_PATCHLEVEL == -1 if RUBY_ENGINE == "ruby" desc = RUBY_DESCRIPTION[/\Aruby #{Regexp.quote(RUBY_VERSION)}([^ ]+) /, 1] else @@ -932,7 +905,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Glob pattern for require-able path suffixes. def self.suffix_pattern - @suffix_pattern ||= "{#{suffixes.join(',')}}" + @suffix_pattern ||= "{#{suffixes.join(",")}}" end ## @@ -960,14 +933,20 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Suffixes for require-able paths. def self.suffixes - @suffixes ||= ['', - '.rb', + @suffixes ||= ["", + ".rb", *%w[DLEXT DLEXT2].map do |key| val = RbConfig::CONFIG[key] - next unless val and not val.empty? + next unless val && !val.empty? ".#{val}" - end, - ].compact.uniq + end].compact.uniq + end + + ## + # Suffixes for dynamic library require-able paths. + + def self.dynamic_library_suffixes + @dynamic_library_suffixes ||= suffixes - [".rb"] end ## @@ -981,7 +960,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} elapsed = Time.now - now - ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display + ui.say format("%2$*1$s: %3$3.3fs", -width, msg, elapsed) if display value end @@ -990,7 +969,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # Lazily loads DefaultUserInteraction and returns the default UI. def self.ui - require_relative 'rubygems/user_interaction' + require_relative "rubygems/user_interaction" Gem::DefaultUserInteraction.ui end @@ -1012,8 +991,8 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.win_platform? if @@win_platform.nil? - ruby_platform = RbConfig::CONFIG['host_os'] - @@win_platform = !!WIN_PATTERNS.find {|r| ruby_platform =~ r } + ruby_platform = RbConfig::CONFIG["host_os"] + @@win_platform = !WIN_PATTERNS.find {|r| ruby_platform =~ r }.nil? end @@win_platform @@ -1027,19 +1006,25 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} end ## + # Is this platform Solaris? + + def self.solaris_platform? + RUBY_PLATFORM.include?("solaris") + end + + ## # Load +plugins+ as Ruby files def self.load_plugin_files(plugins) # :nodoc: plugins.each do |plugin| - # Skip older versions of the GemCutter plugin: Its commands are in # RubyGems proper now. - next if plugin =~ /gemcutter-0\.[0-3]/ + next if /gemcutter-0\.[0-3]/.match?(plugin) begin load plugin - rescue ::Exception => e + rescue ScriptError, StandardError => e details = "#{plugin.inspect}: #{e.message} (#{e.class})" warn "Error loading RubyGems plugin #{details}" end @@ -1085,7 +1070,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.use_gemdeps(path = nil) raise_exception = path - path ||= ENV['RUBYGEMS_GEMDEPS'] + path ||= ENV["RUBYGEMS_GEMDEPS"] return unless path path = path.dup @@ -1101,8 +1086,6 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} end end - path.tap(&Gem::UNTAINT) - unless File.file? path return unless raise_exception @@ -1110,17 +1093,15 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} end ENV["BUNDLE_GEMFILE"] ||= File.expand_path(path) - require_relative 'rubygems/user_interaction' + require_relative "rubygems/user_interaction" require "bundler" begin Gem::DefaultUserInteraction.use_ui(ui) do - begin - Bundler.ui.silence do - @gemdeps = Bundler.setup - end - ensure - Gem::DefaultUserInteraction.ui.close + Bundler.ui.silence do + @gemdeps = Bundler.setup end + ensure + Gem::DefaultUserInteraction.ui.close end rescue Bundler::BundlerError => e warn e.message @@ -1131,7 +1112,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} ## # If the SOURCE_DATE_EPOCH environment variable is set, returns it's value. - # Otherwise, returns the time that `Gem.source_date_epoch_string` was + # Otherwise, returns the time that +Gem.source_date_epoch_string+ was # first called in the same format as SOURCE_DATE_EPOCH. # # NOTE(@duckinator): The implementation is a tad weird because we want to: @@ -1166,7 +1147,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # This is used throughout RubyGems for enabling reproducible builds. def self.source_date_epoch - Time.at(self.source_date_epoch_string.to_i).utc.freeze + Time.at(source_date_epoch_string.to_i).utc.freeze end # FIX: Almost everywhere else we use the `def self.` way of defining class @@ -1177,9 +1158,17 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} # RubyGems distributors (like operating system package managers) can # disable RubyGems update by setting this to error message printed to # end-users on gem update --system instead of actual update. + attr_accessor :disable_system_update_message ## + # Whether RubyGems should enhance builtin `require` to automatically + # check whether the path required is present in installed gems, and + # automatically activate them and add them to `$LOAD_PATH`. + + attr_accessor :discover_gems_on_require + + ## # Hash of loaded Gem::Specification keyed by name attr_reader :loaded_specs @@ -1227,9 +1216,16 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} ## # Find a Gem::Specification of default gem from +path+ + def find_default_spec(path) + @path_to_default_spec_map[path] + end + + ## + # Find an unresolved Gem::Specification of default gem from +path+ + def find_unresolved_default_spec(path) default_spec = @path_to_default_spec_map[path] - return default_spec if default_spec && loaded_specs[default_spec.name] != default_spec + default_spec if default_spec && loaded_specs[default_spec.name] != default_spec end ## @@ -1293,7 +1289,12 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} end def default_gem_load_paths - @default_gem_load_paths ||= $LOAD_PATH[load_path_insert_index..-1] + @default_gem_load_paths ||= $LOAD_PATH[load_path_insert_index..-1].map do |lp| + expanded = File.expand_path(lp) + next expanded unless File.exist?(expanded) + + File.realpath(expanded) + end end end @@ -1302,39 +1303,40 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/".freeze - autoload :BundlerVersionFinder, File.expand_path('rubygems/bundler_version_finder', __dir__) - autoload :ConfigFile, File.expand_path('rubygems/config_file', __dir__) - autoload :Dependency, File.expand_path('rubygems/dependency', __dir__) - autoload :DependencyList, File.expand_path('rubygems/dependency_list', __dir__) - autoload :Installer, File.expand_path('rubygems/installer', __dir__) - autoload :Licenses, File.expand_path('rubygems/util/licenses', __dir__) - autoload :NameTuple, File.expand_path('rubygems/name_tuple', __dir__) - autoload :PathSupport, File.expand_path('rubygems/path_support', __dir__) - autoload :Platform, File.expand_path('rubygems/platform', __dir__) - autoload :RequestSet, File.expand_path('rubygems/request_set', __dir__) - autoload :Requirement, File.expand_path('rubygems/requirement', __dir__) - autoload :Resolver, File.expand_path('rubygems/resolver', __dir__) - autoload :Source, File.expand_path('rubygems/source', __dir__) - autoload :SourceList, File.expand_path('rubygems/source_list', __dir__) - autoload :SpecFetcher, File.expand_path('rubygems/spec_fetcher', __dir__) - autoload :Specification, File.expand_path('rubygems/specification', __dir__) - autoload :Util, File.expand_path('rubygems/util', __dir__) - autoload :Version, File.expand_path('rubygems/version', __dir__) + autoload :ConfigFile, File.expand_path("rubygems/config_file", __dir__) + autoload :CIDetector, File.expand_path("rubygems/ci_detector", __dir__) + autoload :Dependency, File.expand_path("rubygems/dependency", __dir__) + autoload :DependencyList, File.expand_path("rubygems/dependency_list", __dir__) + autoload :Installer, File.expand_path("rubygems/installer", __dir__) + autoload :Licenses, File.expand_path("rubygems/util/licenses", __dir__) + autoload :NameTuple, File.expand_path("rubygems/name_tuple", __dir__) + autoload :PathSupport, File.expand_path("rubygems/path_support", __dir__) + autoload :RequestSet, File.expand_path("rubygems/request_set", __dir__) + autoload :Requirement, File.expand_path("rubygems/requirement", __dir__) + autoload :Resolver, File.expand_path("rubygems/resolver", __dir__) + autoload :Source, File.expand_path("rubygems/source", __dir__) + autoload :SourceList, File.expand_path("rubygems/source_list", __dir__) + autoload :SpecFetcher, File.expand_path("rubygems/spec_fetcher", __dir__) + autoload :SpecificationPolicy, File.expand_path("rubygems/specification_policy", __dir__) + autoload :Util, File.expand_path("rubygems/util", __dir__) + autoload :Version, File.expand_path("rubygems/version", __dir__) end -require_relative 'rubygems/exceptions' +require_relative "rubygems/exceptions" +require_relative "rubygems/specification" # REFACTOR: This should be pulled out into some kind of hacks file. begin ## # Defaults the operating system (or packager) wants to provide for RubyGems. - require 'rubygems/defaults/operating_system' + require "rubygems/defaults/operating_system" rescue LoadError # Ignored rescue StandardError => e + path = e.backtrace_locations.reverse.find {|l| l.path.end_with?("rubygems/defaults/operating_system.rb") }.path msg = "#{e.message}\n" \ - "Loading the rubygems/defaults/operating_system.rb file caused an error. " \ + "Loading the #{path} file caused an error. " \ "This file is owned by your OS, not by rubygems upstream. " \ "Please find out which OS package this file belongs to and follow the guidelines from your OS to report " \ "the problem and ask for help." @@ -1349,10 +1351,32 @@ begin rescue LoadError end +# TruffleRuby >= 24 defines REUSE_AS_BINARY_ON_TRUFFLERUBY in defaults/truffleruby. +# However, TruffleRuby < 24 defines REUSE_AS_BINARY_ON_TRUFFLERUBY directly in its copy +# of lib/rubygems/platform.rb, so it is not defined if RubyGems is updated (gem update --system). +# Instead, we define it here in that case, similar to bundler/lib/bundler/rubygems_ext.rb. +# We must define it here and not in platform.rb because platform.rb is loaded before defaults/truffleruby. +class Gem::Platform + if RUBY_ENGINE == "truffleruby" && !defined?(REUSE_AS_BINARY_ON_TRUFFLERUBY) + REUSE_AS_BINARY_ON_TRUFFLERUBY = %w[libv8 libv8-node sorbet-static].freeze + end +end + ## # Loads the default specs. Gem::Specification.load_defaults -require_relative 'rubygems/core_ext/kernel_gem' -require_relative 'rubygems/core_ext/kernel_require' -require_relative 'rubygems/core_ext/kernel_warn' +require_relative "rubygems/core_ext/kernel_gem" + +path = File.join(__dir__, "rubygems/core_ext/kernel_require.rb") +# When https://bugs.ruby-lang.org/issues/17259 is available, there is no need to override Kernel#warn +if RUBY_ENGINE == "truffleruby" || + RUBY_ENGINE == "ruby" + file = "<internal:#{path}>" +else + require_relative "rubygems/core_ext/kernel_warn" + file = path +end +eval File.read(path), nil, file + +require ENV["BUNDLER_SETUP"] if ENV["BUNDLER_SETUP"] && !defined?(Bundler) |