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:       @gems_size            = {}
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 382
382:     def clean_req(req)
383:       if req.to_s.include?(">= 0")
384:         req.to_s.gsub(/ \(.*?\)$/, '')
385:       else
386:         req.to_s.gsub(/\, (runtime|development)\)$/, ')')
387:       end
388:     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.inpsect 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 409
409:     def error_message
410:       errors.inject("") do |o, (conflict, (origin, requirement))|
411: 
412:         # origin is the SpecSet of specs from the Gemfile that is conflicted with
413:         if origin
414: 
415:           o << %{Bundler could not find compatible versions for gem "#{origin.name}":\n}
416:           o << "  In Gemfile:\n"
417: 
418:           o << gem_message(requirement)
419: 
420:           # If the origin is "bundler", the conflict is us
421:           if origin.name == "bundler"
422:             o << "  Current Bundler version:\n"
423:             newer_bundler_required = requirement.requirement > Gem::Requirement.new(origin.version)
424:           # If the origin is a LockfileParser, it does not respond_to :required_by
425:           elsif !origin.respond_to?(:required_by) || !(origin.required_by.first)
426:             o << "  In snapshot (Gemfile.lock):\n"
427:           end
428: 
429:           o << gem_message(origin)
430: 
431:           # If the bundle wants a newer bundler than the running bundler, explain
432:           if origin.name == "bundler" && newer_bundler_required
433:             o << "Your version of Bundler is older than the one requested by the Gemfile.\n"
434:             o << "Perhaps you need to update Bundler by running `gem install bundler`."
435:           end
436: 
437:         # origin is nil if the required gem and version cannot be found in any of
438:         # the specified sources
439:         else
440: 
441:           # if the gem cannot be found because of a version conflict between lockfile and gemfile,
442:           # print a useful error that suggests running `bundle update`, which may fix things
443:           #
444:           # @base is a SpecSet of the gems in the lockfile
445:           # conflict is the name of the gem that could not be found
446:           if locked = @base[conflict].first
447:             o << "Bundler could not find compatible versions for gem #{conflict.inspect}:\n"
448:             o << "  In snapshot (Gemfile.lock):\n"
449:             o << "    #{clean_req(locked)}\n\n"
450: 
451:             o << "  In Gemfile:\n"
452:             o << gem_message(requirement)
453:             o << "Running `bundle update` will rebuild your snapshot from scratch, using only\n"
454:             o << "the gems in your Gemfile, which may resolve the conflict.\n"
455: 
456:           # the rest of the time, the gem cannot be found because it does not exist in the known sources
457:           else
458:             if requirement.required_by.first
459:               o << "Could not find gem '#{clean_req(requirement)}', required by '#{clean_req(requirement.required_by.first)}', in any of the sources\n"
460:             else
461:               o << "Could not find gem '#{clean_req(requirement)} in any of the sources\n"
462:             end
463:           end
464: 
465:         end
466:         o
467:       end
468:     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 395
395:     def gem_message(requirement)
396:       m = ""
397: 
398:       # A requirement that is required by itself is actually in the Gemfile, and does
399:       # not "depend on" itself
400:       if requirement.required_by.first && requirement.required_by.first.name != requirement.name
401:         m << "    #{clean_req(requirement.required_by.first)} depends on\n"
402:         m << "      #{clean_req(requirement)}\n"
403:       else
404:         m << "    #{clean_req(requirement)}\n"
405:       end
406:       m << "\n"
407:     end
gems_size(dep) click to toggle source
     # File lib/bundler/resolver.rb, line 353
353:     def gems_size(dep)
354:       @gems_size[dep] ||= search(dep).size
355:     end
resolve(reqs, activated) click to toggle source
     # File lib/bundler/resolver.rb, line 163
