From 69dc2ea46538f47ff5edda3bb16863034bd19069 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 21 Dec 2021 14:50:44 +0900 Subject: Merge RubyGems-3.3.0 and Bundler-2.3.0 --- lib/bundler.rb | 8 ++++ lib/bundler/cli.rb | 2 + lib/bundler/cli/install.rb | 2 + lib/bundler/definition.rb | 2 +- lib/bundler/fetcher.rb | 3 -- lib/bundler/installer.rb | 2 +- lib/bundler/lockfile_parser.rb | 23 +++++----- lib/bundler/plugin/installer.rb | 2 +- lib/bundler/rubygems_integration.rb | 33 +++++---------- lib/bundler/self_manager.rb | 73 ++++++++++++++++++++++++++++++++ lib/bundler/source/metadata.rb | 2 +- lib/bundler/templates/Executable.bundler | 2 +- lib/bundler/version.rb | 2 +- lib/rubygems.rb | 2 +- 14 files changed, 112 insertions(+), 46 deletions(-) create mode 100644 lib/bundler/self_manager.rb (limited to 'lib') diff --git a/lib/bundler.rb b/lib/bundler.rb index 81dfd05d26..89865a8254 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -66,6 +66,7 @@ module Bundler autoload :RubyDsl, File.expand_path("bundler/ruby_dsl", __dir__) autoload :RubyVersion, File.expand_path("bundler/ruby_version", __dir__) autoload :Runtime, File.expand_path("bundler/runtime", __dir__) + autoload :SelfManager, File.expand_path("bundler/self_manager", __dir__) autoload :Settings, File.expand_path("bundler/settings", __dir__) autoload :SharedHelpers, File.expand_path("bundler/shared_helpers", __dir__) autoload :Source, File.expand_path("bundler/source", __dir__) @@ -643,6 +644,13 @@ EOF Bundler.rubygems.clear_paths end + def self_manager + @self_manager ||= begin + require_relative "bundler/self_manager" + Bundler::SelfManager.new + end + end + private def eval_yaml_gemspec(path, contents) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 36d0472c62..f6e20e7c67 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -61,6 +61,8 @@ module Bundler Bundler.reset_settings_and_root! end + Bundler.self_manager.restart_with_locked_bundler_if_needed + Bundler.settings.set_command_option_if_given :retry, options[:retry] current_cmd = args.last[:current_command].name diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 4c1915fea6..85f702bc64 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -12,6 +12,8 @@ module Bundler warn_if_root + Bundler.self_manager.install_locked_bundler_and_restart_with_it_if_needed + Bundler::SharedHelpers.set_env "RB_USER_INSTALL", "1" if Bundler::FREEBSD # Disable color in deployment mode diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 84f0367ac7..9a94bd3ed3 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -294,7 +294,7 @@ module Bundler if updating_major = locked_major < current_major Bundler.ui.warn "Warning: the lockfile is being updated to Bundler #{current_major}, " \ - "after which you will be unable to return to Bundler #{@locked_bundler_version.segments.first}." + "after which you will be unable to return to Bundler #{locked_major}." end end diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb index 6dd6d089eb..a453157e68 100644 --- a/lib/bundler/fetcher.rb +++ b/lib/bundler/fetcher.rb @@ -122,7 +122,6 @@ module Bundler # return the specs in the bundler format as an index def specs(gem_names, source) - old = Bundler.rubygems.sources index = Bundler::Index.new if Bundler::Fetcher.disable_endpoint @@ -153,8 +152,6 @@ module Bundler rescue CertificateFailureError Bundler.ui.info "" if gem_names && use_api # newline after dots raise - ensure - Bundler.rubygems.sources = old end def use_api diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb index 49ff177c0f..61bf1e06d4 100644 --- a/lib/bundler/installer.rb +++ b/lib/bundler/installer.rb @@ -239,7 +239,7 @@ module Bundler def ensure_specs_are_compatible! system_ruby = Bundler::RubyVersion.system - rubygems_version = Gem::Version.create(Gem::VERSION) + rubygems_version = Bundler.rubygems.version @definition.specs.each do |spec| if required_ruby_version = spec.required_ruby_version unless required_ruby_version.satisfied_by?(system_ruby.gem_version) diff --git a/lib/bundler/lockfile_parser.rb b/lib/bundler/lockfile_parser.rb index 6ff4910a36..e074cbfc33 100644 --- a/lib/bundler/lockfile_parser.rb +++ b/lib/bundler/lockfile_parser.rb @@ -46,6 +46,16 @@ module Bundler attributes end + def self.bundled_with + lockfile = Bundler.default_lockfile + return unless lockfile.file? + + lockfile_contents = Bundler.read_file(lockfile) + return unless lockfile_contents.include?(BUNDLED) + + lockfile_contents.split(BUNDLED).last.strip + end + def initialize(lockfile) @platforms = [] @sources = [] @@ -77,25 +87,12 @@ module Bundler end end @specs = @specs.values.sort_by(&:identifier) - warn_for_outdated_bundler_version rescue ArgumentError => e Bundler.ui.debug(e) raise LockfileError, "Your lockfile is unreadable. Run `rm #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` " \ "and then `bundle install` to generate a new lockfile." end - def warn_for_outdated_bundler_version - return unless bundler_version - return if bundler_version.segments.last == "dev" - prerelease_text = bundler_version.prerelease? ? " --pre" : "" - current_version = Gem::Version.create(Bundler::VERSION) - return unless current_version < bundler_version - Bundler.ui.warn "Warning: the running version of Bundler (#{current_version}) is older " \ - "than the version that created the lockfile (#{bundler_version}). We suggest you to " \ - "upgrade to the version that created the lockfile by running `gem install " \ - "bundler:#{bundler_version}#{prerelease_text}`.\n" - end - private TYPES = { diff --git a/lib/bundler/plugin/installer.rb b/lib/bundler/plugin/installer.rb index d7411fff45..81ecafa470 100644 --- a/lib/bundler/plugin/installer.rb +++ b/lib/bundler/plugin/installer.rb @@ -21,7 +21,7 @@ module Bundler elsif options[:local_git] install_local_git(names, version, options) else - sources = options[:source] || Bundler.rubygems.sources + sources = options[:source] || Gem.sources install_rubygems(names, version, sources) end end diff --git a/lib/bundler/rubygems_integration.rb b/lib/bundler/rubygems_integration.rb index effb88c1cd..785f7fa360 100644 --- a/lib/bundler/rubygems_integration.rb +++ b/lib/bundler/rubygems_integration.rb @@ -12,25 +12,21 @@ module Bundler EXT_LOCK = Monitor.new end - def self.version - @version ||= Gem::Version.new(Gem::VERSION) - end - - def self.provides?(req_str) - Gem::Requirement.new(req_str).satisfied_by?(version) - end - def initialize @replaced_methods = {} backport_ext_builder_monitor end def version - self.class.version + @version ||= Gem.rubygems_version end def provides?(req_str) - self.class.provides?(req_str) + Gem::Requirement.new(req_str).satisfied_by?(version) + end + + def supports_bundler_trampolining? + provides?(">= 3.3.0.a") end def build_args @@ -142,19 +138,6 @@ module Bundler end end - def sources=(val) - # Gem.configuration creates a new Gem::ConfigFile, which by default will read ~/.gemrc - # If that file exists, its settings (including sources) will overwrite the values we - # are about to set here. In order to avoid that, we force memoizing the config file now. - configuration - - Gem.sources = val - end - - def sources - Gem.sources - end - def gem_dir Gem.dir end @@ -588,6 +571,10 @@ module Bundler end end + def find_bundler(version) + find_name("bundler").find {|s| s.version.to_s == version } + end + def find_name(name) Gem::Specification.stubs_for(name).map(&:to_spec) end diff --git a/lib/bundler/self_manager.rb b/lib/bundler/self_manager.rb new file mode 100644 index 0000000000..bda2eb51f3 --- /dev/null +++ b/lib/bundler/self_manager.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Bundler + # + # This class handles installing and switching to the version of bundler needed + # by an application. + # + class SelfManager + def restart_with_locked_bundler_if_needed + return unless needs_switching? && installed? + + restart_with_locked_bundler + end + + def install_locked_bundler_and_restart_with_it_if_needed + return unless needs_switching? + + Bundler.ui.info \ + "Bundler #{current_version} is running, but your lockfile was generated with #{lockfile_version}. " \ + "Installing Bundler #{lockfile_version} and restarting using that version." + + install_and_restart_with_locked_bundler + end + + private + + def install_and_restart_with_locked_bundler + bundler_dep = Gem::Dependency.new("bundler", lockfile_version) + + Gem.install(bundler_dep) + rescue StandardError => e + Bundler.ui.trace e + Bundler.ui.warn "There was an error installing the locked bundler version (#{lockfile_version}), rerun with the `--verbose` flag for more details. Going on using bundler #{current_version}." + else + restart_with_locked_bundler + end + + def restart_with_locked_bundler + configured_gem_home = ENV["GEM_HOME"] + configured_gem_path = ENV["GEM_PATH"] + + Bundler.with_original_env do + Kernel.exec( + { "GEM_HOME" => configured_gem_home, "GEM_PATH" => configured_gem_path, "BUNDLER_VERSION" => lockfile_version }, + $PROGRAM_NAME, *ARGV + ) + end + end + + def needs_switching? + ENV["BUNDLER_VERSION"].nil? && + Bundler.rubygems.supports_bundler_trampolining? && + SharedHelpers.in_bundle? && + lockfile_version && + !lockfile_version.end_with?(".dev") && + lockfile_version != current_version + end + + def installed? + Bundler.configure + + Bundler.rubygems.find_bundler(lockfile_version) + end + + def current_version + @current_version ||= Bundler::VERSION + end + + def lockfile_version + @lockfile_version ||= Bundler::LockfileParser.bundled_with + end + end +end diff --git a/lib/bundler/source/metadata.rb b/lib/bundler/source/metadata.rb index 50b65ce0ea..8bdbaa5527 100644 --- a/lib/bundler/source/metadata.rb +++ b/lib/bundler/source/metadata.rb @@ -25,7 +25,7 @@ module Bundler s.loaded_from = File.expand_path("..", __FILE__) end - if local_spec = Bundler.rubygems.find_name("bundler").find {|s| s.version.to_s == VERSION } + if local_spec = Bundler.rubygems.find_bundler(VERSION) idx << local_spec end diff --git a/lib/bundler/templates/Executable.bundler b/lib/bundler/templates/Executable.bundler index 8009412ea2..6bb5c51090 100644 --- a/lib/bundler/templates/Executable.bundler +++ b/lib/bundler/templates/Executable.bundler @@ -73,7 +73,7 @@ m = Module.new do requirement = bundler_gem_version.approximate_recommendation - return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0") + return requirement unless Gem.rubygems_version < Gem::Version.new("2.7.0") requirement += ".a" if bundler_gem_version.prerelease? diff --git a/lib/bundler/version.rb b/lib/bundler/version.rb index 0c0d9d360d..d987722f78 100644 --- a/lib/bundler/version.rb +++ b/lib/bundler/version.rb @@ -1,7 +1,7 @@ # frozen_string_literal: false module Bundler - VERSION = "2.3.0.dev".freeze + VERSION = "2.3.0".freeze def self.bundler_major_version @bundler_major_version ||= VERSION.split(".").first.to_i diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 40f3a893d8..a956e06bdb 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -8,7 +8,7 @@ require 'rbconfig' module Gem - VERSION = "3.3.0.dev".freeze + VERSION = "3.3.0".freeze end # Must be first since it unloads the prelude from 1.9.2 -- cgit v1.2.3