Class: Bundler::Resolver

Inherits:
Object
  • Object
show all
Includes:
Molinillo::SpecificationProvider, Molinillo::UI
Defined in:
lib/bundler/resolver.rb,
lib/bundler/resolver/spec_group.rb

Defined Under Namespace

Classes: SpecGroup

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Molinillo::SpecificationProvider

#allow_missing?

Methods included from Molinillo::UI

#output, #progress_rate

Constructor Details

#initialize(index, source_requirements, base, gem_version_promoter, additional_base_requirements, platforms) ⇒ Resolver

Returns a new instance of Resolver



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bundler/resolver.rb', line 26

def initialize(index, source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
  @index = index
  @source_requirements = source_requirements
  @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.new(dep, ls.platform), true)
  end
  additional_base_requirements.each {|d| @base_dg.add_vertex(d.name, d) }
  @platforms = platforms
  @gem_version_promoter = gem_version_promoter
  @allow_bundler_dependency_conflicts = Bundler.feature_flag.allow_bundler_dependency_conflicts?
  @lockfile_uses_separate_rubygems_sources = Bundler.feature_flag.lockfile_uses_separate_rubygems_sources?
end

Class Method Details

.platform_sort_key(platform) ⇒ Object



224
225
226
227
# File 'lib/bundler/resolver.rb', line 224

def self.platform_sort_key(platform)
  return ["", "", ""] if Gem::Platform::RUBY == platform
  platform.to_a.map {|part| part || "" }
end

.resolve(requirements, index, source_requirements = {}, base = [], gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [], platforms = nil) ⇒ Object

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.


18
19
20
21
22
23
24
# File 'lib/bundler/resolver.rb', line 18

def self.resolve(requirements, index, source_requirements = {}, base = [], gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [], platforms = nil)
  platforms = Set.new(platforms) if platforms
  base = SpecSet.new(base) unless base.is_a?(SpecSet)
  resolver = new(index, source_requirements, base, gem_version_promoter, additional_base_requirements, platforms)
  result = resolver.start(requirements)
  SpecSet.new(result)
end

.sort_platforms(platforms) ⇒ Object

Sort platforms from most general to most specific



218
219
220
221
222
# File 'lib/bundler/resolver.rb', line 218

def self.sort_platforms(platforms)
  platforms.sort_by do |platform|
    platform_sort_key(platform)
  end
end

Instance Method Details

#after_resolutionObject



86
87
88
# File 'lib/bundler/resolver.rb', line 86

def after_resolution
  Bundler.ui.info ""
end

#before_resolutionObject



82
83
84
# File 'lib/bundler/resolver.rb', line 82

def before_resolution
  Bundler.ui.info "Resolving dependencies...", debug?
end

#debug(depth = 0) ⇒ void

This method returns an undefined value.

Conveys debug information to the user.

Parameters:

  • depth (Integer) (defaults to: 0)

    the current depth of the resolution process.



70
71
72
73
74
75
# File 'lib/bundler/resolver.rb', line 70

def debug(depth = 0)
  return unless debug?
  debug_info = yield
  debug_info = debug_info.inspect unless debug_info.is_a?(String)
  STDERR.puts debug_info.split("\n").map {|s| "  " * depth + s }
end

#debug?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/bundler/resolver.rb', line 77

def debug?
  return @debug_mode if defined?(@debug_mode)
  @debug_mode = ENV["DEBUG_RESOLVER"] || ENV["DEBUG_RESOLVER_TREE"] || false
end

#dependencies_for(specification) ⇒ Object



96
97
98
# File 'lib/bundler/resolver.rb', line 96

def dependencies_for(specification)
  specification.dependencies_for_activated_platforms
end

#index_for(dependency) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bundler/resolver.rb', line 147

def index_for(dependency)
  source = @source_requirements[dependency.name]
  if source
    source.specs
  elsif @lockfile_uses_separate_rubygems_sources
    Index.build do |idx|
      if dependency.all_sources
        dependency.all_sources.each {|s| idx.add_source(s.specs) if s }
      else
        idx.add_source @source_requirements[:default].specs
      end
    end
  else
    @index
  end
end

#indicate_progressObject



90
91
92
# File 'lib/bundler/resolver.rb', line 90

def indicate_progress
  Bundler.ui.info ".", false unless debug?
end

#name_for(dependency) ⇒ Object



164
165
166
# File 'lib/bundler/resolver.rb', line 164

def name_for(dependency)
  dependency.name
end

#name_for_explicit_dependency_sourceObject



168
169
170
171
172
# File 'lib/bundler/resolver.rb', line 168

def name_for_explicit_dependency_source
  Bundler.default_gemfile.basename.to_s
rescue
  "Gemfile"
end

#name_for_locking_dependency_sourceObject



174
175
176
177
178
# File 'lib/bundler/resolver.rb', line 174

def name_for_locking_dependency_source
  Bundler.default_lockfile.basename.to_s
rescue
  "Gemfile.lock"
end

#relevant_sources_for_vertex(vertex) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/bundler/resolver.rb', line 190

def relevant_sources_for_vertex(vertex)
  if vertex.root?
    [@source_requirements[vertex.name]]
  elsif @lockfile_uses_separate_rubygems_sources
    vertex.recursive_predecessors.map do |v|
      @source_requirements[v.name]
    end << @source_requirements[:default]
  end
end

#requirement_satisfied_by?(requirement, activated, spec) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
# File 'lib/bundler/resolver.rb', line 180

def requirement_satisfied_by?(requirement, activated, spec)
  return false unless requirement.matches_spec?(spec) || spec.source.is_a?(Source::Gemspec)
  if spec.version.prerelease? && !requirement.prerelease? && search_for(requirement).any? {|sg| !sg.version.prerelease? }
    vertex = activated.vertex_named(spec.name)
    return false if vertex.requirements.none?(&:prerelease?)
  end
  spec.activate_platform!(requirement.__platform) if !@platforms || @platforms.include?(requirement.__platform)
  true
end

#search_for(dependency) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bundler/resolver.rb', line 100

def search_for(dependency)
  platform = dependency.__platform
  dependency = dependency.dep unless dependency.is_a? Gem::Dependency
  search = @search_for[dependency] ||= begin
    index = index_for(dependency)
    results = index.search(dependency, @base[dependency.name])

    unless @prerelease_specified[dependency.name]
      # 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

    if vertex = @base_dg.vertex_named(dependency.name)
      locked_requirement = vertex.payload.requirement
    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)
        spec_group = SpecGroup.new(specs)
        spec_group.ignores_bundler_dependencies = @allow_bundler_dependency_conflicts
        groups << spec_group
      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 @gem_version_promoter.major?
      spec_groups
    else
      @gem_version_promoter.sort_versions(dependency, spec_groups)
    end
  end
  search.select {|sg| sg.for?(platform) }.each {|sg| sg.activate_platform!(platform) }
end

#sort_dependencies(dependencies, activated, conflicts) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/bundler/resolver.rb', line 200

def sort_dependencies(dependencies, activated, conflicts)
  dependencies.sort_by do |dependency|
    dependency.all_sources = relevant_sources_for_vertex(activated.vertex_named(dependency.name))
    name = name_for(dependency)
    vertex = activated.vertex_named(name)
    [
      @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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bundler/resolver.rb', line 44

def start(requirements)
  @prerelease_specified = {}
  requirements.each {|dep| @prerelease_specified[dep.name] ||= dep.prerelease? }

  verify_gemfile_dependencies_are_found!(requirements)
  dg = @resolver.resolve(requirements, @base_dg)
  dg.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