Module: Homebrew::Upgrade Private

Defined in:
upgrade.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for upgrading formulae.

Defined Under Namespace

Classes: Dependents

Class Method Summary collapse

Class Method Details

.dependants(formulae, flags:, dry_run: false, ask: false, installed_on_request: false, force_bottle: false, build_from_source_formulae: [], interactive: false, keep_tmp: false, debug_symbols: false, force: false, debug: false, quiet: false, verbose: false) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



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
# File 'upgrade.rb', line 269

def self.dependants(
  formulae,
  flags:,
  dry_run: false,
  ask: false,
  installed_on_request: false,
  force_bottle: false,
  build_from_source_formulae: [],
  interactive: false,
  keep_tmp: false,
  debug_symbols: false,
  force: false,
  debug: false,
  quiet: false,
  verbose: false
)
  if Homebrew::EnvConfig.no_installed_dependents_check?
    unless Homebrew::EnvConfig.no_env_hints?
      opoo <<~EOS
        HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK is set: not checking for outdated
        dependents or dependents with broken linkage!
      EOS
    end
    return
  end
  formulae_to_install = formulae.dup
  formulae_to_install.reject! { |f| f.core_formula? && f.versioned_formula? }
  return if formulae_to_install.empty?

  already_broken_dependents = check_broken_dependents(formulae_to_install)

  # TODO: this should be refactored to use FormulaInstaller new logic
  outdated_dependents =
    formulae_to_install.flat_map(&:runtime_installed_formula_dependents)
                       .uniq
                       .select(&:outdated?)

  # Ensure we never attempt a source build for outdated dependents of upgraded formulae.
  outdated_dependents, skipped_dependents = outdated_dependents.partition do |dependent|
    dependent.bottled? && dependent.deps.map(&:to_formula).all?(&:bottled?)
  end

  return if outdated_dependents.blank? && already_broken_dependents.blank?

  outdated_dependents -= formulae_to_install if dry_run

  upgradeable_dependents =
    outdated_dependents.reject(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }
  pinned_dependents =
    outdated_dependents.select(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }

  Dependents.new(upgradeable_dependents, pinned_dependents, skipped_dependents)
end

.formula_installers(formulae_to_install, flags:, dry_run: false, force_bottle: false, build_from_source_formulae: [], dependents: false, interactive: false, keep_tmp: false, debug_symbols: false, force: false, overwrite: false, debug: false, quiet: false, verbose: false) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'upgrade.rb', line 16

def self.formula_installers(
  formulae_to_install,
  flags:,
  dry_run: false,
  force_bottle: false,
  build_from_source_formulae: [],
  dependents: false,
  interactive: false,
  keep_tmp: false,
  debug_symbols: false,
  force: false,
  overwrite: false,
  debug: false,
  quiet: false,
  verbose: false
)
  return if formulae_to_install.empty?

  # Sort keg-only before non-keg-only formulae to avoid any needless conflicts
  # with outdated, non-keg-only versions of formulae being upgraded.
  formulae_to_install.sort! do |a, b|
    if !a.keg_only? && b.keg_only?
      1
    elsif a.keg_only? && !b.keg_only?
      -1
    else
      0
    end
  end

  dependency_graph = Utils::TopologicalHash.graph_package_dependencies(formulae_to_install)
  begin
    formulae_to_install = dependency_graph.tsort & formulae_to_install
  rescue TSort::Cyclic
    raise CyclicDependencyError, dependency_graph.strongly_connected_components if Homebrew::EnvConfig.developer?
  end

  formulae_to_install.filter_map do |formula|
    Migrator.migrate_if_needed(formula, force:, dry_run:)
    begin
      fi = create_formula_installer(
        formula,
        flags:,
        force_bottle:,
        build_from_source_formulae:,
        interactive:,
        keep_tmp:,
        debug_symbols:,
        force:,
        overwrite:,
        debug:,
        quiet:,
        verbose:,
      )
      fi.fetch_bottle_tab(quiet: !debug)

      all_runtime_deps_installed = fi.bottle_tab_runtime_dependencies.presence&.all? do |dependency, hash|
        minimum_version = Version.new(hash["version"]) if hash["version"].present?
        Dependency.new(dependency).installed?(minimum_version:, minimum_revision: hash["revision"])
      end

      if !dry_run && dependents && all_runtime_deps_installed
        # Don't need to install this bottle if all of the runtime
        # dependencies have the same or newer version already installed.
        next
      end

      fi
    rescue CannotInstallFormulaError => e
      ofail e
      nil
    rescue UnsatisfiedRequirements, DownloadError => e
      ofail "#{formula}: #{e}"
      nil
    end
  end
