Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Make version conflict messages better
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Aug 7, 2018
1 parent 017cc4d commit 32222b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
40 changes: 32 additions & 8 deletions lib/bundler/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,23 +302,34 @@ def formatted_versions_with_platforms(versions_with_platforms)
end

def version_conflict_message(e)
# only show essential conflicts, if possible
conflicts = e.conflicts.dup
conflicts.delete_if do |_name, conflict|
deps = conflict.requirement_trees.map(&:last).flatten(1)
!Bundler::VersionRanges.empty?(*Bundler::VersionRanges.for_many(deps.map(&:requirement)))
end
e.conflicts.replace(conflicts) unless conflicts.empty?

solver_name = "Bundler"
possibility_type = "gem"
e.message_with_trees(
:solver_name => "Bundler",
:possibility_type => "gem",
:solver_name => solver_name,
:possibility_type => possibility_type,
:reduce_trees => lambda do |trees|
# called first, because we want to reduce the amount of work required to find maximal empty sets
trees.uniq! {|t| t.flatten.map {|dep| [dep.name, dep.requirement] } }

# bail out if tree size is too big for Array#combination to make any sense
return trees if trees.size > 15
maximal = 1.upto(trees.size).map do |size|
trees.map(&:last).flatten(1).combination(size).to_a
end.flatten(1).select do |deps|
Bundler::VersionRanges.empty?(*Bundler::VersionRanges.for_many(deps.map(&:requirement)))
end.min_by(&:size)
trees.reject! {|t| !maximal.include?(t.last) } if maximal

trees = trees.sort_by {|t| t.flatten.map(&:to_s) }
trees.uniq! {|t| t.flatten.map {|dep| [dep.name, dep.requirement] } }
trees.reject! {|t| !maximal.include?(t.last) } if maximal

trees.sort_by {|t| t.reverse.map(&:name) }
trees.sort_by {|t| [t.flatten.map(&:to_s), t.reverse.map(&:name)] }
end,
:printable_requirement => lambda {|req| SharedHelpers.pretty_dependency(req) },
:additional_message_for_conflict => lambda do |o, name, conflict|
Expand Down Expand Up @@ -351,7 +362,11 @@ def version_conflict_message(e)
[]
end.compact.map(&:to_s).uniq.sort

o << "Could not find gem '#{SharedHelpers.pretty_dependency(conflict.requirement)}'"
metadata_requirement = name.end_with?("\0")

o << "Could not find gem '" unless metadata_requirement
o << SharedHelpers.pretty_dependency(conflict.requirement)
o << "'" unless metadata_requirement
if conflict.requirement_trees.first.size > 1
o << ", which is required by "
o << "gem '#{SharedHelpers.pretty_dependency(conflict.requirement_trees.first[-2])}',"
Expand All @@ -360,12 +375,21 @@ def version_conflict_message(e)

o << if relevant_sources.empty?
"in any of the sources.\n"
elsif metadata_requirement
"is not available in #{relevant_sources.join(" or ")}"
else
"in any of the relevant sources:\n #{relevant_sources * "\n "}\n"
end
end
end,
:version_for_spec => lambda {|spec| spec.version }
:version_for_spec => lambda {|spec| spec.version },
:incompatible_version_message_for_conflict => lambda do |name, _conflict|
if name.end_with?("\0")
%(#{solver_name} found conflicting requirements for the #{name} version:)
else
%(#{solver_name} could not find compatible versions for #{possibility_type} "#{name}":)
end
end
)
end

Expand Down
5 changes: 2 additions & 3 deletions spec/install/gems/resolving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,14 @@
expect(out).to_not include("Gem::InstallError: require_ruby requires Ruby version > 9000")

nice_error = strip_whitespace(<<-E).strip
Bundler could not find compatible versions for gem "ruby\0":
Bundler found conflicting requirements for the ruby\0 version:
In Gemfile:
ruby\0 (#{error_message_requirement})
require_ruby was resolved to 1.0, which depends on
ruby\0 (> 9000)
Could not find gem 'ruby\0 (> 9000)', which is required by gem 'require_ruby', in any of the relevant sources:
the local ruby installation
ruby\0 (> 9000), which is required by gem 'require_ruby', is not available in the local ruby installation
E
expect(last_command.bundler_err).to end_with(nice_error)
end
Expand Down

0 comments on commit 32222b5

Please sign in to comment.