Parent

Class Index [+]

Quicksearch

Bundler::Resolver

Constants

ALL

Attributes

errors[R]

Public Class Methods

new(index, source_requirements, base) click to toggle source
     # File lib/bundler/resolver.rb, line 135
135:     def initialize(index, source_requirements, base)
136:       @errors               = {}
137:       @stack                = []
138:       @base                 = base
139:       @index                = index
140:       @deps_for             = {}
141:       @missing_gems         = Hash.new(0)
142:       @source_requirements  = source_requirements
143:     end
resolve(requirements, index, source_requirements = {}, base = []) 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

The list of dependencies to resolve

Returns

,nil

If the list of dependencies can be resolved, a

  collection of gemspecs is returned. Otherwise, nil is returned.
     # File lib/bundler/resolver.rb, line 124
124:     def self.resolve(requirements, index, source_requirements = {}, base = [])
125:       base = SpecSet.new(base) unless base.is_a?(SpecSet)
126:       resolver = new(index, source_requirements, base)
127:       result = catch(:success) do
128:         resolver.start(requirements)
129:         raise resolver.version_conflict
130:         nil
131:       end
132:       SpecSet.new(result)
133:     end

Public Instance Methods

clean_req(req) click to toggle source
     # File lib/bundler/resolver.rb, line 397
397:     def clean_req(req)
398:       if req.to_s.include?(">= 0")
399:         req.to_s.gsub(/ \(.*?\)$/, '')
400:       else
401:         req.to_s.gsub(/\, (runtime|development)\)$/, ')')
402:       end
403:     end
clear_search_cache() click to toggle source
     # File lib/bundler/resolver.rb, line 364
364:     def clear_search_cache
365:       @deps_for = {}
366:     end
debug() click to toggle source
     # File lib/bundler/resolver.rb, line 145
145:     def debug
146:       if ENV['DEBUG_RESOLVER']
147:         debug_info = yield
148:         debug_info = debug_info.inspect unless debug_info.is_a?(String)
149:         $stderr.puts debug_info
150:       end
151:     end
error_message() click to toggle source
     # File lib/bundler/resolver.rb, line 424
424:     def error_message
425:       errors.inject("") do |o, (conflict, (origin, requirement))|
426: 
427:         # origin is the SpecSet of specs from the Gemfile that is conflicted with
428:         if origin
429: 
430:           o << %{Bundler could not find compatible versions for gem "#{origin.name}":\n}
431:           o << "  In Gemfile:\n"
432: 
433:           o << gem_message(requirement)
434: 
435:           # If the origin is "bundler", the conflict is us
436:           if origin.name == "bundler"
437:             o << "  Current Bundler version:\n"
438:             other_bundler_required = !requirement.requirement.satisfied_by?(origin.version)
439:           # If the origin is a LockfileParser, it does not respond_to :required_by
440:           elsif !origin.respond_to?(:required_by) || !(origin.required_by.first)
441:             o << "  In snapshot (Gemfile.lock):\n"
442:           end
443: 
444:           o << gem_message(origin)
445: 
446:           # If the bundle wants a newer bundler than the running bundler, explain
447:           if origin.name == "bundler" && other_bundler_required
448:             o << "This Gemfile requires a different version of Bundler.\n"
449:             o << "Perhaps you need to update Bundler by running `gem install bundler`?"
450:           end
451: 
452:         # origin is nil if the required gem and version cannot be found in any of
453:         # the specified sources
454:         else
455: 
456:           # if the gem cannot be found because of a version conflict between lockfile and gemfile,
457:           # print a useful error that suggests running `bundle update`, which may fix things
458:           #
459:           # @base is a SpecSet of the gems in the lockfile
460:           # conflict is the name of the gem that could not be found
461:           if locked = @base[conflict].first
462:             o << "Bundler could not find compatible versions for gem #{conflict.inspect}:\n"
463:             o << "  In snapshot (Gemfile.lock):\n"
464:             o << "    #{clean_req(locked)}\n\n"
465: 
466:             o << "  In Gemfile:\n"
467:             o << gem_message(requirement)
468:             o << "Running `bundle update` will rebuild your snapshot from scratch, using only\n"
469:             o << "the gems in your Gemfile, which may resolve the conflict.\n"
470: 
471:           # the rest of the time, the gem cannot be found because it does not exist in the known sources
472:           else
473:             if requirement.required_by.first
474:               o << "Could not find gem '#{clean_req(requirement)}', which is required by "
475:               o << "gem '#{clean_req(requirement.required_by.first)}', in any of the sources."
476:             else
477:               o << "Could not find gem '#{clean_req(requirement)} in any of the sources\n"
478:             end
479:           end
480: 
481:         end
482:         o
483:       end
484:     end
gem_message(requirement) click to toggle source

