Parent

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 138
138:     def initialize(index, source_requirements, base)
139:       @errors               = {}
140:       @stack                = []
141:       @base                 = base
142:       @index                = index
143:       @missing_gems         = Hash.new(0)
144:       @source_requirements  = source_requirements
145:     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 127
127:     def self.resolve(requirements, index, source_requirements = {}, base = [])
128:       base = SpecSet.new(base) unless base.is_a?(SpecSet)
129:       resolver = new(index, source_requirements, base)
130:       result = catch(:success) do
131:         resolver.start(requirements)
132:         raise resolver.version_conflict
133:         nil
134:       end
135:       SpecSet.new(result)
136:     end

Public Instance Methods

clean_req(req) click to toggle source
     # File lib/bundler/resolver.rb, line 378
378:     def clean_req(req)
379:       if req.to_s.include?(">= 0")
380:         req.to_s.gsub(/ \(.*?\)$/, '')
381:       else
382:         req.to_s.gsub(/\, (runtime|development)\)$/, ')')
383:       end
384:     end
debug() click to toggle source
     # File lib/bundler/resolver.rb, line 147
147:     def debug
148:       if ENV['DEBUG_RESOLVER']
149:         debug_info = yield
150:         debug_info = debug_info.inpsect unless debug_info.is_a?(String)
151:         $stderr.puts debug_info
152:       end
153:     end
error_message() click to toggle source
     # File lib/bundler/resolver.rb, line 405
405:     def error_message
406:       output = errors.inject("") do |o, (conflict, (origin, requirement))|
407: 
408:         # origin is the SpecSet of specs from the Gemfile that is conflicted with
409:         if origin
410: 
411:           o << %{Bundler could not find compatible versions for gem "#{origin.name}":\n}
412:           o << "  In Gemfile:\n"
413: 
414:           o << gem_message(requirement)
415: 
416:           # If the origin is a LockfileParser, it does not respond_to :required_by
417:           unless origin.respond_to?(:required_by) && required_by = origin.required_by.first
418:             o << "  In snapshot (Gemfile.lock):\n"
419:           end
420: 
421:           o << gem_message(origin)
422: 
423:         # origin is nil if the required gem and version cannot be found in any of
424:         # the specified sources
425:         else
426: 
427:           # if the gem cannot be found because of a version conflict between lockfile and gemfile,
428:           # print a useful error that suggests running `bundle update`, which may fix things
429:           #
430:           # @base is a SpecSet of the gems in the lockfile
431:           # conflict is the name of the gem that could not be found
432:           if locked = @base[conflict].first
433:             o << "Bundler could not find compatible versions for gem #{conflict.inspect}:\n"
434:             o << "  In snapshot (Gemfile.lock):\n"
435:             o << "    #{clean_req(locked)}\n\n"
436: 
437:             o << "  In Gemfile:\n"
438:             o << gem_message(requirement)
439:             o << "Running `bundle update` will rebuild your snapshot from scratch, using only\n"
440:             o << "the gems in your Gemfile, which may resolve the conflict.\n"
441: 
442:           # the rest of the time, the gem cannot be found because it does not exist in the known sources
443:           else
444:             if requirement.required_by.first
445:               o << "Could not find gem '#{clean_req(requirement)}', required by '#{clean_req(requirement.required_by.first)}', in any of the sources\n"
446:             else
447:               o << "Could not find gem '#{clean_req(requirement)} in any of the sources\n"
448:             end
449:           end
450: 
451:         end
452:       end
453:     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 391
391:     def gem_message(requirement)
392:       m = ""
393: 
394:       # A requirement that is required by itself is actually in the Gemfile, and does
395:       # not "depend on" itself
396:       if requirement.required_by.first && requirement.required_by.first.name != requirement.name
397:         m << "    #{clean_req(requirement.required_by.first)} depends on\n"
398:         m << "      #{clean_req(requirement)}\n"
399:       else
400:         m << "    #{clean_req(requirement)}\n"
401:       end
402:       m << "\n"
403:     end
resolve(reqs, activated) click to toggle source
     # File lib/bundler/resolver.rb, line 165