end

.install_formula(formula_installer, upgrade:) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



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 'upgrade.rb', line 210

def self.install_formula(formula_installer, upgrade:)
  formula = formula_installer.formula

  formula_installer.check_installation_already_attempted

  if upgrade
    print_upgrade_message(formula, formula_installer.options)

    kegs = outdated_kegs(formula)
    linked_kegs = kegs.select(&:linked?)
  else
    formula.print_tap_action
  end

  # first we unlink the currently active keg for this formula otherwise it is
  # possible for the existing build to interfere with the build we are about to
  # do! Seriously, it happens!
  kegs.each(&:unlink) if kegs.present?

  formula_installer.install
  formula_installer.finish
rescue FormulaInstallationAlreadyAttemptedError
  # We already attempted to upgrade f as part of the dependency tree of
  # another formula. In that case, don't generate an error, just move on.
  nil
ensure
  # restore previous installation state if build failed
  begin
    linked_kegs&.each(&:link) unless formula.latest_version_installed?
  rescue
    nil
  end
end

.puts_no_installed_dependents_check_disable_message_if_not_already!Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



259
260
261
262
263
264
265
266
267
# File 'upgrade.rb', line 259

def self.puts_no_installed_dependents_check_disable_message_if_not_already!
  return if Homebrew::EnvConfig.no_env_hints?
  return if Homebrew::EnvConfig.no_installed_dependents_check?
  return if @puts_no_installed_dependents_check_disable_message_if_not_already

  puts "Disable this behaviour by setting HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK."
  puts "Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`)."
  @puts_no_installed_dependents_check_disable_message_if_not_already = true
end

.upgrade_dependents(deps, formulae, flags:, dry_run: false, installed_on_request: false, force_bottle: false, build_from_source_formulae: [], interactive: false, keep_tmp: false, debug_symbols: false, force: false, debug: false, quiet: false, verbose: false) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



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
388
389
390
391
392
393
394
395
396
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'upgrade.rb', line 325

