Class: Zeitwerk::Loader

Inherits:
Object
  • Object
show all
Extended by:
Internal, RealModName
Includes:
Callbacks, Config, EagerLoad, Helpers, RealModName
Defined in:
lib/zeitwerk/loader.rb

Direct Known Subclasses

GemLoader

Defined Under Namespace

Modules: Callbacks, Config, EagerLoad, Helpers

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Config

#inflector, #logger, #roots

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RealModName

real_mod_name

Methods included from Internal

internal

Methods included from EagerLoad

#eager_load, #eager_load_dir, #eager_load_namespace, #load_file

Methods included from Config

#collapse, #dirs, #do_not_eager_load, #enable_reloading, #ignore, #log!, #on_load, #on_setup, #on_unload, #push_dir, #reloading_enabled?, #tag, #tag=

Constructor Details

#initializeLoader

Returns a new instance of Loader.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zeitwerk/loader.rb', line 107

def initialize
  super

  @autoloads       = {}
  @inceptions      = Zeitwerk::Cref::Map.new
  @autoloaded_dirs = []
  @to_unload       = {}
  @namespace_dirs  = Zeitwerk::Cref::Map.new
  @shadowed_files  = Set.new
  @setup           = false
  @eager_loaded    = false

  @mutex = Mutex.new
  @dirs_autoload_monitor = Monitor.new

  Registry.loaders.register(self)
end

Class Attribute Details

.default_loggerObject

: call(String) -> void | debug(String) -> void | nil



372
373
374
# File 'lib/zeitwerk/loader.rb', line 372

def default_logger
  @default_logger
end

Instance Attribute Details

#autoloaded_dirsObject (readonly)

We keep track of autoloaded directories to remove them from the registry at the end of eager loading.

Files are removed as they are autoloaded, but directories need to wait due to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).

: Array



66
67
68
# File 'lib/zeitwerk/loader.rb', line 66

def autoloaded_dirs
  @autoloaded_dirs
end

#autoloadsObject (readonly)

Maps absolute paths for which an autoload has been set —and not executed— to their corresponding Zeitwerk::Cref object.

"/Users/fxn/blog/app/models/user.rb"          => #<Zeitwerk::Cref:... @mod=Object, @cname=:User, ...>,
"/Users/fxn/blog/app/models/hotel/pricing.rb" => #<Zeitwerk::Cref:... @mod=Hotel, @cname=:Pricing, ...>,
...

: Hash[String, Zeitwerk::Cref]



32
33
34
# File 'lib/zeitwerk/loader.rb', line 32

def autoloads
  @autoloads
end

#inceptionsObject (readonly)

When the path passed to Module#autoload is in the stack of features being loaded at the moment, Ruby passes. For example, Module#autoload? returns ‘nil` even if the autoload has not been attempted. See

https://bugs.ruby-lang.org/issues/21035

We call these “inceptions”.

A common case is the entry point of gems managed by Zeitwerk. Their main file is normally required and, while doing so, the loader sets an autoload on the gem namespace. That autoload hits this edge case.

There is some logic that neeeds to know if an autoload for a given constant already exists. We check Module#autoload? first, and fallback to the inceptions just in case.

This map keeps track of pairs (cref, autoload_path) found by the loader. The object Zeitwerk::Registry.inceptions, on the other hand, acts as a global registry for them.

: Zeitwerk::Cref::Map



56
57
58
# File 'lib/zeitwerk/loader.rb', line 56

def inceptions
  @inceptions
end

#namespace_dirsObject (readonly)

Maps namespace crefs to the directories that conform the namespace.

When these crefs get defined we know their children are spread over those directories. We’ll visit them to set up the corresponding autoloads.

: Zeitwerk::Cref::Map



85
86
87
# File 'lib/zeitwerk/loader.rb', line 85

def namespace_dirs
  @namespace_dirs
end

#shadowed_filesObject (readonly)

A shadowed file is a file managed by this loader that is ignored when setting autoloads because its matching constant is already taken.

This private set is populated lazily, as we descend. For example, if the loader has only scanned the top-level, ‘shadowed_files` does not have the shadowed files that may exist deep in the project tree.

: Set



96
97
98
# File 'lib/zeitwerk/loader.rb', line 96

def shadowed_files
  @shadowed_files
end

#to_unloadObject (readonly)

If reloading is enabled, this collection maps autoload paths to their autoloaded crefs.

On unload, the autoload paths are passed to callbacks, files deleted from $LOADED_FEATURES, and the crefs are deleted.

: Hash[String, Zeitwerk::Cref]



76
77
78
# File 'lib/zeitwerk/loader.rb', line 76

def to_unload
  @to_unload
end

Class Method Details

.all_dirsObject

Returns an array with the absolute paths of the root directories of all registered loaders. This is a read-only collection.

: () -> Array



456
457
458
459
460
461
462
# File 'lib/zeitwerk/loader.rb', line 456

def all_dirs
  dirs = []
  Registry.loaders.each do |loader|
    dirs.concat(loader.dirs)
  end
  dirs.freeze
end

.eager_load_allObject

Broadcasts ‘eager_load` to all loaders. Those that have not been setup are skipped.

