2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/dependency'
|
|
|
|
require 'rubygems/exceptions'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'rubygems/util/list'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
|
|
|
# Given a set of Gem::Dependency objects as +needed+ and a way to query the
|
|
|
|
# set of available specs via +set+, calculates a set of ActivationRequest
|
|
|
|
# objects which indicate all the specs that should be activated to meet the
|
|
|
|
# all the requirements.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
class Gem::Resolver
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'rubygems/resolver/molinillo'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# If the DEBUG_RESOLVER environment variable is set then debugging mode is
|
|
|
|
# enabled for the resolver. This will display information about the state
|
|
|
|
# of the resolver while a set of dependencies is being resolved.
|
|
|
|
|
|
|
|
DEBUG_RESOLVER = !ENV['DEBUG_RESOLVER'].nil?
|
|
|
|
|
|
|
|
##
|
2014-09-13 23:30:02 -04:00
|
|
|
# Set to true if all development dependencies should be considered.
|
2013-11-25 14:14:49 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_accessor :development
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
##
|
|
|
|
# Set to true if immediate development dependencies should be considered.
|
|
|
|
|
|
|
|
attr_accessor :development_shallow
|
|
|
|
|
2013-12-17 20:02:58 -05:00
|
|
|
##
|
|
|
|
# When true, no dependencies are looked up for requested gems.
|
|
|
|
|
|
|
|
attr_accessor :ignore_dependencies
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# List of dependencies that could not be found in the configured sources.
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_reader :missing
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-12-07 20:22:39 -05:00
|
|
|
attr_reader :stats
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
##
|
|
|
|
# Hash of gems to skip resolution. Keyed by gem name, with arrays of
|
|
|
|
# gem specifications as values.
|
|
|
|
|
|
|
|
attr_accessor :skip_gems
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
|
|
|
# When a missing dependency, don't stop. Just go on and record what was
|
|
|
|
# missing.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_accessor :soft_missing
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Combines +sets+ into a ComposedSet that allows specification lookup in a
|
|
|
|
# uniform manner. If one of the +sets+ is itself a ComposedSet its sets are
|
|
|
|
# flattened into the result ComposedSet.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.compose_sets(*sets)
|
2013-11-10 12:51:40 -05:00
|
|
|
sets.compact!
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
sets = sets.map do |set|
|
|
|
|
case set
|
2014-02-03 19:48:31 -05:00
|
|
|
when Gem::Resolver::BestSet then
|
|
|
|
set
|
2013-11-21 18:27:30 -05:00
|
|
|
when Gem::Resolver::ComposedSet then
|
|
|
|
set.sets
|
|
|
|
else
|
|
|
|
set
|
|
|
|
end
|
|
|
|
end.flatten
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
case sets.length
|
|
|
|
when 0 then
|
|
|
|
raise ArgumentError, 'one set in the composition must be non-nil'
|
|
|
|
when 1 then
|
|
|
|
sets.first
|
|
|
|
else
|
2013-11-18 19:34:13 -05:00
|
|
|
Gem::Resolver::ComposedSet.new(*sets)
|
2013-11-10 12:51:40 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2013-11-25 14:14:49 -05:00
|
|
|
# Creates a Resolver that queries only against the already installed gems
|
|
|
|
# for the +needed+ dependencies.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.for_current_gems(needed)
|
2013-11-18 19:34:13 -05:00
|
|
|
new needed, Gem::Resolver::CurrentSet.new
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2013-11-18 19:34:13 -05:00
|
|
|
# Create Resolver object which will resolve the tree starting
|
2013-10-17 20:41:36 -04:00
|
|
|
# with +needed+ Dependency objects.
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
2013-09-14 04:59:02 -04:00
|
|
|
# +set+ is an object that provides where to look for specifications to
|
2013-10-17 20:41:36 -04:00
|
|
|
# satisfy the Dependencies. This defaults to IndexSet, which will query
|
2013-09-14 04:59:02 -04:00
|
|
|
# rubygems.org.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def initialize(needed, set = nil)
|
2013-11-18 19:34:13 -05:00
|
|
|
@set = set || Gem::Resolver::IndexSet.new
|
2013-09-14 04:59:02 -04:00
|
|
|
@needed = needed
|
|
|
|
|
2013-12-17 20:02:58 -05:00
|
|
|
@development = false
|
2014-09-13 23:30:02 -04:00
|
|
|
@development_shallow = false
|
2013-12-17 20:02:58 -05:00
|
|
|
@ignore_dependencies = false
|
|
|
|
@missing = []
|
2014-09-13 23:30:02 -04:00
|
|
|
@skip_gems = {}
|
2013-12-17 20:02:58 -05:00
|
|
|
@soft_missing = false
|
|
|
|
@stats = Gem::Resolver::Stats.new
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def explain(stage, *data) # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
d = data.map {|x| x.pretty_inspect }.join(", ")
|
2014-09-13 23:30:02 -04:00
|
|
|
$stderr.printf "%10s %s\n", stage.to_s.upcase, d
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def explain_list(stage) # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
|
|
|
data = yield
|
|
|
|
$stderr.printf "%10s (%d entries)\n", stage.to_s.upcase, data.size
|
2019-08-29 21:18:32 -04:00
|
|
|
unless data.empty?
|
|
|
|
require 'pp'
|
|
|
|
PP.pp data, $stderr
|
|
|
|
end
|
2013-11-21 18:27:30 -05:00
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
|
|
|
# Creates an ActivationRequest for the given +dep+ and the last +possible+
|
|
|
|
# specification.
|
|
|
|
#
|
|
|
|
# Returns the Specification and the ActivationRequest
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def activation_request(dep, possible) # :nodoc:
|
2013-11-10 12:51:40 -05:00
|
|
|
spec = possible.pop
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
explain :activate, [spec.full_name, possible.size]
|
2014-09-13 23:30:02 -04:00
|
|
|
explain :possible, possible
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
activation_request =
|
2013-11-18 19:34:13 -05:00
|
|
|
Gem::Resolver::ActivationRequest.new spec, dep, possible
|
2013-11-10 12:51:40 -05:00
|
|
|
|
|
|
|
return spec, activation_request
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def requests(s, act, reqs=[]) # :nodoc:
|
2013-12-17 20:02:58 -05:00
|
|
|
return reqs if @ignore_dependencies
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
s.fetch_development_dependencies if @development
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
s.dependencies.reverse_each do |d|
|
|
|
|
next if d.type == :development and not @development
|
2014-09-13 23:30:02 -04:00
|
|
|
next if d.type == :development and @development_shallow and
|
|
|
|
act.development?
|
|
|
|
next if d.type == :development and @development_shallow and
|
|
|
|
act.parent
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
reqs << Gem::Resolver::DependencyRequest.new(d, act)
|
2013-12-07 20:22:39 -05:00
|
|
|
@stats.requirement!
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
@set.prefetch reqs
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-12-07 20:22:39 -05:00
|
|
|
@stats.record_requirements reqs
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
reqs
|
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
include Molinillo::UI
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def output
|
2018-05-30 09:01:35 -04:00
|
|
|
@output ||= debug? ? $stdout : File.open(IO::NULL, 'w')
|
2015-07-01 17:50:14 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def debug?
|
|
|
|
DEBUG_RESOLVER
|
|
|
|
end
|
2013-12-07 20:22:39 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
include Molinillo::SpecificationProvider
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
##
|
|
|
|
# Proceed with resolution! Returns an array of ActivationRequest objects.
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def resolve
|
|
|
|
locking_dg = Molinillo::DependencyGraph.new
|
2020-06-10 13:46:05 -04:00
|
|
|
Molinillo::Resolver.new(self, self).resolve(@needed.map {|d| DependencyRequest.new d, nil }, locking_dg).tsort.map(&:payload).compact
|
2015-07-01 17:50:14 -04:00
|
|
|
rescue Molinillo::VersionConflict => e
|
|
|
|
conflict = e.conflicts.values.first
|
|
|
|
raise Gem::DependencyResolutionError, Conflict.new(conflict.requirement_trees.first.first, conflict.existing, conflict.requirement)
|
2015-07-08 02:09:09 -04:00
|
|
|
ensure
|
2016-03-27 22:26:39 -04:00
|
|
|
@output.close if defined?(@output) and !debug?
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-09-24 20:53:19 -04:00
|
|
|
##
|
2013-11-10 12:51:40 -05:00
|
|
|
# Extracts the specifications that may be able to fulfill +dependency+ and
|
|
|
|
# returns those that match the local platform and all those that match.
|
2013-09-24 20:53:19 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def find_possible(dependency) # :nodoc:
|
2013-11-10 12:51:40 -05:00
|
|
|
all = @set.find_all dependency
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
if (skip_dep_gems = skip_gems[dependency.name]) && !skip_dep_gems.empty?
|
|
|
|
matching = all.select do |api_spec|
|
2020-06-10 13:46:05 -04:00
|
|
|
skip_dep_gems.any? {|s| api_spec.version == s.version }
|
2014-09-13 23:30:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
all = matching unless matching.empty?
|
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
matching_platform = select_local_platforms all
|
|
|
|
|
|
|
|
return matching_platform, all
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2015-07-01 17:50:14 -04:00
|
|
|
# Returns the gems in +specs+ that match the local platform.
|
2013-09-18 17:39:43 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def select_local_platforms(specs) # :nodoc:
|
2015-07-01 17:50:14 -04:00
|
|
|
specs.select do |spec|
|
|
|
|
Gem::Platform.installable? spec
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def search_for(dependency)
|
|
|
|
possibles, all = find_possible(dependency)
|
|
|
|
if !@soft_missing && possibles.empty?
|
|
|
|
@missing << dependency
|
|
|
|
exc = Gem::UnsatisfiableDependencyError.new dependency, all
|
|
|
|
exc.errors = @set.errors
|
|
|
|
raise exc
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
2017-10-07 21:32:18 -04:00
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
groups = Hash.new {|hash, key| hash[key] = [] }
|
2017-10-07 21:32:18 -04:00
|
|
|
|
|
|
|
# create groups & sources in the same loop
|
2019-02-14 07:59:03 -05:00
|
|
|
sources = possibles.map do |spec|
|
2017-10-07 21:32:18 -04:00
|
|
|
source = spec.source
|
|
|
|
groups[source] << spec
|
|
|
|
source
|
2019-02-14 07:59:03 -05:00
|
|
|
end.uniq.reverse
|
2017-10-07 21:32:18 -04:00
|
|
|
|
|
|
|
activation_requests = []
|
|
|
|
|
|
|
|
sources.each do |source|
|
|
|
|
groups[source].
|
2020-06-10 13:46:05 -04:00
|
|
|
sort_by {|spec| [spec.version, Gem::Platform.local =~ spec.platform ? 1 : 0] }.
|
|
|
|
map {|spec| ActivationRequest.new spec, dependency }.
|
|
|
|
each {|activation_request| activation_requests << activation_request }
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
activation_requests
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def dependencies_for(specification)
|
|
|
|
return [] if @ignore_dependencies
|
|
|
|
spec = specification.spec
|
|
|
|
requests(spec, specification)
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def requirement_satisfied_by?(requirement, activated, spec)
|
2020-12-22 18:45:19 -05:00
|
|
|
matches_spec = requirement.matches_spec? spec
|
|
|
|
return matches_spec if @soft_missing
|
|
|
|
|
|
|
|
matches_spec &&
|
|
|
|
spec.spec.required_ruby_version.satisfied_by?(Gem.ruby_version) &&
|
|
|
|
spec.spec.required_rubygems_version.satisfied_by?(Gem.rubygems_version)
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def name_for(dependency)
|
|
|
|
dependency.name
|
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def allow_missing?(dependency)
|
|
|
|
@missing << dependency
|
|
|
|
@soft_missing
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2017-03-16 21:29:22 -04:00
|
|
|
def sort_dependencies(dependencies, activated, conflicts)
|
2017-08-04 10:57:31 -04:00
|
|
|
dependencies.sort_by.with_index do |dependency, i|
|
2017-03-16 21:29:22 -04:00
|
|
|
name = name_for(dependency)
|
|
|
|
[
|
|
|
|
activated.vertex_named(name).payload ? 0 : 1,
|
|
|
|
amount_constrained(dependency),
|
|
|
|
conflicts[name] ? 0 : 1,
|
|
|
|
activated.vertex_named(name).payload ? 0 : search_for(dependency).count,
|
2020-12-08 02:33:39 -05:00
|
|
|
i, # for stable sort
|
2017-03-16 21:29:22 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
SINGLE_POSSIBILITY_CONSTRAINT_PENALTY = 1_000_000
|
|
|
|
private_constant :SINGLE_POSSIBILITY_CONSTRAINT_PENALTY if defined?(private_constant)
|
|
|
|
|
|
|
|
# returns an integer \in (-\infty, 0]
|
|
|
|
# a number closer to 0 means the dependency is less constraining
|
|
|
|
#
|
|
|
|
# dependencies w/ 0 or 1 possibilities (ignoring version requirements)
|
|
|
|
# are given very negative values, so they _always_ sort first,
|
|
|
|
# before dependencies that are unconstrained
|
|
|
|
def amount_constrained(dependency)
|
|
|
|
@amount_constrained ||= {}
|
|
|
|
@amount_constrained[dependency.name] ||= begin
|
|
|
|
name_dependency = Gem::Dependency.new(dependency.name)
|
|
|
|
dependency_request_for_name = Gem::Resolver::DependencyRequest.new(name_dependency, dependency.requester)
|
|
|
|
all = @set.find_all(dependency_request_for_name).size
|
|
|
|
|
|
|
|
if all <= 1
|
|
|
|
all - SINGLE_POSSIBILITY_CONSTRAINT_PENALTY
|
|
|
|
else
|
|
|
|
search = search_for(dependency).size
|
|
|
|
search - all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :amount_constrained
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
require 'rubygems/resolver/activation_request'
|
|
|
|
require 'rubygems/resolver/conflict'
|
|
|
|
require 'rubygems/resolver/dependency_request'
|
|
|
|
require 'rubygems/resolver/requirement_list'
|
2013-12-07 20:22:39 -05:00
|
|
|
require 'rubygems/resolver/stats'
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
require 'rubygems/resolver/set'
|
|
|
|
require 'rubygems/resolver/api_set'
|
|
|
|
require 'rubygems/resolver/composed_set'
|
|
|
|
require 'rubygems/resolver/best_set'
|
|
|
|
require 'rubygems/resolver/current_set'
|
|
|
|
require 'rubygems/resolver/git_set'
|
|
|
|
require 'rubygems/resolver/index_set'
|
|
|
|
require 'rubygems/resolver/installer_set'
|
|
|
|
require 'rubygems/resolver/lock_set'
|
|
|
|
require 'rubygems/resolver/vendor_set'
|
2016-02-01 07:43:26 -05:00
|
|
|
require 'rubygems/resolver/source_set'
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
require 'rubygems/resolver/specification'
|
|
|
|
require 'rubygems/resolver/spec_specification'
|
|
|
|
require 'rubygems/resolver/api_specification'
|
|
|
|
require 'rubygems/resolver/git_specification'
|
|
|
|
require 'rubygems/resolver/index_specification'
|
|
|
|
require 'rubygems/resolver/installed_specification'
|
2013-11-25 14:14:49 -05:00
|
|
|
require 'rubygems/resolver/local_specification'
|
2013-12-07 20:22:39 -05:00
|
|
|
require 'rubygems/resolver/lock_specification'
|
2013-11-18 19:34:13 -05:00
|
|
|
require 'rubygems/resolver/vendor_specification'
|