For a given conflicted requirement, print out what exactly went wrong

     # File lib/bundler/resolver.rb, line 410
410:     def gem_message(requirement)
411:       m = ""
412: 
413:       # A requirement that is required by itself is actually in the Gemfile, and does
414:       # not "depend on" itself
415:       if requirement.required_by.first && requirement.required_by.first.name != requirement.name
416:         m << "    #{clean_req(requirement.required_by.first)} depends on\n"
417:         m << "      #{clean_req(requirement)}\n"
418:       else
419:         m << "    #{clean_req(requirement)}\n"
420:       end
421:       m << "\n"
422:     end
gems_size(dep) click to toggle source
     # File lib/bundler/resolver.rb, line 360
360:     def gems_size(dep)
361:       search(dep).size
362:     end
resolve(reqs, activated) click to toggle source
     # File lib/bundler/resolver.rb, line 164
164:     def resolve(reqs, activated)
165:       # If the requirements are empty, then we are in a success state. Aka, all
166:       # gem dependencies have been resolved.
167:       throw :success, successify(activated) if reqs.empty?
168: 
169:       debug { print "\e[2J\e[f" ; "==== Iterating ====\n\n" }
170: 
171:       # Sort dependencies so that the ones that are easiest to resolve are first.
172:       # Easiest to resolve is defined by:
173:       #   1) Is this gem already activated?
174:       #   2) Do the version requirements include prereleased gems?
175:       #   3) Sort by number of gems available in the source.
176:       reqs = reqs.sort_by do |a|
177:         [ activated[a.name] ? 0 : 1,
178:           a.requirement.prerelease? ? 0 : 1,
179:           @errors[a.name]   ? 0 : 1,
180:           activated[a.name] ? 0 : @gems_size[a] ]
181:       end
182: 
183:       debug { "Activated:\n" + activated.values.map {|a| "  #{a}" }.join("\n") }
184:       debug { "Requirements:\n" + reqs.map {|r| "  #{r}"}.join("\n") }
185: 
186:       activated = activated.dup
187: 
188:       # Pull off the first requirement so that we can resolve it
189:       current = reqs.shift
190: 
191:       debug { "Attempting:\n  #{current}"}
192: 
193:       # Check if the gem has already been activated, if it has, we will make sure
194:       # that the currently activated gem satisfies the requirement.
195:       existing = activated[current.name]
196:       if existing || current.name == 'bundler'
197:         # Force the current
198:         if current.name == 'bundler' && !existing
199:           existing = search(DepProxy.new(Gem::Dependency.new('bundler', VERSION), Gem::Platform::RUBY)).first
200:           raise GemNotFound, %{Bundler could not find gem "bundler" (#{VERSION})} unless existing
201:           existing.required_by << existing
202:           activated['bundler'] = existing
203:         end
204: 
205:         if current.requirement.satisfied_by?(existing.version)
206:           debug { "    * [SUCCESS] Already activated" }
207:           @errors.delete(existing.name)
208:           # Since the current requirement is satisfied, we can continue resolving
209:           # the remaining requirements.
210: 
211:           # I have no idea if this is the right way to do it, but let's see if it works
212:           # The current requirement might activate some other platforms, so let's try
213:           # adding those requirements here.
214:           dependencies = existing.activate_platform(current.__platform)
215:           reqs.concat dependencies
216: 
217:           dependencies.each do |dep|
218:             next if dep.type == :development
219:             @gems_size[dep] ||= gems_size(dep)
220:           end
221: 
222:           resolve(reqs, activated)
223:         else
224:           debug { "    * [FAIL] Already activated" }
225:           @errors[existing.name] = [existing, current]
226:           debug { current.required_by.map {|d| "      * #{d.name} (#{d.requirement})" }.join("\n") }
227:           # debug { "    * All current conflicts:\n" + @errors.keys.map { |c| "      - #{c}" }.join("\n") }
228:           # Since the current requirement conflicts with an activated gem, we need
229:           # to backtrack to the current requirement's parent and try another version
230:           # of it (maybe the current requirement won't be present anymore). If the
231:           # current requirement is a root level requirement, we need to jump back to
232:           # where the conflicting gem was activated.
233:           parent = current.required_by.last
234:           # `existing` could not respond to required_by if it is part of the base set
235:           # of specs that was passed to the resolver (aka, instance of LazySpecification)
236:           parent ||= existing.required_by.last if existing.respond_to?(:required_by)
237:           # We track the spot where the current gem was activated because we need
238:           # to keep a list of every spot a failure happened.
239:           if parent && parent.name != 'bundler'
240:             debug { "    -> Jumping to: #{parent.name}" }
241:             required_by = existing.respond_to?(:required_by) && existing.required_by.last
242:             throw parent.name, required_by && required_by.name
243:           else
244:             # The original set of dependencies conflict with the base set of specs
245:             # passed to the resolver. This is by definition an impossible resolve.
246:             raise version_conflict
247:           end
248:         end
249:       else
250:         # There are no activated gems for the current requirement, so we are going
251:         # to find all gems that match the current requirement and try them in decending
252:         # order. We also need to keep a set of all conflicts that happen while trying
253:         # this gem. This is so that if no versions work, we can figure out the best
254:         # place to backtrack to.
255:         conflicts = Set.new
256: 
257:         # Fetch all gem versions matching the requirement
258:         matching_versions = search(current)
259: 
260:         # If we found no versions that match the current requirement
261:         if matching_versions.empty?
262:           # If this is a top-level Gemfile requirement
263:           if current.required_by.empty?
264:             if base = @base[current.name] and !base.empty?
265:               version = base.first.version
266:               message = "You have requested:\n"                      "  #{current.name} #{current.requirement}\n\n"                      "The bundle currently has #{current.name} locked at #{version}.\n"                      "Try running `bundle update #{current.name}`"
267:             elsif current.source
268:               name = current.name
269:               versions = @source_requirements[name][name].map { |s| s.version }
270:               message  = "Could not find gem '#{current}' in #{current.source}.\n"
271:               if versions.any?
272:                 message << "Source contains '#{name}' at: #{versions.join(', ')}"
273:               else
274:                 message << "Source does not contain any versions of '#{current}'"
275:               end
276:             else
277:               message = "Could not find gem '#{current}' "
278:               if @index.source_types.include?(Bundler::Source::Rubygems)
279:                 message << "in any of the gem sources listed in your Gemfile."
280:               else
281:                 message << "in the gems available on this machine."
282:               end
283:             end
284:             raise GemNotFound, message
285:           # This is not a top-level Gemfile requirement
286:           else
287:             @errors[current.name] = [nil, current]
288:           end
289:         end
290: 
291:         matching_versions.reverse_each do |spec_group|
292:           conflict = resolve_requirement(spec_group, current, reqs.dup, activated.dup)
293:           conflicts << conflict if conflict
294:         end
295:         # If the current requirement is a root level gem and we have conflicts, we
296:         # can figure out the best spot to backtrack to.
297:         if current.required_by.empty? && !conflicts.empty?
298:           # Check the current "catch" stack for the first one that is included in the
299:           # conflicts set. That is where the parent of the conflicting gem was required.
300:           # By jumping back to this spot, we can try other version of the parent of
301:           # the conflicting gem, hopefully finding a combination that activates correctly.
302:           @stack.reverse_each do |savepoint|
303:             if conflicts.include?(savepoint)
304:               debug { "    -> Jumping to: #{savepoint}" }
305:               throw savepoint
306:             end
307:           end
308:         end
309:       end
310:     end
resolve_requirement(spec_group, requirement, reqs, activated) click to toggle source
     # File lib/bundler/resolver.rb, line 315
315:     def resolve_requirement(spec_group, requirement, reqs, activated)
316:       # We are going to try activating the spec. We need to keep track of stack of
317:       # requirements that got us to the point of activating this gem.
318:       spec_group.required_by.replace requirement.required_by
319:       spec_group.required_by << requirement
320: 
321:       activated[spec_group.name] = spec_group
322:       debug { "  Activating: #{spec_group.name} (#{spec_group.version})" }
323:       debug { spec_group.required_by.map { |d| "    * #{d.name} (#{d.requirement})" }.join("\n") }
324: 
325:       dependencies = spec_group.activate_platform(requirement.__platform)
326: 
327:       # Now, we have to loop through all child dependencies and add them to our
328:       # array of requirements.
329:       debug { "    Dependencies"}
330:       dependencies.each do |dep|
331:         next if dep.type == :development
332:         debug { "    * #{dep.name} (#{dep.requirement})" }
333:         dep.required_by.replace(requirement.required_by)
334:         dep.required_by << requirement
335:         @gems_size[dep] ||= gems_size(dep)
336:         reqs << dep
337:       end
338: 
339:       # We create a savepoint and mark it by the name of the requirement that caused
340:       # the gem to be activated. If the activated gem ever conflicts, we are able to
341:       # jump back to this point and try another version of the gem.
342:       length = @stack.length
343:       @stack << requirement.name
344:       retval = catch(requirement.name) do
345:         # try to resolve the next option
346:         resolve(reqs, activated)
347:       end
348: 
349:       # clear the search cache since the catch means we couldn't meet the
350:       # requirement we need with the current constraints on search
351:       clear_search_cache
352: 
353:       # Since we're doing a lot of throw / catches. A push does not necessarily match
354:       # up to a pop. So, we simply slice the stack back to what it was before the catch
355:       # block.
356:       @stack.slice!(length..1)
357:       retval
358:     end
search(dep) click to toggle source
     # File lib/bundler/resolver.rb, line 368
368:     def search(dep)
369:       if base = @base[dep.name] and base.any?
370:         reqs = [dep.requirement.as_list, base.first.version.to_s].flatten.compact
371:         d = Gem::Dependency.new(base.first.name, *reqs)
372:       else
373:         d = dep.dep
374:       end
375: 
376:       @deps_for[d.hash] ||= begin
377:         index = @source_requirements[d.name] || @index
378:         results = index.search(d, @base[d.name])
379: 
380:         if results.any?
381:           version = results.first.version
382:           nested  = [[]]
383:           results.each do |spec|
384:             if spec.version != version
385:               nested << []
386:               version = spec.version
387:             end
388:             nested.last << spec
389:           end
390:           deps = nested.map{|a| SpecGroup.new(a) }.select{|sg| sg.for?(dep.__platform) }
391:         else
392:           deps = []
393:         end
394:       end
395:     end
start(reqs) click to toggle source
     # File lib/bundler/resolver.rb, line 157
157:     def start(reqs)
158:       activated = {}
159:       @gems_size = Hash[reqs.map { |r| [r, gems_size(r)] }]
160: 
161:       resolve(reqs, activated)
162:     end
successify(activated) click to toggle source
     # File lib/bundler/resolver.rb, line 153
153:     def successify(activated)
154:       activated.values.map { |s| s.to_specs }.flatten.compact
155:     end
version_conflict() click to toggle source
     # File lib/bundler/resolver.rb, line 405
405:     def version_conflict
406:       VersionConflict.new(errors.keys, error_message)
407:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.