: () -> void



428
429
430
431
432
433
434
435
436
# File 'lib/zeitwerk/loader.rb', line 428

def eager_load_all
  Registry.loaders.each do |loader|
    begin
      loader.eager_load
    rescue SetupRequired
      # This is fine, we eager load what can be eager loaded.
    end
  end
end

.eager_load_namespace(mod) ⇒ Object

Broadcasts ‘eager_load_namespace` to all loaders. Those that have not been setup are skipped.

: (Module) -> void



442
443
444
445
446
447
448
449
450
# File 'lib/zeitwerk/loader.rb', line 442

def eager_load_namespace(mod)
  Registry.loaders.each do |loader|
    begin
      loader.eager_load_namespace(mod)
    rescue SetupRequired
      # This is fine, we eager load what can be eager loaded.
    end
  end
end

.for_gem(warn_on_extra_files: true) ⇒ Object

This is a shortcut for

require "zeitwerk"

loader = Zeitwerk::Loader.new
loader.tag = File.basename(__FILE__, ".rb")
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
loader.push_dir(__dir__)

except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload.

This method returns a subclass of Zeitwerk::Loader, but the exact type is private, client code can only rely on the interface.

: (?warn_on_extra_files: boolish) -> Zeitwerk::GemLoader



390
391
392
393
# File 'lib/zeitwerk/loader.rb', line 390

def for_gem(warn_on_extra_files: true)
  called_from = caller_locations(1, 1).first.path
  Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
end

.for_gem_extension(namespace) ⇒ Object

This is a shortcut for

require "zeitwerk"

loader = Zeitwerk::Loader.new
loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb")
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
loader.push_dir(__dir__, namespace: namespace)

except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload.

This method returns a subclass of Zeitwerk::Loader, but the exact type is private, client code can only rely on the interface.

: (Module) -> Zeitwerk::GemLoader



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/zeitwerk/loader.rb', line 411

def for_gem_extension(namespace)
  unless namespace.is_a?(Module) # Note that Class < Module.
    raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
  end

  unless real_mod_name(namespace)
    raise Zeitwerk::Error, "extending anonymous namespaces is unsupported"
  end

  called_from = caller_locations(1, 1).first.path
  Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false)
end

Instance Method Details

#all_expected_cpathsObject

Returns a hash that maps the absolute paths of the managed files and directories to their respective expected constant paths.

: () -> Hash[String, String]



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/zeitwerk/loader.rb', line 247

def all_expected_cpaths
  result = {}

  actual_roots.each do |root_dir, root_namespace|
    queue = [[root_dir, real_mod_name(root_namespace)]]

    while (dir, cpath = queue.shift)
      result[dir] = cpath

      prefix = cpath == "Object" ? "" : cpath + "::"

      ls(dir) do |basename, abspath, ftype|
        if ftype == :file
          basename.delete_suffix!(".rb")
          result[abspath] = prefix + inflector.camelize(basename, abspath)
        else
          if collapse?(abspath)
            queue << [abspath, cpath]
          else
            queue << [abspath, prefix + inflector.camelize(basename, abspath)]
          end
        end
      end
    end
  end

  result
end

#cpath_expected_at(path) ⇒ Object

: (String | Pathname) -> String?

Raises:



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
# File 'lib/zeitwerk/loader.rb', line 277

def cpath_expected_at(path)
  abspath = File.expand_path(path)

  raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)

  return unless dir?(abspath) || ruby?(abspath)
  return if ignored_path?(abspath)

  paths = []

  if ruby?(abspath)
    basename = File.basename(abspath, ".rb")
    return if hidden?(basename)

    paths << [basename, abspath]
    walk_up_from = File.dirname(abspath)
  else
    walk_up_from = abspath
  end

  root_namespace = nil

  walk_up(walk_up_from) do |dir|
    break if root_namespace = roots[dir]
    return if ignored_path?(dir)

    basename = File.basename(dir)
    return if hidden?(basename)

    paths << [basename, dir] unless collapse?(dir)
  end

  return unless root_namespace

  if paths.empty?
    real_mod_name(root_namespace)
  else
    cnames = paths.reverse_each.map { cname_for(_1, _2) }

    if root_namespace == Object
      cnames.join("::")
    else
      "#{real_mod_name(root_namespace)}::#{cnames.join("::")}"
    end
  end
end

#reloadObject

Unloads all loaded code, and calls setup again so that the loader is able to pick any changes in the file system.

This method is not thread-safe, please see how this can be achieved by client code in the README of the project.

: () -> void ! Zeitwerk::Error



233
234
235
236
237
238
239
240
241
# File 'lib/zeitwerk/loader.rb', line 233

def reload
  raise ReloadingDisabledError unless reloading_enabled?
  raise SetupRequired unless @setup

  unload
  recompute_ignored_paths
  recompute_collapse_dirs
  setup
end

#setupObject

Sets autoloads in the root namespaces.

: () -> void



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/zeitwerk/loader.rb', line 128

def setup
  mutex.synchronize do
    break if @setup

    actual_roots.each do |root_dir, root_namespace|
      define_autoloads_for_dir(root_dir, root_namespace)
    end

    on_setup_callbacks.each(&:call)

    @setup = true
  end
end

#unloadObject

Removes loaded constants and configured autoloads.

The objects the constants stored are no longer reachable through them. In addition, since said objects are normally not referenced from anywhere else, they are eligible for garbage collection, which would effectively unload them.

This method is public but undocumented. Main interface is ‘reload`, which means `unload` + `setup`. This one is available to be used together with `unregister`, which is undocumented too.

