From bece8808744ee178a4c55c6954edd30fb93b9ad3 Mon Sep 17 00:00:00 2001 From: Ry Biesemeyer Date: Thu, 2 Dec 2021 16:09:36 +0000 Subject: [PATCH] [rubygems/rubygems] Centralize logic using `Pathname#relative_path_from` To avoid potential crashes when trying to jump from a drive to another on Windows, and take the change refactor things a bit. https://github.com/rubygems/rubygems/commit/7c9a9a431a --- lib/bundler/cli/check.rb | 2 +- lib/bundler/cli/install.rb | 2 +- lib/bundler/definition.rb | 4 ++-- lib/bundler/env.rb | 4 ++-- lib/bundler/installer/standalone.rb | 9 +-------- lib/bundler/lockfile_parser.rb | 4 ++-- lib/bundler/shared_helpers.rb | 15 +++++++++++++++ 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/bundler/cli/check.rb b/lib/bundler/cli/check.rb index cc1f37f0c3..85c49f510a 100644 --- a/lib/bundler/cli/check.rb +++ b/lib/bundler/cli/check.rb @@ -29,7 +29,7 @@ module Bundler Bundler.ui.warn "Install missing gems with `bundle install`" exit 1 elsif !Bundler.default_lockfile.file? && Bundler.frozen_bundle? - Bundler.ui.error "This bundle has been frozen, but there is no #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} present" + Bundler.ui.error "This bundle has been frozen, but there is no #{SharedHelpers.relative_lockfile_path} present" exit 1 else Bundler.load.lock(:preserve_unknown_sections => true) unless options[:"dry-run"] diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 3422b19645..f7228db623 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -29,7 +29,7 @@ module Bundler flag ||= "--frozen flag" if options[:frozen] flag ||= "deployment setting" raise ProductionError, "The #{flag} requires a lockfile. Please make " \ - "sure you have checked your #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} into version control " \ + "sure you have checked your #{SharedHelpers.relative_lockfile_path} into version control " \ "before deploying." end diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 564530a98c..3c18ce7139 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -405,13 +405,13 @@ module Bundler msg << "\n\nYou have added to the Gemfile:\n" << added.join("\n") if added.any? msg << "\n\nYou have deleted from the Gemfile:\n" << deleted.join("\n") if deleted.any? msg << "\n\nYou have changed in the Gemfile:\n" << changed.join("\n") if changed.any? - msg << "\n\nRun `bundle install` elsewhere and add the updated #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)} to version control.\n" + msg << "\n\nRun `bundle install` elsewhere and add the updated #{SharedHelpers.relative_gemfile_path} to version control.\n" unless explicit_flag suggested_command = unless Bundler.settings.locations("frozen").keys.include?(:env) "bundle config set frozen false" end - msg << "If this is a development machine, remove the #{Bundler.default_gemfile.relative_path_from(SharedHelpers.pwd)} " \ + msg << "If this is a development machine, remove the #{SharedHelpers.relative_lockfile_path} " \ "freeze by running `#{suggested_command}`." if suggested_command end diff --git a/lib/bundler/env.rb b/lib/bundler/env.rb index 4f8b6f605d..f6cb198e38 100644 --- a/lib/bundler/env.rb +++ b/lib/bundler/env.rb @@ -40,11 +40,11 @@ module Bundler out << "\n## Gemfile\n" gemfiles.each do |gemfile| - out << "\n### #{Pathname.new(gemfile).relative_path_from(SharedHelpers.pwd)}\n\n" + out << "\n### #{SharedHelpers.relative_path_to(gemfile)}\n\n" out << "```ruby\n" << read_file(gemfile).chomp << "\n```\n" end - out << "\n### #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}\n\n" + out << "\n### #{SharedHelpers.relative_path_to(Bundler.default_lockfile)}\n\n" out << "```\n" << read_file(Bundler.default_lockfile).chomp << "\n```\n" end diff --git a/lib/bundler/installer/standalone.rb b/lib/bundler/installer/standalone.rb index 2145e846f7..fed3d8c410 100644 --- a/lib/bundler/installer/standalone.rb +++ b/lib/bundler/installer/standalone.rb @@ -55,20 +55,13 @@ module Bundler if spec.source.instance_of?(Source::Path) && spec.source.path.absolute? full_path else - relative_path_from(Bundler.root.join(bundler_path), :to => full_path) || full_path + SharedHelpers.relative_path_to(full_path, :from => Bundler.root.join(bundler_path)) end rescue TypeError error_message = "#{spec.name} #{spec.version} has an invalid gemspec" raise Gem::InvalidSpecificationException.new(error_message) end - def relative_path_from(source, to:) - Pathname.new(to).relative_path_from(source).to_s - rescue ArgumentError - # on Windows, if source and destination are on different drivers, there's no relative path from one to the other - nil - end - def define_path_helpers <<~'END' unless defined?(Gem) diff --git a/lib/bundler/lockfile_parser.rb b/lib/bundler/lockfile_parser.rb index 146e728989..31f57f14e8 100644 --- a/lib/bundler/lockfile_parser.rb +++ b/lib/bundler/lockfile_parser.rb @@ -68,7 +68,7 @@ module Bundler if lockfile.match?(/<<<<<<<|=======|>>>>>>>|\|\|\|\|\|\|\|/) raise LockfileError, "Your lockfile contains merge conflicts.\n" \ - "Run `git checkout HEAD -- #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` first to get a clean lock." + "Run `git checkout HEAD -- #{SharedHelpers.relative_lockfile_path}` first to get a clean lock." end lockfile.split(/(?:\r?\n)+/) do |line| @@ -92,7 +92,7 @@ module Bundler @specs = @specs.values.sort_by!(&:full_name) rescue ArgumentError => e Bundler.ui.debug(e) - raise LockfileError, "Your lockfile is unreadable. Run `rm #{Bundler.default_lockfile.relative_path_from(SharedHelpers.pwd)}` " \ + raise LockfileError, "Your lockfile is unreadable. Run `rm #{SharedHelpers.relative_lockfile_path}` " \ "and then `bundle install` to generate a new lockfile." end diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index d1d4e1d07a..165982ad0a 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -197,6 +197,21 @@ module Bundler filesystem_access(gemfile_path) {|g| File.open(g, "w") {|file| file.puts contents } } end + def relative_gemfile_path + relative_path_to(Bundler.default_gemfile) + end + + def relative_lockfile_path + relative_path_to(Bundler.default_lockfile) + end + + def relative_path_to(destination, from: pwd) + Pathname.new(destination).relative_path_from(from).to_s + rescue ArgumentError + # on Windows, if source and destination are on different drivers, there's no relative path from one to the other + destination + end + private def validate_bundle_path