165:     def resolve(reqs, activated)
166:       # If the requirements are empty, then we are in a success state. Aka, all
167:       # gem dependencies have been resolved.
168:       throw :success, successify(activated) if reqs.empty?
169: 
170:       debug { print "\e[2J\e[f" ; "==== Iterating ====\n\n" }
171: 
172:       # Sort dependencies so that the ones that are easiest to resolve are first.
173:       # Easiest to resolve is defined by:
174:       #   1) Is this gem already activated?
175:       #   2) Do the version requirements include prereleased gems?
176:       #   3) Sort by number of gems available in the source.
177:       reqs = reqs.sort_by do |a|
178:         [ activated[a.name] ? 0 : 1,
179:           a.requirement.prerelease? ? 0 : 1,
180:           @errors[a.name]   ? 0 : 1,
181:           activated[a.name] ? 0 : search(a).size ]
182:       end
183: 
184:       debug { "Activated:\n" + activated.values.map { |a| "  #{a.name} (#{a.version})" }.join("\n") }
185:       debug { "Requirements:\n" + reqs.map { |r| "  #{r.name} (#{r.requirement})"}.join("\n") }
186: 
187:       activated = activated.dup
188: 
189:       # Pull off the first requirement so that we can resolve it
190:       current = reqs.shift
191: 
192:       debug { "Attempting:\n  #{current.name} (#{current.requirement})"}
193: 
194:       # Check if the gem has already been activated, if it has, we will make sure
195:       # that the currently activated gem satisfies the requirement.
196:       existing = activated[current.name]
197:       if existing || current.name == 'bundler'
198:         # Force the current
199:         if current.name == 'bundler' && !existing
200:           existing = search(DepProxy.new(Gem::Dependency.new('bundler', VERSION), Gem::Platform::RUBY)).first
201:           activated['bundler'] = existing
202:           raise GemNotFound, %{Bundler could not find gem "bundler" (#{VERSION})} unless 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:           reqs.concat existing.activate_platform(current.__platform)
215: 
216:           resolve(reqs, activated)
217:         else
218:           debug { "    * [FAIL] Already activated" }
219:           @errors[existing.name] = [existing, current]
220:           debug { current.required_by.map {|d| "      * #{d.name} (#{d.requirement})" }.join("\n") }
221:           # debug { "    * All current conflicts:\n" + @errors.keys.map { |c| "      - #{c}" }.join("\n") }
222:           # Since the current requirement conflicts with an activated gem, we need
223:           # to backtrack to the current requirement's parent and try another version
224:           # of it (maybe the current requirement won't be present anymore). If the
225:           # current requirement is a root level requirement, we need to jump back to
226:           # where the conflicting gem was activated.
227:           parent = current.required_by.last
228:           # `existing` could not respond to required_by if it is part of the base set
229:           # of specs that was passed to the resolver (aka, instance of LazySpecification)
230:           parent ||= existing.required_by.last if existing.respond_to?(:required_by)
231:           # We track the spot where the current gem was activated because we need
232:           # to keep a list of every spot a failure happened.
233:           debug { "    -> Jumping to: #{parent.name}" }
234:           if parent
235:             throw parent.name, existing.respond_to?(:required_by) && existing.required_by.last.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."
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 353
353:     def search(dep)
354:       if base = @base[dep.name] and base.any?
355:         d = Gem::Dependency.new(base.first.name, *[dep.requirement.as_list, base.first.version].flatten)
356:       else
357:         d = dep.dep
358:       end
359:       index = @source_requirements[d.name] || @index
360:       results = index.search_for_all_platforms(d, @base[d.name])
361: 
362:       if results.any?
363:         version = results.first.version
364:         nested  = [[]]
365:         results.each do |spec|
366:           if spec.version != version
367:             nested << []
368:             version = spec.version
369:           end
370:           nested.last << spec
371:         end
372:         nested.map { |a| SpecGroup.new(a) }.select { |sg| sg.for?(dep.__platform) }
373:       else
374:         []
375:       end
376:     end
start(reqs) click to toggle source
     # File lib/bundler/resolver.rb, line 159
159:     def start(reqs)
160:       activated = {}
161: 
162:       resolve(reqs, activated)
163:     end
successify(activated) click to toggle source
     # File lib/bundler/resolver.rb, line 155
155:     def successify(activated)
156:       activated.values.map { |s| s.to_specs }.flatten.compact
157:     end
version_conflict() click to toggle source
     # File lib/bundler/resolver.rb, line 386
386:     def version_conflict
387:       VersionConflict.new(errors.keys, error_message)
388:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.