def self.upgrade_dependents(deps, formulae,
                            flags:,
                            dry_run: false,
                            installed_on_request: false,
                            force_bottle: false,
                            build_from_source_formulae: [],
                            interactive: false,
                            keep_tmp: false,
                            debug_symbols: false,
                            force: false,
                            debug: false,
                            quiet: false,
                            verbose: false)
  return if deps.blank?

  upgradeable = deps.upgradeable
  pinned      = deps.pinned
  skipped     = deps.skipped
  if pinned.present?
    plural = Utils.pluralize("dependent", pinned.count)
    opoo "Not upgrading #{pinned.count} pinned #{plural}:"
    puts(pinned.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end
  if skipped.present?
    opoo <<~EOS
      The following dependents of upgraded formulae are outdated but will not
      be upgraded because they are not bottled:
        #{skipped * "\n  "}
    EOS
  end
  # Print the upgradable dependents.
  if upgradeable.blank?
    ohai "No outdated dependents to upgrade!" unless dry_run
  else
    installed_formulae = (dry_run ? formulae : FormulaInstaller.installed.to_a).dup
    formula_plural = Utils.pluralize("formula", installed_formulae.count, plural: "e")
    upgrade_verb = dry_run ? "Would upgrade" : "Upgrading"
    ohai "#{upgrade_verb} #{Utils.pluralize("dependent", upgradeable.count,
                                            include_count: true)} of upgraded #{formula_plural}:"
    Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
    formulae_upgrades = upgradeable.map do |f|
      name = f.full_specified_name
      if f.optlinked?
        "#{name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
      else
        "#{name} #{f.pkg_version}"
      end
    end
    puts formulae_upgrades.join(", ")
  end

  upgradeable.reject! { |f| FormulaInstaller.installed.include?(f) }

  return if upgradeable.blank?

  unless dry_run
    dependent_installers = formula_installers(
      upgradeable,
      flags:,
      force_bottle:,
      build_from_source_formulae:,
      dependents:                 true,
      interactive:,
      keep_tmp:,
      debug_symbols:,
      force:,
      debug:,
      quiet:,
      verbose:,
    )
    upgrade_formulae(dependent_installers, dry_run: dry_run, verbose: verbose)
  end

  # Update installed formulae after upgrading
  installed_formulae = FormulaInstaller.installed.to_a

  # Assess the dependents tree again now we've upgraded.
  unless dry_run
    oh1 "Checking for dependents of upgraded formulae..."
    Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
  end

  broken_dependents = check_broken_dependents(installed_formulae)
  if broken_dependents.blank?
    if dry_run
      ohai "No currently broken dependents found!"
      opoo "If they are broken by the upgrade they will also be upgraded or reinstalled."
    else
      ohai "No broken dependents found!"
    end
    return
  end

  reinstallable_broken_dependents =
    broken_dependents.reject(&:outdated?)
                     .reject(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }
  outdated_pinned_broken_dependents =
    broken_dependents.select(&:outdated?)
                     .select(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }

  # Print the pinned dependents.
  if outdated_pinned_broken_dependents.present?
    count = outdated_pinned_broken_dependents.count
    plural = Utils.pluralize("dependent", outdated_pinned_broken_dependents.count)
    onoe "Not reinstalling #{count} broken and outdated, but pinned #{plural}:"
    $stderr.puts(outdated_pinned_broken_dependents.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end

  # Print the broken dependents.
  if reinstallable_broken_dependents.blank?
    ohai "No broken dependents to reinstall!"
  else
    ohai "Reinstalling #{Utils.pluralize("dependent", reinstallable_broken_dependents.count,
                                         include_count: true)} with broken linkage from source:"
    Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
    puts reinstallable_broken_dependents.map(&:full_specified_name)
                                        .join(", ")
  end

  return if dry_run

  reinstallable_broken_dependents.each do |formula|
    formula_installer = Reinstall.build_install_context(
      formula,
      flags:,
      force_bottle:,
      build_from_source_formulae: build_from_source_formulae + [formula.full_name],
      interactive:,
      keep_tmp:,
      debug_symbols:,
      force:,
      debug:,
      quiet:,
      verbose:,
    )
    Reinstall.reinstall_formula(
      formula_installer,
      flags:,
      force_bottle:,
      build_from_source_formulae:,
      interactive:,
      keep_tmp:,
      debug_symbols:,
      force:,
      debug:,
      quiet:,
      verbose:,
    )
  rescue FormulaInstallationAlreadyAttemptedError
    # We already attempted to reinstall f as part of the dependency tree of
    # another formula. In that case, don't generate an error, just move on.
    nil
  rescue CannotInstallFormulaError, DownloadError => e
    ofail e
  rescue BuildError => e
    e.dump(verbose:)
    puts
    Homebrew.failed = true
  end
end

.upgrade_formula(formula_installer, dry_run: false, verbose: false) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'upgrade.rb', line 188

def self.upgrade_formula(formula_installer, dry_run: false, verbose: false)
  formula = formula_installer.formula

  if dry_run
    Install.print_dry_run_dependencies(formula, formula_installer.compute_dependencies) do |f|
      name = f.full_specified_name
      if f.optlinked?
        "#{name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
      else
        "#{name} #{f.pkg_version}"
      end
    end
    return
  end

  install_formula(formula_installer, upgrade: true)
rescue BuildError => e
  e.dump(verbose:)
  puts
  Homebrew.failed = true
end

.upgrade_formulae(formula_installers, dry_run: false, verbose: false) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'upgrade.rb', line 94

def self.upgrade_formulae(formula_installers, dry_run: false, verbose: false)
  unless dry_run
    formula_installers.each do |fi|
      fi.prelude
      fi.fetch
    rescue CannotInstallFormulaError => e
      ofail e
    rescue UnsatisfiedRequirements, DownloadError => e
      ofail "#{fi.formula.full_name}: #{e}"
    end
  end

  formula_installers.each do |fi|
    upgrade_formula(fi, dry_run:, verbose:)
    Cleanup.install_formula_clean!(fi.formula, dry_run:)
  end
end