163:     def resolve(reqs, activated)
164:       # If the requirements are empty, then we are in a success state. Aka, all
165:       # gem dependencies have been resolved.
166:       throw :success, successify(activated) if reqs.empty?
167: 
168:       debug { print "\e[2J\e[f" ; "==== Iterating ====\n\n" }
169: 
170:       # Sort dependencies so that the ones that are easiest to resolve are first.
171:       # Easiest to resolve is defined by:
172:       #   1) Is this gem already activated?
173:       #   2) Do the version requirements include prereleased gems?
174:       #   3) Sort by number of gems available in the source.
175:       reqs = reqs.sort_by do |a|
176:         [ activated[a.name] ? 0 : 1,
177:           a.requirement.prerelease? ? 0 : 1,
178:           @errors[a.name]   ? 0 : 1,
179:           activated[a.name] ? 0 : gems_size(a) ]
180:       end
181: 
182:       debug { "Activated:\n" + activated.values.map { |a| "  #{a.name} (#{a.version})" }.join("\n") }
183:       debug { "Requirements:\n" + reqs.map { |r| "  #{r.name} (#{r.requirement})"}.join("\n") }
184: 
185:       activated = activated.dup
186: 
187:       # Pull off the first requirement so that we can resolve it
188:       current = reqs.shift
189: 
190:       debug { "Attempting:\n  #{current.name} (#{current.requirement})"}
191: 
192:       # Check if the gem has already been activated, if it has, we will make sure
193:       # that the currently activated gem satisfies the requirement.
194:       existing = activated[current.name]
195:       if existing || current.name == 'bundler'
196:         # Force the current
197:         if current.name == 'bundler' && !existing
198:           existing = search(DepProxy.new(Gem::Dependency.new('bundler', VERSION), Gem::Platform::RUBY)).first
199:           raise GemNotFound, %{Bundler could not find gem "bundler" (#{VERSION})} unless existing
200:           existing.required_by << existing
201:           activated['bundler'] = existing
202:         end
203: 
204:         if current.requirement.satisfied_by?(existing.version)
205:           debug { "    * [SUCCESS] Already activated" }
206:           @errors.delete(existing.name)
207:           # Since the current requirement is satisfied, we can continue resolving
208:           # the remaining requirements.
209: 
210:           # I have no idea if this is the right way to do it, but let's see if it works
211:           # The current requirement might activate some other platforms, so let's try
212:           # adding those requirements here.
213:           reqs.concat existing.activate_platform(current.__platform)
214: 
215:           resolve(reqs, activated)
216:         else
217:           debug { "    * [FAIL] Already activated" }
218:           @errors[existing.name] = [existing, current]
219:           debug { current.required_by.map {|d| "      * #{d.name} (#{d.requirement})" }.join("\n") }
220:           # debug { "    * All current conflicts:\n" + @errors.keys.map { |c| "      - #{c}" }.join("\n") }
221:           # Since the current requirement conflicts with an activated gem, we need
222:           # to backtrack to the current requirement's parent and try another version
223:           # of it (maybe the current requirement won't be present anymore). If the
224:           # current requirement is a root level requirement, we need to jump back to
225:           # where the conflicting gem was activated.
226:           parent = current.required_by.last
227:           # `existing` could not respond to required_by if it is part of the base set
228:           # of specs that was passed to the resolver (aka, instance of LazySpecification)
229:           parent ||= existing.required_by.last if existing.respond_to?(:required_by)
230:           # We track the spot where the current gem was activated because we need
231:           # to keep a list of every spot a failure happened.
232:           if parent && parent.name != 'bundler'
233:             debug { "    -> Jumping to: #{parent.name}" }
234:             required_by = existing.respond_to?(:required_by) && existing.required_by.last
235:             throw parent.name, required_by && required_by.name
236:           else
237:             # The original set of dependencies conflict with the base set of specs
238:             # passed to the resolver. This is by definition an impossible resolve.
239:             raise version_conflict
240:           end
241:         end
242:       else
243:         # There are no activated gems for the current requirement, so we are going
244:         # to find all gems that match the current requirement and try them in decending
245:         # order. We also need to keep a set of all conflicts that happen while trying
246:         # this gem. This is so that if no versions work, we can figure out the best
247:         # place to backtrack to.
248:         conflicts = Set.new
249: 
250:         # Fetch all gem versions matching the requirement
251:         #
252:         # TODO: Warn / error when no matching versions are found.
253:         matching_versions = search(current)
254: 
255:         if matching_versions.empty?
256:           if current.required_by.empty?
257:             if base = @base[current.name] and !base.empty?
258:               version = base.first.version
259:               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}`"
260:             elsif current.source
261:               name = current.name
262:               versions = @source_requirements[name][name].map { |s| s.version }
263:               message  = "Could not find gem '#{current}' in #{current.source}.\n"
264:               if versions.any?
265:                 message << "Source contains '#{name}' at: #{versions.join(', ')}"
266:               else
267:                 message << "Source does not contain any versions of '#{current}'"
268:               end
269:             else
270:               message = "Could not find gem '#{current}' "
271:               if @index.sources.include?(Bundler::Source::Rubygems)
272:                 message << "in any of the gem sources listed in your Gemfile."
273:               else
274:                 message << "in the gems available on this machine."
275:               end
276:             end
277:             raise GemNotFound, message
278:           else
279:             if @missing_gems[current] >= 5
280:               message =  "Bundler could not find find gem #{current.required_by.last},"
281:               message << "which is required by gem #{current}."
282:               raise GemNotFound, message
283:             end
284:             @missing_gems[current] += 1
285: 
286:             debug { "    Could not find #{current} by #{current.required_by.last}" }
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:         reqs << dep
336:       end
337: 
338:       # We create a savepoint and mark it by the name of the requirement that caused
339:       # the gem to be activated. If the activated gem ever conflicts, we are able to
340:       # jump back to this point and try another version of the gem.
341:       length = @stack.length
342:       @stack << requirement.name
343:       retval = catch(requirement.name) do
344:         resolve(reqs, activated)
345:       end
346:       # Since we're doing a lot of throw / catches. A push does not necessarily match
347:       # up to a pop. So, we simply slice the stack back to what it was before the catch
348:       # block.
349:       @stack.slice!(length..1)
350:       retval
351:     end
search(dep) click to toggle source
     # File lib/bundler/resolver.rb, line 357
357:     def search(dep)
358:       if base = @base[dep.name] and base.any?
359:         d = Gem::Dependency.new(base.first.name, *[dep.requirement.as_list, base.first.version].flatten)
360:       else
361:         d = dep.dep
362:       end
363:       index = @source_requirements[d.name] || @index
364:       results = index.search_for_all_platforms(d, @base[d.name])
365: 
366:       if results.any?
367:         version = results.first.version
368:         nested  = [[]]
369:         results.each do |spec|
370:           if spec.version != version
371:             nested << []
372:             version = spec.version
373:           end
374:           nested.last << spec
375:         end
376:         nested.map { |a| SpecGroup.new(a) }.select { |sg| sg.for?(dep.__platform) }
377:       else
378:         []
379:       end
380:     end
start(reqs) click to toggle source
     # File lib/bundler/resolver.rb, line 157
157:     def start(reqs)
158:       activated = {}
159: 
160:       resolve(reqs, activated)
161:     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 390
390:     def version_conflict
391:       VersionConflict.new(errors.keys, error_message)
392:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.