From 0b4dbe2e6a0a0b27860fb6a509ff0429ece436c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Sat, 31 Jul 2021 14:10:59 +0200 Subject: [PATCH] [rubygems/rubygems] Improve "gem not found in source" errors When printing sources inside these error messages, it's useful to only consider the current state of the source. For example, when requiring `bundler/setup`, the source shouldn't be configured to be able to hit the network, so the error message should only mention "locally installed gems" to make that more clear. https://github.com/rubygems/rubygems/commit/30eb14f853 --- lib/bundler/resolver.rb | 4 ++-- lib/bundler/source.rb | 4 ++++ lib/bundler/source/rubygems.rb | 17 ++++++++++++++++- lib/bundler/source/rubygems_aggregate.rb | 4 ++++ spec/bundler/commands/exec_spec.rb | 4 ++-- spec/bundler/commands/lock_spec.rb | 2 +- spec/bundler/support/indexes.rb | 2 +- 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/bundler/resolver.rb b/lib/bundler/resolver.rb index 0e28e8559b..d26e2feb10 100644 --- a/lib/bundler/resolver.rb +++ b/lib/bundler/resolver.rb @@ -272,7 +272,7 @@ module Bundler rescue GemfileNotFound nil end - message = String.new("Could not find gem '#{SharedHelpers.pretty_dependency(requirement)}' in #{source}#{cache_message}.\n") + message = String.new("Could not find gem '#{SharedHelpers.pretty_dependency(requirement)}' in #{source.to_err}#{cache_message}.\n") message << "The source contains the following versions of '#{name}': #{formatted_versions_with_platforms(versions_with_platforms)}" if versions_with_platforms.any? end raise GemNotFound, message @@ -371,7 +371,7 @@ module Bundler o << if metadata_requirement "is not available in #{relevant_source}" else - "in #{relevant_source}.\n" + "in #{relevant_source.to_err}.\n" end end end, diff --git a/lib/bundler/source.rb b/lib/bundler/source.rb index 5388a7681e..434112ac8a 100644 --- a/lib/bundler/source.rb +++ b/lib/bundler/source.rb @@ -67,6 +67,10 @@ module Bundler "#<#{self.class}:0x#{object_id} #{self}>" end + def to_err + to_s + end + def path? instance_of?(Bundler::Source::Path) end diff --git a/lib/bundler/source/rubygems.rb b/lib/bundler/source/rubygems.rb index 858a69a48b..6728ffdc63 100644 --- a/lib/bundler/source/rubygems.rb +++ b/lib/bundler/source/rubygems.rb @@ -96,11 +96,22 @@ module Bundler out << " specs:\n" end + def to_err + if remotes.empty? + "locally installed gems" + elsif @allow_remote + "rubygems repository #{remote_names} or installed locally" + elsif @allow_cached + "cached gems from rubygems repository #{remote_names} or installed locally" + else + "locally installed gems" + end + end + def to_s if remotes.empty? "locally installed gems" else - remote_names = remotes.map(&:to_s).join(", ") "rubygems repository #{remote_names} or installed locally" end end @@ -319,6 +330,10 @@ module Bundler protected + def remote_names + remotes.map(&:to_s).join(", ") + end + def credless_remotes remotes.map(&method(:suppress_configured_credentials)) end diff --git a/lib/bundler/source/rubygems_aggregate.rb b/lib/bundler/source/rubygems_aggregate.rb index 685bf7e90a..09cf4002ea 100644 --- a/lib/bundler/source/rubygems_aggregate.rb +++ b/lib/bundler/source/rubygems_aggregate.rb @@ -16,6 +16,10 @@ module Bundler @index end + def to_err + to_s + end + def to_s "any of the sources" end diff --git a/spec/bundler/commands/exec_spec.rb b/spec/bundler/commands/exec_spec.rb index 68c4726608..c6648f0a7a 100644 --- a/spec/bundler/commands/exec_spec.rb +++ b/spec/bundler/commands/exec_spec.rb @@ -836,7 +836,7 @@ RSpec.describe "bundle exec" do let(:exit_code) { Bundler::GemNotFound.new.status_code } let(:expected) { "" } let(:expected_err) { <<-EOS.strip } -Could not find gem 'rack (= 2)' in rubygems repository #{file_uri_for(gem_repo1)}/ or installed locally. +Could not find gem 'rack (= 2)' in locally installed gems. The source contains the following versions of 'rack': 0.9.1, 1.0.0 Run `bundle install` to install missing gems. EOS @@ -863,7 +863,7 @@ Run `bundle install` to install missing gems. let(:exit_code) { Bundler::GemNotFound.new.status_code } let(:expected) { "" } let(:expected_err) { <<-EOS.strip } -Could not find gem 'rack (= 2)' in rubygems repository #{file_uri_for(gem_repo1)}/ or installed locally. +Could not find gem 'rack (= 2)' in locally installed gems. The source contains the following versions of 'rack': 1.0.0 Run `bundle install` to install missing gems. EOS diff --git a/spec/bundler/commands/lock_spec.rb b/spec/bundler/commands/lock_spec.rb index 21eb6e5456..171ec1ba1d 100644 --- a/spec/bundler/commands/lock_spec.rb +++ b/spec/bundler/commands/lock_spec.rb @@ -86,7 +86,7 @@ RSpec.describe "bundle lock" do it "does not fetch remote specs when using the --local option" do bundle "lock --update --local", :raise_on_error => false - expect(err).to match(/installed locally/) + expect(err).to match(/locally installed gems/) end it "works with --gemfile flag" do diff --git a/spec/bundler/support/indexes.rb b/spec/bundler/support/indexes.rb index bf4300edb2..91dd699b5f 100644 --- a/spec/bundler/support/indexes.rb +++ b/spec/bundler/support/indexes.rb @@ -17,7 +17,7 @@ module Spec def resolve(args = []) @platforms ||= ["ruby"] deps = [] - default_source = instance_double("Bundler::Source::Rubygems", :specs => @index) + default_source = instance_double("Bundler::Source::Rubygems", :specs => @index, :to_err => "locally install gems") source_requirements = { :default => default_source } @deps.each do |d| source_requirements[d.name] = d.source = default_source