Skip to content

Commit

Permalink
Improve "gem not found in source" errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deivid-rodriguez committed Jul 31, 2021
1 parent 9fd39bd commit 30eb14f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bundler/lib/bundler/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def verify_gemfile_dependencies_are_found!(requirements)
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
Expand Down Expand Up @@ -371,7 +371,7 @@ def version_conflict_message(e)
o << if metadata_requirement
"is not available in #{relevant_source}"
else
"in #{relevant_source}.\n"
"in #{relevant_source.to_err}.\n"
end
end
end,
Expand Down
4 changes: 4 additions & 0 deletions bundler/lib/bundler/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def inspect
"#<#{self.class}:0x#{object_id} #{self}>"
end

def to_err
to_s
end

def path?
instance_of?(Bundler::Source::Path)
end
Expand Down
17 changes: 16 additions & 1 deletion bundler/lib/bundler/source/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ def to_lock
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
Expand Down Expand Up @@ -319,6 +330,10 @@ def dependency_api_available?

protected

def remote_names
remotes.map(&:to_s).join(", ")
end

def credless_remotes
remotes.map(&method(:suppress_configured_credentials))
end
Expand Down
4 changes: 4 additions & 0 deletions bundler/lib/bundler/source/rubygems_aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def specs
@index
end

def to_err
to_s
end

def to_s
"any of the sources"
end
Expand Down
4 changes: 2 additions & 2 deletions bundler/spec/commands/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def bin_path(a,b,c)
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
Expand All @@ -863,7 +863,7 @@ def bin_path(a,b,c)
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
Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/commands/lock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def read_lockfile(file = "Gemfile.lock")
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
Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/support/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def platform(*args)
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
Expand Down

0 comments on commit 30eb14f

Please sign in to comment.