Class: Bundler::Resolver

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

Defined Under Namespace

Classes: SpecGroup

Constant Summary

Constants included from GemHelpers

GemHelpers::GENERICS, GemHelpers::GENERIC_CACHE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Molinillo::SpecificationProvider

#allow_missing?

Methods included from Molinillo::UI

#output, #progress_rate

Methods included from GemHelpers

generic, generic_local_platform, local_platform, platform_specificity_match, same_deps, same_specificity, select_best_platform_match

Constructor Details

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

Returns a new instance of Resolver.



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

def initialize(source_requirements, base, gem_version_promoter, additional_base_requirements, platforms, )
  @source_requirements = source_requirements
  @metadata_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.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?
end

Class Method Details

.platform_sort_key(platform) ⇒ Object



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

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) ⇒ 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.


20
21
22
23
24
25
26
# File 'lib/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)
  , regular_requirements = requirements.partition {|dep| dep.name.end_with?("\0") }
  resolver = new(source_requirements, base, gem_version_promoter, additional_base_requirements, platforms, )
  result = resolver.start(requirements)
  SpecSet.new(SpecSet.new(result).for(regular_requirements))
end

Instance Method Details

#after_resolutionObject



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

def after_resolution
  Bundler.ui.info ""
end

#before_resolutionObject



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

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.



74
75
76
77
78
79
# File 'lib/bundler/resolver.rb', line 74

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?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
# File 'lib/bundler/resolver.rb', line 81

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) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/bundler/resolver.rb', line 203

def dependencies_equal?(dependencies, other_dependencies)
  dependencies.map(&:dep) == other_dependencies.map(&:dep)
end

#dependencies_for(specification) ⇒ Object



105
106
107
# File 'lib/bundler/resolver.rb', line 105

def dependencies_for(specification)
  specification.dependencies_for_activated_platforms
end

#index_for(dependency) ⇒ Object



171
172
173
# File 'lib/bundler/resolver.rb', line 171

def index_for(dependency)
  source_for(dependency.name).specs
end

#indicate_progressObject



99
100
101
# File 'lib/bundler/resolver.rb', line 99

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

#name_for(dependency) ⇒ Object



183
184
185
# File 'lib/bundler/resolver.rb', line 183

def name_for(dependency)
  dependency.name
end

#name_for_explicit_dependency_sourceObject



187
188
189
190
191
# File 'lib/bundler/resolver.rb', line 187

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

#name_for_locking_dependency_sourceObject



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

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

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

Returns:

  • (Boolean)


199
200
201
# File 'lib/bundler/resolver.rb', line 199

def requirement_satisfied_by?(requirement, activated, spec)
  requirement.matches_spec?(spec) || spec.source.is_a?(Source::Gemspec)
end

#results_for(dependency, base) ⇒ Object



179
180
181
# File 'lib/bundler/resolver.rb', line 179

def results_for(dependency, base)
  index_for(dependency).search(dependency, base)
end

#search_for(dependency_proxy) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bundler/resolver.rb', line 109

def search_for(dependency_proxy)
  platform = dependency_proxy.__platform
  dependency = dependency_proxy.dep
  name = dependency.name
  @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)
        next groups unless specs.any? {|spec| spec.match_platform(platform) }

        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)
        if spec_group_ruby
          spec_group_ruby.force_ruby_platform = dependency.force_ruby_platform
          groups << spec_group_ruby
        end

        next groups if @resolving_only_for_ruby || dependency.force_ruby_platform

        spec_group = SpecGroup.create_for(specs_by_platform, @platforms, platform)
        groups << 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
end

#sort_dependencies(dependencies, activated, conflicts) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/bundler/resolver.rb', line 207

def sort_dependencies(dependencies, activated, conflicts)
  dependencies.sort_by do |dependency|
    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

#source_for(name) ⇒ Object



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

def source_for(name)
  @source_requirements[name] || @source_requirements[:default]
end

#start(requirements) ⇒ Object



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

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.
    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