In Files

  • bundler/resolver.rb
  • bundler/resolver/spec_group.rb

Class/Module Index [+]

Quicksearch

Bundler::Resolver

Public Class Methods

new(source_requirements, base, gem_version_promoter, additional_base_requirements, platforms) click to toggle source
 
               # File bundler/resolver.rb, line 27
def initialize(source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
  @source_requirements = source_requirements

  @index_requirements = source_requirements.each_with_object({}) do |source_requirement, index_requirements|
    name, source = source_requirement
    index_requirements[name] = name == :global ? source : source.specs
  end

  @base = base
  @resolver = Molinillo::Resolver.new(self, self)
  @search_for = {}
  @base_dg = Molinillo::DependencyGraph.new
  @base.each do |ls|
    dep = Dependency.new(ls.name, ls.version)
    @base_dg.add_vertex(ls.name, DepProxy.get_proxy(dep, ls.platform), true)
  end
  additional_base_requirements.each {|d| @base_dg.add_vertex(d.name, d) }
  @platforms = platforms.reject {|p| p != Gem::Platform::RUBY && (platforms - [p]).any? {|pl| generic(pl) == p } }
  @resolving_only_for_ruby = platforms == [Gem::Platform::RUBY]
  @gem_version_promoter = gem_version_promoter
  @use_gvp = Bundler.feature_flag.use_gem_version_promoter_for_major_updates? || !@gem_version_promoter.major?
  @no_aggregate_global_source = @source_requirements[:global].nil?

  @variant_specific_names = []
  @generic_names = ["Ruby\0", "RubyGems\0"]
end
            
platform_sort_key(platform) click to toggle source
 
               # File bundler/resolver.rb, line 272
def self.platform_sort_key(platform)
  # Prefer specific platform to not specific platform
  return ["99-LAST", "", "", ""] if Gem::Platform::RUBY == platform
  ["00", *platform.to_a.map {|part| part || "" }]
end
            
resolve(requirements, source_requirements = {}, base = [], gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [], platforms = nil) click to toggle source

Figures out the best possible configuration of gems that satisfies the list of passed dependencies and any child dependencies without causing any gem activation errors.

Parameters

*dependencies<Gem::Dependency>

The list of dependencies to resolve

Returns

<GemBundle>,nil

If the list of dependencies can be resolved, a

collection of gemspecs is returned. Otherwise, nil is returned.
 
               # File bundler/resolver.rb, line 20
def self.resolve(requirements, source_requirements = {}, base = [], gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [], platforms = nil)
  base = SpecSet.new(base) unless base.is_a?(SpecSet)
  resolver = new(source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
  result = resolver.start(requirements)
  SpecSet.new(result)
end
            

Public Instance Methods

after_resolution() click to toggle source
 
               # File bundler/resolver.rb, line 104
def after_resolution
  Bundler.ui.info ""
end
            
before_resolution() click to toggle source
 
               # File bundler/resolver.rb, line 100
def before_resolution
  Bundler.ui.info "Resolving dependencies...", debug?
end
            
debug(depth = 0) click to toggle source

Conveys debug information to the user.

@param [Integer] depth the current depth of the resolution process. @return [void]

 
               # File bundler/resolver.rb, line 83
def debug(depth = 0)
  return unless debug?
  debug_info = yield
  debug_info = debug_info.inspect unless debug_info.is_a?(String)
  puts debug_info.split("\n").map {|s| depth == 0 ? "BUNDLER: #{s}" : "BUNDLER(#{depth}): #{s}" }
end
            
debug?() click to toggle source
 
               # File bundler/resolver.rb, line 90
def debug?
  return @debug_mode if defined?(@debug_mode)
  @debug_mode =
    ENV["BUNDLER_DEBUG_RESOLVER"] ||
    ENV["BUNDLER_DEBUG_RESOLVER_TREE"] ||
    ENV["DEBUG_RESOLVER"] ||
    ENV["DEBUG_RESOLVER_TREE"] ||
    false
end
            
dependencies_equal?(dependencies, other_dependencies) click to toggle source
 
               # File bundler/resolver.rb, line 239
def dependencies_equal?(dependencies, other_dependencies)
  dependencies.map(&:dep) == other_dependencies.map(&:dep)
end
            
dependencies_for(specification) click to toggle source
 
               # File bundler/resolver.rb, line 114
def dependencies_for(specification)
  all_dependencies = specification.dependencies_for_activated_platforms

  if @variant_specific_names.include?(specification.name)
    @variant_specific_names |= all_dependencies.map(&:name) - @generic_names
  else
    generic_names, variant_specific_names = specification.partitioned_dependency_names_for_activated_platforms
    @variant_specific_names |= variant_specific_names - @generic_names
    @generic_names |= generic_names
  end

  all_dependencies
end
            
index_for(dependency) click to toggle source
 
               # File bundler/resolver.rb, line 202
def index_for(dependency)
  source = @index_requirements[dependency.name]
  if source
    source
  elsif @no_aggregate_global_source
    Index.build do |idx|
      dependency.all_sources.each {|s| idx.add_source(s.specs) }
    end
  else
    @index_requirements[:global]
  end
end
            
indicate_progress() click to toggle source
 
               # File bundler/resolver.rb, line 108
def indicate_progress
  Bundler.ui.info ".", false unless debug?
end
            
name_for(dependency) click to toggle source
 
               # File bundler/resolver.rb, line 219
def name_for(dependency)
  dependency.name
end
            
name_for_explicit_dependency_source() click to toggle source
 
               # File bundler/resolver.rb, line 223
def name_for_explicit_dependency_source
  Bundler.default_gemfile.basename.to_s
rescue StandardError
  "Gemfile"
end
            
name_for_locking_dependency_source() click to toggle source
 
               # File bundler/resolver.rb, line 229
def name_for_locking_dependency_source
  Bundler.default_lockfile.basename.to_s
rescue StandardError
  "Gemfile.lock"
end
            
relevant_sources_for_vertex(vertex) click to toggle source
 
               # File bundler/resolver.rb, line 243
def relevant_sources_for_vertex(vertex)
  if vertex.root?
    [@source_requirements[vertex.name]]
  elsif @no_aggregate_global_source
    vertex.recursive_predecessors.map do |v|
      @source_requirements[v.name]
    end.compact << @source_requirements[:default]
  else
    []
  end
end
            
requirement_satisfied_by?(requirement, activated, spec) click to toggle source
 
               # File bundler/resolver.rb, line 235
def requirement_satisfied_by?(requirement, activated, spec)
  requirement.matches_spec?(spec) || spec.source.is_a?(Source::Gemspec)
end
            
results_for(dependency, base) click to toggle source
 
               # File bundler/resolver.rb, line 215
def results_for(dependency, base)
  index_for(dependency).search(dependency, base)
end
            
search_for(dependency_proxy) click to toggle source
 
               # File bundler/resolver.rb, line 128
def search_for(dependency_proxy)
  platform = dependency_proxy.__platform
  dependency = dependency_proxy.dep
  name = dependency.name
  search_result = @search_for[dependency_proxy] ||= begin
    results = results_for(dependency, @base[name])

    if vertex = @base_dg.vertex_named(name)
      locked_requirement = vertex.payload.requirement
    end

    if !@prerelease_specified[name] && (!@use_gvp || locked_requirement.nil?)
      # Move prereleases to the beginning of the list, so they're considered
      # last during resolution.
      pre, results = results.partition {|spec| spec.version.prerelease? }
      results = pre + results
    end

    spec_groups = if results.any?
      nested = []
      results.each do |spec|
        version, specs = nested.last
        if version == spec.version
          specs << spec
        else
          nested << [spec.version, [spec]]
        end
      end
      nested.reduce([]) do |groups, (version, specs)|
        next groups if locked_requirement && !locked_requirement.satisfied_by?(version)

        specs_by_platform = Hash.new do |current_specs, current_platform|
          current_specs[current_platform] = select_best_platform_match(specs, current_platform)
        end

        spec_group_ruby = SpecGroup.create_for(specs_by_platform, [Gem::Platform::RUBY], Gem::Platform::RUBY)
        groups << spec_group_ruby if spec_group_ruby

        next groups if @resolving_only_for_ruby

        spec_group = SpecGroup.create_for(specs_by_platform, @platforms, platform)
        groups << spec_group if spec_group

        groups
      end
    else
      []
    end
    # GVP handles major itself, but it's still a bit risky to trust it with it
    # until we get it settled with new behavior. For 2.x it can take over all cases.
    if !@use_gvp
      spec_groups
    else
      @gem_version_promoter.sort_versions(dependency, spec_groups)
    end
  end

  unless search_result.empty?
    specific_dependency = @variant_specific_names.include?(name)
    return search_result unless specific_dependency

    search_result.each do |sg|
      if @generic_names.include?(name)
        @variant_specific_names -= [name]
        sg.activate_all_platforms!
      else
        sg.activate_platform!(platform)
      end
    end
  end

  search_result
end
            
sort_dependencies(dependencies, activated, conflicts) click to toggle source
 
               # File bundler/resolver.rb, line 255
def sort_dependencies(dependencies, activated, conflicts)
  dependencies.sort_by do |dependency|
    name = name_for(dependency)
    vertex = activated.vertex_named(name)
    dependency.all_sources = relevant_sources_for_vertex(vertex)
    [
      @base_dg.vertex_named(name) ? 0 : 1,
      vertex.payload ? 0 : 1,
      vertex.root? ? 0 : 1,
      amount_constrained(dependency),
      conflicts[name] ? 0 : 1,
      vertex.payload ? 0 : search_for(dependency).count,
      self.class.platform_sort_key(dependency.__platform),
    ]
  end
end
            
start(requirements) click to toggle source
 
               # File bundler/resolver.rb, line 54
def start(requirements)
  @gem_version_promoter.prerelease_specified = @prerelease_specified = {}
  requirements.each {|dep| @prerelease_specified[dep.name] ||= dep.prerelease? }

  verify_gemfile_dependencies_are_found!(requirements)
  dg = @resolver.resolve(requirements, @base_dg)
  dg.
    tap {|resolved| validate_resolved_specs!(resolved) }.
    map(&:payload).
    reject {|sg| sg.name.end_with?("\0") }.
    map(&:to_specs).
    flatten
rescue Molinillo::VersionConflict => e
  message = version_conflict_message(e)
  raise VersionConflict.new(e.conflicts.keys.uniq, message)
rescue Molinillo::CircularDependencyError => e
  names = e.dependencies.sort_by(&:name).map {|d| "gem '#{d.name}'" }
  raise CyclicDependencyError, "Your bundle requires gems that depend"          " on each other, creating an infinite loop. Please remove"          " #{names.count > 1 ? "either " : ""}#{names.join(" or ")}"          " and try again."
end