2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "dependency"
|
|
|
|
require_relative "exceptions"
|
|
|
|
require_relative "util/list"
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12: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 10:52:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
class Gem::Resolver
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "resolver/molinillo"
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-25 23:14:49 +04: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-14 07:30:02 +04:00
|
|
|
# Set to true if all development dependencies should be considered.
|
2013-11-25 23:14:49 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :development
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
##
|
|
|
|
# Set to true if immediate development dependencies should be considered.
|
|
|
|
|
|
|
|
attr_accessor :development_shallow
|
|
|
|
|
2013-12-18 05:02:58 +04:00
|
|
|
##
|
|
|
|
# When true, no dependencies are looked up for requested gems.
|
|
|
|
|
|
|
|
attr_accessor :ignore_dependencies
|
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
##
|
|
|
|
# List of dependencies that could not be found in the configured sources.
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_reader :missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-12-08 05:22:39 +04:00
|
|
|
attr_reader :stats
|
|
|
|
|
2014-09-14 07: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 12:59:02 +04:00
|
|
|
##
|
|
|
|
# When a missing dependency, don't stop. Just go on and record what was
|
|
|
|
# missing.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :soft_missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-22 03:27:30 +04: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 13:20:47 +03:00
|
|
|
def self.compose_sets(*sets)
|
2013-11-10 21:51:40 +04:00
|
|
|
sets.compact!
|
|
|
|
|
2013-11-22 03:27:30 +04:00
|
|
|
sets = sets.map do |set|
|
|
|
|
case set
|
2014-02-04 04:48:31 +04:00
|
|
|
when Gem::Resolver::BestSet then
|
|
|
|
set
|
2013-11-22 03:27:30 +04:00
|
|
|
when Gem::Resolver::ComposedSet then
|
|
|
|
set.sets
|
|
|
|
else
|
|
|
|
set
|
|
|
|
end
|
|
|
|
end.flatten
|
|
|
|
|
2013-11-10 21:51:40 +04: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-19 04:34:13 +04:00
|
|
|
Gem::Resolver::ComposedSet.new(*sets)
|
2013-11-10 21:51:40 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2013-11-25 23:14:49 +04:00
|
|
|
# Creates a Resolver that queries only against the already installed gems
|
|
|
|
# for the +needed+ dependencies.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.for_current_gems(needed)
|
2013-11-19 04:34:13 +04:00
|
|
|
new needed, Gem::Resolver::CurrentSet.new
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2013-11-19 04:34:13 +04:00
|
|
|
# Create Resolver object which will resolve the tree starting
|
2013-10-18 04:41:36 +04:00
|
|
|
# with +needed+ Dependency objects.
|
2012-11-29 10:52:18 +04:00
|
|
|
#
|
2013-09-14 12:59:02 +04:00
|
|
|
# +set+ is an object that provides where to look for specifications to
|
2013-10-18 04:41:36 +04:00
|
|
|
# satisfy the Dependencies. This defaults to IndexSet, which will query
|
2013-09-14 12:59:02 +04:00
|
|
|
# rubygems.org.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def initialize(needed, set = nil)
|
2013-11-19 04:34:13 +04:00
|
|
|
@set = set || Gem::Resolver::IndexSet.new
|
2013-09-14 12:59:02 +04:00
|
|
|
@needed = needed
|
|
|
|
|
2013-12-18 05:02:58 +04:00
|
|
|
@development = false
|
2014-09-14 07:30:02 +04:00
|
|
|
@development_shallow = false
|
2013-12-18 05:02:58 +04:00
|
|
|
@ignore_dependencies = false
|
|
|
|
@missing = []
|
2014-09-14 07:30:02 +04:00
|
|
|
@skip_gems = {}
|
2013-12-18 05:02:58 +04:00
|
|
|
@soft_missing = false
|
|
|
|
@stats = Gem::Resolver::Stats.new
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def explain(stage, *data) # :nodoc:
|
2014-09-14 07:30:02 +04:00
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
2023-03-16 06:57:49 +03:00
|
|
|
d = data.map(&:pretty_inspect).join(", ")
|
2014-09-14 07:30:02 +04:00
|
|
|
$stderr.printf "%10s %s\n", stage.to_s.upcase, d
|
2013-11-19 04:34:13 +04:00
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def explain_list(stage) # :nodoc:
|
2014-09-14 07:30:02 +04:00
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
|
|
|
data = yield
|
|
|
|
$stderr.printf "%10s (%d entries)\n", stage.to_s.upcase, data.size
|
2019-08-30 04:18:32 +03:00
|
|
|
unless data.empty?
|
|
|
|
require "pp"
|
|
|
|
PP.pp data, $stderr
|
|
|
|
end
|
2013-11-22 03:27:30 +04:00
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Creates an ActivationRequest for the given +dep+ and the last +possible+
|
|
|
|
# specification.
|
|
|
|
#
|
|
|
|
# Returns the Specification and the ActivationRequest
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def activation_request(dep, possible) # :nodoc:
|
2013-11-10 21:51:40 +04:00
|
|
|
spec = possible.pop
|
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
explain :activate, [spec.full_name, possible.size]
|
2014-09-14 07:30:02 +04:00
|
|
|
explain :possible, possible
|
2013-11-19 04:34:13 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
activation_request =
|
2013-11-19 04:34:13 +04:00
|
|
|
Gem::Resolver::ActivationRequest.new spec, dep, possible
|
2013-11-10 21:51:40 +04:00
|
|
|
|
2023-03-23 04:16:17 +03:00
|
|
|
[spec, activation_request]
|
2013-11-10 21:51:40 +04:00
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def requests(s, act, reqs=[]) # :nodoc:
|
2013-12-18 05:02:58 +04:00
|
|
|
return reqs if @ignore_dependencies
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
s.fetch_development_dependencies if @development
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
s.dependencies.reverse_each do |d|
|
2022-08-09 05:16:07 +03:00
|
|
|
next if d.type == :development && !@development
|
|
|
|
next if d.type == :development && @development_shallow &&
|
2014-09-14 07:30:02 +04:00
|
|
|
act.development?
|
2022-08-09 05:16:07 +03:00
|
|
|
next if d.type == :development && @development_shallow &&
|
2014-09-14 07:30:02 +04:00
|
|
|
act.parent
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
reqs << Gem::Resolver::DependencyRequest.new(d, act)
|
2013-12-08 05:22:39 +04:00
|
|
|
@stats.requirement!
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
@set.prefetch reqs
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-12-08 05:22:39 +04:00
|
|
|
@stats.record_requirements reqs
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
reqs
|
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
include Molinillo::UI
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def output
|
2018-05-30 16:01:35 +03:00
|
|
|
@output ||= debug? ? $stdout : File.open(IO::NULL, "w")
|
2015-07-02 00:50:14 +03:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def debug?
|
|
|
|
DEBUG_RESOLVER
|
|
|
|
end
|
2013-12-08 05:22:39 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
include Molinillo::SpecificationProvider
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
##
|
|
|
|
# Proceed with resolution! Returns an array of ActivationRequest objects.
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def resolve
|
|
|
|
locking_dg = Molinillo::DependencyGraph.new
|
2020-06-10 20:46:05 +03:00
|
|
|
Molinillo::Resolver.new(self, self).resolve(@needed.map {|d| DependencyRequest.new d, nil }, locking_dg).tsort.map(&:payload).compact
|
2015-07-02 00:50:14 +03: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 09:09:09 +03:00
|
|
|
ensure
|
2022-08-09 05:16:07 +03:00
|
|
|
@output.close if defined?(@output) && !debug?
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-09-25 04:53:19 +04:00
|
|
|
##
|
2013-11-10 21:51:40 +04: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-25 04:53:19 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def find_possible(dependency) # :nodoc:
|
2013-11-10 21:51:40 +04:00
|
|
|
all = @set.find_all dependency
|
2014-09-14 07: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 20:46:05 +03:00
|
|
|
skip_dep_gems.any? {|s| api_spec.version == s.version }
|
2014-09-14 07:30:02 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
all = matching unless matching.empty?
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
matching_platform = select_local_platforms all
|
|
|
|
|
2023-03-23 04:16:17 +03:00
|
|
|
[matching_platform, all]
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2015-07-02 00:50:14 +03:00
|
|
|
# Returns the gems in +specs+ that match the local platform.
|
2013-09-19 01:39:43 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def select_local_platforms(specs) # :nodoc:
|
2015-07-02 00:50:14 +03:00
|
|
|
specs.select do |spec|
|
|
|
|
Gem::Platform.installable? spec
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2015-07-02 00:50:14 +03: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-25 04:53:19 +04:00
|
|
|
end
|
2017-10-08 04:32:18 +03:00
|
|
|
|
2020-06-10 20:46:05 +03:00
|
|
|
groups = Hash.new {|hash, key| hash[key] = [] }
|
2017-10-08 04:32:18 +03:00
|
|
|
|
|
|
|
# create groups & sources in the same loop
|
2019-02-14 15:59:03 +03:00
|
|
|
sources = possibles.map do |spec|
|
2017-10-08 04:32:18 +03:00
|
|
|
source = spec.source
|
|
|
|
groups[source] << spec
|
|
|
|
source
|
2019-02-14 15:59:03 +03:00
|
|
|
end.uniq.reverse
|
2017-10-08 04:32:18 +03:00
|
|
|
|
|
|
|
activation_requests = []
|
|
|
|
|
|
|
|
sources.each do |source|
|
|
|
|
groups[source].
|
2022-09-08 00:01:03 +03:00
|
|
|
sort_by {|spec| [spec.version, spec.platform =~ Gem::Platform.local ? 1 : 0] }.
|
2020-06-10 20:46:05 +03:00
|
|
|
map {|spec| ActivationRequest.new spec, dependency }.
|
|
|
|
each {|activation_request| activation_requests << activation_request }
|
2017-10-08 04:32:18 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
activation_requests
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def dependencies_for(specification)
|
|
|
|
return [] if @ignore_dependencies
|
|
|
|
spec = specification.spec
|
|
|
|
requests(spec, specification)
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def requirement_satisfied_by?(requirement, activated, spec)
|
2020-12-23 02:45:19 +03: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 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def name_for(dependency)
|
|
|
|
dependency.name
|
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def allow_missing?(dependency)
|
|
|
|
@missing << dependency
|
|
|
|
@soft_missing
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2017-03-17 04:29:22 +03:00
|
|
|
def sort_dependencies(dependencies, activated, conflicts)
|
2017-08-04 17:57:31 +03:00
|
|
|
dependencies.sort_by.with_index do |dependency, i|
|
2017-03-17 04:29:22 +03: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 10:33:39 +03:00
|
|
|
i, # for stable sort
|
2017-03-17 04:29:22 +03: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 23:58:57 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "resolver/activation_request"
|
|
|
|
require_relative "resolver/conflict"
|
|
|
|
require_relative "resolver/dependency_request"
|
|
|
|
require_relative "resolver/requirement_list"
|
|
|
|
require_relative "resolver/stats"
|
2022-07-17 11:08:51 +03:00
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "resolver/set"
|
|
|
|
require_relative "resolver/api_set"
|
|
|
|
require_relative "resolver/composed_set"
|
|
|
|
require_relative "resolver/best_set"
|
|
|
|
require_relative "resolver/current_set"
|
|
|
|
require_relative "resolver/git_set"
|
|
|
|
require_relative "resolver/index_set"
|
|
|
|
require_relative "resolver/installer_set"
|
|
|
|
require_relative "resolver/lock_set"
|
|
|
|
require_relative "resolver/vendor_set"
|
|
|
|
require_relative "resolver/source_set"
|
2022-07-17 11:08:51 +03:00
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "resolver/specification"
|
|
|
|
require_relative "resolver/spec_specification"
|
|
|
|
require_relative "resolver/api_specification"
|
|
|
|
require_relative "resolver/git_specification"
|
|
|
|
require_relative "resolver/index_specification"
|
|
|
|
require_relative "resolver/installed_specification"
|
|
|
|
require_relative "resolver/local_specification"
|
|
|
|
require_relative "resolver/lock_specification"
|
|
|
|
require_relative "resolver/vendor_specification"
|