: () -> void



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/zeitwerk/loader.rb', line 154

def unload
  mutex.synchronize do
    raise SetupRequired unless @setup

    # We are going to keep track of the files that were required by our
    # autoloads to later remove them from $LOADED_FEATURES, thus making them
    # loadable by Kernel#require again.
    #
    # Directories are not stored in $LOADED_FEATURES, keeping track of files
    # is enough.
    unloaded_files = Set.new

    autoloads.each do |abspath, cref|
      if cref.autoload?
        unload_autoload(cref)
      else
        # Could happen if loaded with require_relative. That is unsupported,
        # and the constant path would escape unloadable_cpath? This is just
        # defensive code to clean things up as much as we are able to.
        unload_cref(cref)
        unloaded_files.add(abspath) if ruby?(abspath)
      end
    end

    to_unload.each do |abspath, cref|
      unless on_unload_callbacks.empty?
        begin
          value = cref.get
        rescue ::NameError
          # Perhaps the user deleted the constant by hand, or perhaps an
          # autoload failed to define the expected constant but the user
          # rescued the exception.
        else
          run_on_unload_callbacks(cref, value, abspath)
        end
      end

      unload_cref(cref)
      unloaded_files.add(abspath) if ruby?(abspath)
    end

    unless unloaded_files.empty?
      # Bootsnap decorates Kernel#require to speed it up using a cache and
      # this optimization does not check if $LOADED_FEATURES has the file.
      #
      # To make it aware of changes, the gem defines singleton methods in
      # $LOADED_FEATURES:
      #
      #   https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb
      #
      # Rails applications may depend on bootsnap, so for unloading to work
      # in that setting it is preferable that we restrict our API choice to
      # one of those methods.
      $LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) }
    end

    autoloads.clear
    autoloaded_dirs.clear
    to_unload.clear
    namespace_dirs.clear
    shadowed_files.clear

    unregister_inceptions
    unregister_explicit_namespaces

    Registry.autoloads.unregister_loader(self)

    @setup        = false
    @eager_loaded = false
  end
end

#unloadable_cpath?(cpath) ⇒ Boolean

Says if the given constant path would be unloaded on reload. This predicate returns ‘false` if reloading is disabled.

This is an undocumented method that I wrote to help transition from the classic autoloader in Rails. Its usage was removed from Rails in 7.0.

: (String) -> bool

Returns:

  • (Boolean)


331
332
333
# File 'lib/zeitwerk/loader.rb', line 331

def unloadable_cpath?(cpath)
  unloadable_cpaths.include?(cpath)
end

#unloadable_cpathsObject

Returns an array with the constant paths that would be unloaded on reload. This predicate returns an empty array if reloading is disabled.

This is an undocumented method that I wrote to help transition from the classic autoloader in Rails. Its usage was removed from Rails in 7.0.

: () -> Array



342
343
344
# File 'lib/zeitwerk/loader.rb', line 342

def unloadable_cpaths
  to_unload.values.map(&:path)
end

#unregisterObject

This is a dangerous method.

: () -> void



350
351
352
353
354
355
356
# File 'lib/zeitwerk/loader.rb', line 350

def unregister
  unregister_inceptions
  unregister_explicit_namespaces
  Registry.loaders.unregister(self)
  Registry.autoloads.unregister_loader(self)
  Registry.unregister_loader(self)
end