Class: Bundler::Resolver
- Inherits:
-
Object
- Object
- Bundler::Resolver
- Defined in:
- lib/bundler/resolver.rb
Defined Under Namespace
Constant Summary collapse
- ALL =
Bundler::Dependency::PLATFORM_MAP.values.uniq.freeze
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#initial_reqs ⇒ Object
readonly
Returns the value of attribute initial_reqs.
-
#iteration_counter ⇒ Object
readonly
Returns the value of attribute iteration_counter.
-
#iteration_rate ⇒ Object
readonly
Returns the value of attribute iteration_rate.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Class Method Summary collapse
-
.resolve(requirements, index, source_requirements = {}, base = []) ⇒ 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.
Instance Method Summary collapse
- #activate_gem(reqs, activated, requirement, current) ⇒ Object
- #clean_req(req) ⇒ Object
- #clear_search_cache ⇒ Object
- #debug ⇒ Object
- #dependency_tree(m, requirements) ⇒ Object
- #error_message ⇒ Object
- #find_conflict_state(conflict, states) ⇒ Object
- #find_state(current, states) ⇒ Object
-
#gem_message(requirement, required_by = []) ⇒ Object
For a given conflicted requirement, print out what exactly went wrong.
- #gems_size(dep) ⇒ Object
- #handle_conflict(current, states, existing = nil) ⇒ Object
-
#initialize(index, source_requirements, base) ⇒ Resolver
constructor
A new instance of Resolver.
- #other_possible?(conflict, states) ⇒ Boolean
- #reset_state ⇒ Object
- #resolve(reqs, activated, current_traversal) ⇒ Object
- #resolve_conflict(current, states) ⇒ Object
- #resolve_for_conflict(state) ⇒ Object
- #search(dep) ⇒ Object
- #start(reqs, current_traversal = false) ⇒ Object
- #state_any?(state) ⇒ Boolean
- #successify(activated) ⇒ Object
- #version_conflict(current_traversal = true) ⇒ Object
Constructor Details
#initialize(index, source_requirements, base) ⇒ Resolver
Returns a new instance of Resolver.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bundler/resolver.rb', line 137 def initialize(index, source_requirements, base) @initial_reqs = [] @errors = {} @base = base @index = index @deps_for = {} @missing_gems = Hash.new(0) @prereleases_cache = Hash.new { |h,k| h[k] = k.prerelease? } @source_requirements = source_requirements @iteration_counter = 0 @started_at = Time.now end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
113 114 115 |
# File 'lib/bundler/resolver.rb', line 113 def errors @errors end |
#initial_reqs ⇒ Object (readonly)
Returns the value of attribute initial_reqs.
113 114 115 |
# File 'lib/bundler/resolver.rb', line 113 def initial_reqs @initial_reqs end |
#iteration_counter ⇒ Object (readonly)
Returns the value of attribute iteration_counter.
113 114 115 |
# File 'lib/bundler/resolver.rb', line 113 def iteration_counter @iteration_counter end |
#iteration_rate ⇒ Object (readonly)
Returns the value of attribute iteration_rate.
113 114 115 |
# File 'lib/bundler/resolver.rb', line 113 def iteration_rate @iteration_rate end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
113 114 115 |
# File 'lib/bundler/resolver.rb', line 113 def started_at @started_at end |
Class Method Details
.resolve(requirements, index, source_requirements = {}, base = []) ⇒ 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.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bundler/resolver.rb', line 125 def self.resolve(requirements, index, source_requirements = {}, base = []) Bundler.ui.info "Resolving dependencies...", false base = SpecSet.new(base) unless base.is_a?(SpecSet) resolver = new(index, source_requirements, base) result = resolver.start(requirements) Bundler.ui.info "" # new line now that dots are done SpecSet.new(result) rescue => e Bundler.ui.info "" # new line before the error raise e end |
Instance Method Details
#activate_gem(reqs, activated, requirement, current) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bundler/resolver.rb', line 212 def activate_gem(reqs, activated, requirement, current) requirement.required_by.replace current.required_by requirement.required_by << current activated[requirement.name] = requirement debug { " Activating: #{requirement.name} (#{requirement.version})" } debug { requirement.required_by.map { |d| " * #{d.name} (#{d.requirement})" }.join("\n") } dependencies = requirement.activate_platform(current.__platform) debug { " Dependencies"} dependencies.each do |dep| next if dep.type == :development dep.required_by.replace(current.required_by) dep.required_by << current @gems_size[dep] ||= gems_size(dep) reqs << dep end end |
#clean_req(req) ⇒ Object
426 427 428 429 430 431 432 |
# File 'lib/bundler/resolver.rb', line 426 def clean_req(req) if req.to_s.include?(">= 0") req.to_s.gsub(/ \(.*?\)$/, '') else req.to_s.gsub(/\, (runtime|development)\)$/, ')') end end |
#clear_search_cache ⇒ Object
393 394 395 |
# File 'lib/bundler/resolver.rb', line 393 def clear_search_cache @deps_for = {} end |
#debug ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/bundler/resolver.rb', line 150 def debug if ENV['DEBUG_RESOLVER'] debug_info = yield debug_info = debug_info.inspect unless debug_info.is_a?(String) $stderr.puts debug_info end end |
#dependency_tree(m, requirements) ⇒ Object
460 461 462 463 464 465 466 467 |
# File 'lib/bundler/resolver.rb', line 460 def dependency_tree(m, requirements) requirements.each_with_index do |i, j| m << " " << (" " * j) m << "#{clean_req(i)}" m << " depends on\n" end m << " " << (" " * requirements.size) end |
#error_message ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/bundler/resolver.rb', line 469 def errors.inject("") do |o, (conflict, (origin, requirement))| # origin is the SpecSet of specs from the Gemfile that is conflicted with if origin o << %{Bundler could not find compatible versions for gem "#{origin.name}":\n} o << " In Gemfile:\n" required_by = requirement.required_by o << (requirement, required_by) # If the origin is "bundler", the conflict is us if origin.name == "bundler" o << " Current Bundler version:\n" other_bundler_required = !requirement.requirement.satisfied_by?(origin.version) # If the origin is a LockfileParser, it does not respond_to :required_by elsif !origin.respond_to?(:required_by) || !(origin.required_by.first) o << " In snapshot (Gemfile.lock):\n" end required_by = origin.required_by[0..-2] o << (origin, required_by) # If the bundle wants a newer bundler than the running bundler, explain if origin.name == "bundler" && other_bundler_required o << "This Gemfile requires a different version of Bundler.\n" o << "Perhaps you need to update Bundler by running `gem install bundler`?" end # origin is nil if the required gem and version cannot be found in any of # the specified sources else # if the gem cannot be found because of a version conflict between lockfile and gemfile, # print a useful error that suggests running `bundle update`, which may fix things # # @base is a SpecSet of the gems in the lockfile # conflict is the name of the gem that could not be found if locked = @base[conflict].first o << "Bundler could not find compatible versions for gem #{conflict.inspect}:\n" o << " In snapshot (Gemfile.lock):\n" o << " #{clean_req(locked)}\n\n" o << " In Gemfile:\n" required_by = requirement.required_by o << (requirement, required_by) o << "Running `bundle update` will rebuild your snapshot from scratch, using only\n" o << "the gems in your Gemfile, which may resolve the conflict.\n" # the rest of the time, the gem cannot be found because it does not exist in the known sources else if requirement.required_by.first o << "Could not find gem '#{clean_req(requirement)}', which is required by " o << "gem '#{clean_req(requirement.required_by.first)}', in any of the sources." else o << "Could not find gem '#{clean_req(requirement)} in any of the sources\n" end end end o end end |
#find_conflict_state(conflict, states) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/bundler/resolver.rb', line 204 def find_conflict_state(conflict, states) return unless conflict until states.empty? do state = states.pop return state if conflict.name == state.name end end |
#find_state(current, states) ⇒ Object
194 195 196 |
# File 'lib/bundler/resolver.rb', line 194 def find_state(current, states) states.detect { |i| current && current.name == i.name } end |
#gem_message(requirement, required_by = []) ⇒ Object
For a given conflicted requirement, print out what exactly went wrong
446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/bundler/resolver.rb', line 446 def (requirement, required_by=[]) m = "" # A requirement that is required by itself is actually in the Gemfile, and does # not "depend on" itself if requirement.required_by.first && requirement.required_by.first.name != requirement.name dependency_tree(m, required_by) m << "#{clean_req(requirement)}\n" else m << " #{clean_req(requirement)}\n" end m << "\n" end |
#gems_size(dep) ⇒ Object
389 390 391 |
# File 'lib/bundler/resolver.rb', line 389 def gems_size(dep) search(dep).size end |
#handle_conflict(current, states, existing = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bundler/resolver.rb', line 176 def handle_conflict(current, states, existing=nil) until current.nil? current_state = find_state(current, states) return current if state_any?(current_state) current = current.required_by.last if current end until existing.nil? existing_state = find_state(existing, states) return existing if state_any?(existing_state) existing = existing.required_by.last if existing end end |
#other_possible?(conflict, states) ⇒ Boolean
198 199 200 201 202 |
# File 'lib/bundler/resolver.rb', line 198 def other_possible?(conflict, states) return unless conflict state = states.detect { |i| i.name == conflict.name } state && state.possibles.any? end |
#reset_state ⇒ Object
440 441 442 443 |
# File 'lib/bundler/resolver.rb', line 440 def reset_state clear_search_cache @errors = {} end |
#resolve(reqs, activated, current_traversal) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/bundler/resolver.rb', line 260 def resolve(reqs, activated, current_traversal) states = [] depth = 0 conflicts = Set.new until reqs.empty? indicate_progress debug { print "\e[2J\e[f" ; "==== Iterating ====\n\n" } reqs = reqs.sort_by do |a| [ activated[a.name] ? 0 : 1, @prereleases_cache[a.requirement] ? 0 : 1, @errors[a.name] ? 0 : 1, activated[a.name] ? 0 : @gems_size[a] ] end debug { "Activated:\n" + activated.values.map {|a| " #{a}" }.join("\n") } debug { "Requirements:\n" + reqs.map {|r| " #{r}"}.join("\n") } current = reqs.shift $stderr.puts "#{' ' * depth}#{current}" if ENV['DEBUG_RESOLVER_TREE'] debug { "Attempting:\n #{current}"} existing = activated[current.name] if existing || current.name == 'bundler' # Force the current if current.name == 'bundler' && !existing existing = search(DepProxy.new(Gem::Dependency.new('bundler', VERSION), Gem::Platform::RUBY)).first raise GemNotFound, %Q{Bundler could not find gem "bundler" (#{VERSION})} unless existing existing.required_by << existing activated['bundler'] = existing end if current.requirement.satisfied_by?(existing.version) debug { " * [SUCCESS] Already activated" } @errors.delete(existing.name) dependencies = existing.activate_platform(current.__platform) reqs.concat dependencies dependencies.each do |dep| next if dep.type == :development @gems_size[dep] ||= gems_size(dep) end depth += 1 next else debug { " * [FAIL] Already activated" } @errors[existing.name] = [existing, current] conflicts << current.name parent = current.required_by.last if current_traversal parent = handle_conflict(current, states) else parent = handle_conflict(parent, states) end if parent.nil? && !conflicts.empty? parent = states.reverse.detect { |i| conflicts.include?(i.name) && state_any?(i)} end if existing.respond_to?(:required_by) parent = handle_conflict(parent, states, existing.required_by[-2]) unless other_possible?(parent, states) end return version_conflict(current_traversal) if parent.nil? || parent.name == 'bundler' reqs, activated, depth, conflicts = resolve_conflict(parent, states) end else matching_versions = search(current) # If we found no versions that match the current requirement if matching_versions.empty? # If this is a top-level Gemfile requirement if current.required_by.empty? if base = @base[current.name] and !base.empty? version = base.first.version = "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}`" elsif current.source name = current.name versions = @source_requirements[name][name].map { |s| s.version } = "Could not find gem '#{current}' in #{current.source}.\n" if versions.any? << "Source contains '#{name}' at: #{versions.join(', ')}" else << "Source does not contain any versions of '#{current}'" end else = "Could not find gem '#{current}' " if @index.source_types.include?(Bundler::Source::Rubygems) << "in any of the gem sources listed in your Gemfile." else << "in the gems available on this machine." end end raise GemNotFound, # This is not a top-level Gemfile requirement else @errors[current.name] = [nil, current] parent = handle_conflict(current, states) reqs, activated, depth = resolve_conflict(parent, states) next end end state = State.new(reqs.dup, activated.dup, current, matching_versions, depth, conflicts) states << state requirement = state.possibles.pop activate_gem(reqs, activated, requirement, current) end end successify(activated) end |
#resolve_conflict(current, states) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/bundler/resolver.rb', line 243 def resolve_conflict(current, states) # Find the state where the conflict has occurred state = find_conflict_state(current, states) debug { " -> Going to: #{current.name} state" } if current # Resolve the conflicts by rewinding the state # when the conflicted gem was activated reqs, activated, depth, conflicts = resolve_for_conflict(state) # Keep the state around if it still has other possibilities states << state unless state.possibles.empty? clear_search_cache return reqs, activated, depth, conflicts end |
#resolve_for_conflict(state) ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bundler/resolver.rb', line 232 def resolve_for_conflict(state) return version_conflict if state.nil? || state.possibles.empty? reqs, activated, depth, conflicts = state.reqs.dup, state.activated.dup, state.depth, state.conflicts.dup requirement = state.requirement possible = state.possibles.pop activate_gem(reqs, activated, possible, requirement) return reqs, activated, depth, conflicts end |
#search(dep) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/bundler/resolver.rb', line 397 def search(dep) if base = @base[dep.name] and base.any? reqs = [dep.requirement.as_list, base.first.version.to_s].flatten.compact d = Gem::Dependency.new(base.first.name, *reqs) else d = dep.dep end @deps_for[d.hash] ||= begin index = @source_requirements[d.name] || @index results = index.search(d, @base[d.name]) if results.any? version = results.first.version nested = [[]] results.each do |spec| if spec.version != version nested << [] version = spec.version end nested.last << spec end deps = nested.map{|a| SpecGroup.new(a) }.select{|sg| sg.for?(dep.__platform) } else deps = [] end end end |
#start(reqs, current_traversal = false) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/bundler/resolver.rb', line 162 def start(reqs, current_traversal=false) @initial_reqs = reqs.dup unless current_traversal activated = {} @gems_size = Hash[reqs.map { |r| [r, gems_size(r)] }] resolve(reqs, activated, current_traversal) end |
#state_any?(state) ⇒ Boolean
190 191 192 |
# File 'lib/bundler/resolver.rb', line 190 def state_any?(state) state && state.possibles.any? end |
#successify(activated) ⇒ Object
158 159 160 |
# File 'lib/bundler/resolver.rb', line 158 def successify(activated) activated.values.map { |s| s.to_specs }.flatten.compact end |
#version_conflict(current_traversal = true) ⇒ Object
434 435 436 437 438 |
# File 'lib/bundler/resolver.rb', line 434 def version_conflict(current_traversal=true) raise VersionConflict.new(errors.keys, ) if current_traversal reset_state start(initial_reqs, true) end |