Class: Bundler::Resolver
- Inherits:
-
Object
show all
- Includes:
- GemHelpers
- Defined in:
- lib/bundler/resolver.rb,
lib/bundler/resolver/base.rb,
lib/bundler/resolver/root.rb,
lib/bundler/resolver/package.rb,
lib/bundler/resolver/candidate.rb,
lib/bundler/resolver/spec_group.rb,
lib/bundler/resolver/incompatibility.rb
Overview
This class implements the interface needed by PubGrub for resolution. It is equivalent to the ‘PubGrub::BasicPackageSource` class provided by PubGrub by default and used by the most simple PubGrub consumers.
Defined Under Namespace
Classes: Base, Candidate, Incompatibility, Package, Root, SpecGroup
Constant Summary
Constants included
from GemHelpers
GemHelpers::GENERICS, GemHelpers::GENERIC_CACHE
Instance Method Summary
collapse
Methods included from GemHelpers
force_ruby_platform, generic, generic_local_platform, generic_local_platform_is_ruby?, local_platform, platform_specificity_match, same_deps, same_specificity, select_best_platform_match, sort_best_platform_match
Constructor Details
#initialize(base, gem_version_promoter) ⇒ Resolver
Returns a new instance of Resolver.
18
19
20
21
22
|
# File 'lib/bundler/resolver.rb', line 18
def initialize(base, gem_version_promoter)
@source_requirements = base.source_requirements
@base = base
@gem_version_promoter = gem_version_promoter
end
|
Instance Method Details
#all_versions_for(package) ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/bundler/resolver.rb', line 244
def all_versions_for(package)
name = package.name
results = (@base[name] + filter_prereleases(@all_specs[name], package)).uniq {|spec| [spec.version.hash, spec.platform] }
if name == "bundler" && !bundler_pinned_to_current_version?
bundler_spec = Gem.loaded_specs["bundler"]
results << bundler_spec if bundler_spec
end
locked_requirement = base_requirements[name]
results = filter_matching_specs(results, locked_requirement) if locked_requirement
results.group_by(&:version).reduce([]) do |groups, (version, specs)|
platform_specs = package.platforms.map {|platform| select_best_platform_match(specs, platform) }
if package.top_level?
next groups if platform_specs.any?(&:empty?)
else
next groups if platform_specs.all?(&:empty?)
end
platform_specs.flatten!
ruby_specs = select_best_platform_match(specs, Gem::Platform::RUBY)
groups << Resolver::Candidate.new(version, specs: ruby_specs) if ruby_specs.any?
next groups if platform_specs == ruby_specs || package.force_ruby_platform?
groups << Resolver::Candidate.new(version, specs: platform_specs)
groups
end
end
|
#bundler_pinned_to_current_version? ⇒ Boolean
293
294
295
|
# File 'lib/bundler/resolver.rb', line 293
def bundler_pinned_to_current_version?
!default_bundler_source.nil?
end
|
#debug? ⇒ Boolean
193
194
195
196
197
198
199
|
# File 'lib/bundler/resolver.rb', line 193
def debug?
ENV["BUNDLER_DEBUG_RESOLVER"] ||
ENV["BUNDLER_DEBUG_RESOLVER_TREE"] ||
ENV["DEBUG_RESOLVER"] ||
ENV["DEBUG_RESOLVER_TREE"] ||
false
end
|
#default_bundler_source ⇒ Object
289
290
291
|
# File 'lib/bundler/resolver.rb', line 289
def default_bundler_source
@source_requirements[:default_bundler]
end
|
#find_names_to_relax(incompatibility) ⇒ Object
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
|
# File 'lib/bundler/resolver.rb', line 119
def find_names_to_relax(incompatibility)
names_to_unlock = []
names_to_allow_prereleases_for = []
extended_explanation = nil
while incompatibility.conflict?
cause = incompatibility.cause
incompatibility = cause.incompatibility
incompatibility.terms.each do |term|
package = term.package
name = package.name
if base_requirements[name]
names_to_unlock << name
elsif package.ignores_prereleases? && @all_specs[name].any? {|s| s.version.prerelease? }
names_to_allow_prereleases_for << name
end
no_versions_incompat = [cause.incompatibility, cause.satisfier].find {|incompat| incompat.cause.is_a?(PubGrub::Incompatibility::NoVersions) }
next unless no_versions_incompat
extended_explanation = no_versions_incompat.extended_explanation
end
end
[names_to_unlock.uniq, names_to_allow_prereleases_for.uniq, extended_explanation]
end
|
#incompatibilities_for(package, version) ⇒ Object
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/bundler/resolver.rb', line 201
def incompatibilities_for(package, version)
package_deps = @cached_dependencies[package]
sorted_versions = @sorted_versions[package]
package_deps[version].map do |dep_package, dep_constraint|
low = high = sorted_versions.index(version)
while low > 0 && package_deps[sorted_versions[low - 1]][dep_package] == dep_constraint
low -= 1
end
low =
if low == 0
nil
else
sorted_versions[low]
end
while high < sorted_versions.length && package_deps[sorted_versions[high]][dep_package] == dep_constraint
high += 1
end
high =
if high == sorted_versions.length
nil
else
sorted_versions[high]
end
range = PubGrub::VersionRange.new(min: low, max: high, include_min: true)
self_constraint = PubGrub::VersionConstraint.new(package, range: range)
dep_term = PubGrub::Term.new(dep_constraint, false)
self_term = PubGrub::Term.new(self_constraint, true)
custom_explanation = if dep_package.meta? && package.root?
"current #{dep_package} version is #{dep_constraint.constraint_string}"
end
PubGrub::Incompatibility.new([self_term, dep_term], cause: :dependency, custom_explanation: custom_explanation)
end
end
|
#name_for_explicit_dependency_source ⇒ Object
297
298
299
300
301
|
# File 'lib/bundler/resolver.rb', line 297
def name_for_explicit_dependency_source
Bundler.default_gemfile.basename.to_s
rescue StandardError
"Gemfile"
end
|
#no_versions_incompatibility_for(package, unsatisfied_term) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/bundler/resolver.rb', line 170
def no_versions_incompatibility_for(package, unsatisfied_term)
cause = PubGrub::Incompatibility::NoVersions.new(unsatisfied_term)
name = package.name
constraint = unsatisfied_term.constraint
constraint_string = constraint.constraint_string
requirements = constraint_string.split(" OR ").map {|req| Gem::Requirement.new(req.split(",")) }
if name == "bundler" && bundler_pinned_to_current_version?
custom_explanation = "the current Bundler version (#{Bundler::VERSION}) does not satisfy #{constraint}"
extended_explanation = bundler_not_found_message(requirements)
else
specs_matching_other_platforms = filter_matching_specs(@all_specs[name], requirements)
platforms_explanation = specs_matching_other_platforms.any? ? " for any resolution platforms (#{package.platforms.join(", ")})" : ""
custom_explanation = "#{constraint} could not be found in #{repository_for(package)}#{platforms_explanation}"
label = "#{name} (#{constraint_string})"
extended_explanation = other_specs_matching_message(specs_matching_other_platforms, label) if specs_matching_other_platforms.any?
end
Incompatibility.new([unsatisfied_term], cause: cause, custom_explanation: custom_explanation, extended_explanation: extended_explanation)
end
|
#parse_dependency(package, dependency) ⇒ Object
#raise_not_found!(package) ⇒ Object
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
# File 'lib/bundler/resolver.rb', line 303
def raise_not_found!(package)
name = package.name
source = source_for(name)
specs = @all_specs[name]
matching_part = name
requirement_label = SharedHelpers.pretty_dependency(package.dependency)
cache_message = begin
" or in gems cached in #{Bundler.settings.app_cache_path}" if Bundler.app_cache.exist?
rescue GemfileNotFound
nil
end
specs_matching_requirement = filter_matching_specs(specs, package.dependency.requirement)
not_found_message = if specs_matching_requirement.any?
specs = specs_matching_requirement
matching_part = requirement_label
platforms = package.platforms
if platforms.size == 1
"Could not find gem '#{requirement_label}' with platform '#{platforms.first}'"
else
"Could not find gems matching '#{requirement_label}' valid for all resolution platforms (#{platforms.join(", ")})"
end
else
"Could not find gem '#{requirement_label}'"
end
message = String.new("#{not_found_message} in #{source}#{cache_message}.\n")
if specs.any?
message << "\n#{other_specs_matching_message(specs, matching_part)}"
end
raise GemNotFound, message
end
|
#setup_solver ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/bundler/resolver.rb', line 35
def setup_solver
root = Resolver::Root.new(name_for_explicit_dependency_source)
root_version = Resolver::Candidate.new(0)
@all_specs = Hash.new do |specs, name|
source = source_for(name)
matches = source.specs.search(name)
if source.respond_to?(:dependency_api_available?) && source.dependency_api_available?
matches = filter_invalid_self_dependencies(matches, name)
end
specs[name] = matches.sort_by {|s| [s.version, s.platform.to_s] }
end
@all_versions = Hash.new do |candidates, package|
candidates[package] = all_versions_for(package)
end
@sorted_versions = Hash.new do |candidates, package|
candidates[package] = filtered_versions_for(package).sort
end
@sorted_versions[root] = [root_version]
root_dependencies = prepare_dependencies(@requirements, @packages)
@cached_dependencies = Hash.new do |dependencies, package|
dependencies[package] = Hash.new do |versions, version|
versions[version] = to_dependency_hash(version.dependencies.reject {|d| d.name == package.name }, @packages)
end
end
@cached_dependencies[root] = { root_version => root_dependencies }
logger = Bundler::UI::Shell.new
logger.level = debug? ? "debug" : "warn"
[root, logger]
end
|
#solve_versions(root:, logger:) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/bundler/resolver.rb', line 79
def solve_versions(root:, logger:)
solver = PubGrub::VersionSolver.new(source: self, root: root, logger: logger)
result = solver.solve
result.map {|package, version| version.to_specs(package) }.flatten.uniq
rescue PubGrub::SolveFailure => e
incompatibility = e.incompatibility
names_to_unlock, names_to_allow_prereleases_for, extended_explanation = find_names_to_relax(incompatibility)
names_to_relax = names_to_unlock + names_to_allow_prereleases_for
if names_to_relax.any?
if names_to_unlock.any?
Bundler.ui.debug "Found conflicts with locked dependencies. Will retry with #{names_to_unlock.join(", ")} unlocked...", true
@base.unlock_names(names_to_unlock)
end
if names_to_allow_prereleases_for.any?
Bundler.ui.debug "Found conflicts with dependencies with prereleases. Will retrying considering prereleases for #{names_to_allow_prereleases_for.join(", ")}...", true
@base.include_prereleases(names_to_allow_prereleases_for)
end
root, logger = setup_solver
Bundler.ui.debug "Retrying resolution...", true
retry
end
explanation = e.message
if extended_explanation
explanation << "\n\n"
explanation << extended_explanation
end
raise SolveFailure.new(explanation)
end
|
#source_for(name) ⇒ Object
285
286
287
|
# File 'lib/bundler/resolver.rb', line 285
def source_for(name)
@source_requirements[name] || @source_requirements[:default]
end
|
#start ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/bundler/resolver.rb', line 24
def start
@requirements = @base.requirements
@packages = @base.packages
root, logger = setup_solver
Bundler.ui.info "Resolving dependencies...", true
solve_versions(root: root, logger: logger)
end
|
#versions_for(package, range = VersionRange.any) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/bundler/resolver.rb', line 158
def versions_for(package, range=VersionRange.any)
versions = select_sorted_versions(package, range)
if versions.size > 1
sort_versions_by_preferred(package, versions)
else
versions
end
end
|