Module: Homebrew::Bootsnap Private

Defined in:
startup/bootsnap.rb,
startup/bootsnap.rbi

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.

Class Method Summary collapse

Class Method Details

.core_gem_namesArray<String>

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.

Returns:



20
# File 'startup/bootsnap.rb', line 20

def self.core_gem_names = CORE_GEM_NAMES

.keyString

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.

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'startup/bootsnap.rb', line 26

def self.key
  @key ||= begin
    require "digest/sha2"

    checksum = Digest::SHA256.new
    checksum << RUBY_VERSION
    checksum << RUBY_PLATFORM
    checksum << gem_directories
                .select { |gem| core_gem_names.any? { |name| gem.start_with?("#{name}-") } }
                .join(",")

    checksum.hexdigest
  end
end

.load!(compile_cache: true) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • compile_cache (Boolean) (defaults to: true)


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
93
94
95
96
97
98
# File 'startup/bootsnap.rb', line 66

def self.load!(compile_cache: true)
  return unless enabled?

  begin
    require ENV.fetch("HOMEBREW_BOOTSNAP_GEM_PATH")
  rescue LoadError
    return
  end

  installed_gem_directories = gem_directories.join(",")
  gem_directories_cache = File.join(cache_dir, "bootsnap/gem-directories")
  if !File.exist?(gem_directories_cache) || File.read(gem_directories_cache) != installed_gem_directories
    # The compile cache is shared across optional groups, but Bootsnap treats
    # gem load paths as immutable, so invalidate their separate index.
    require "fileutils"
    FileUtils.rm_f load_path_cache
    FileUtils.mkdir_p File.dirname(gem_directories_cache)
    File.write(gem_directories_cache, installed_gem_directories)
  end

  ::Bootsnap.setup(
    cache_dir:,
    ignore_directories:,
    # In development environments the bootsnap compilation cache is
    # generated on the fly when source files are loaded.
    # https://github.com/Shopify/bootsnap?tab=readme-ov-file#precompilation
    development_mode:   true,
    load_path_cache:    true,
    # Ruby refuses InstructionSequence#to_binary while Coverage is active.
    compile_cache_iseq: compile_cache && ENV["HOMEBREW_TESTS_COVERAGE"].nil?,
    compile_cache_yaml: compile_cache,
  )
end

.prewarm!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.

Compile caches for the load graphs of common commands in a detached background process, so the next brew command doesn't pay the cost of compiling caches for Ruby files changed by e.g. brew update.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'startup/bootsnap.rb', line 116

def self.prewarm!
  return unless enabled?
  return if ENV["HOMEBREW_TESTS"]

  pid = Process.spawn(
    *HOMEBREW_RUBY_EXEC_ARGS,
    "-I", $LOAD_PATH.join(File::PATH_SEPARATOR),
    "-rglobal", "-rcmd/install", "-rcmd/fetch", "-rcmd/upgrade",
    "-e", "",
    in: File::NULL, out: File::NULL, err: File::NULL, pgroup: true
  )
  Process.detach(pid)
rescue SystemCallError
  nil
end

.reset!void

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.

This method returns an undefined value.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'startup/bootsnap.rb', line 100

def self.reset!
  return unless enabled?

  ::Bootsnap.unload_cache!
  # Gem changes invalidate resolution, but compiled files validate themselves
  # against their source contents, so only discard the persisted load path.
  require "fileutils"
  FileUtils.rm_f load_path_cache
  @key = nil

  load!(compile_cache: false)
end