Class: Zeitwerk::Loader

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/zeitwerk/loader.rb

Defined Under Namespace

Modules: Callbacks

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#on_dir_autoloaded, #on_file_autoloaded, #on_namespace_loaded

Constructor Details

#initializeLoader

Returns a new instance of Loader.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zeitwerk/loader.rb', line 126

def initialize
  @initialized_at = Time.now

  @tag       = SecureRandom.hex(3)
  @inflector = Inflector.new
  @logger    = self.class.default_logger

  @root_dirs             = {}
  @preloads              = []
  @ignored               = Set.new
  @ignored_paths         = Set.new
  @autoloads             = {}
  @autoloaded_dirs       = []
  @to_unload             = {}
  @lazy_subdirs          = {}
  @eager_load_exclusions = Set.new

  # TODO: find a better name for these mutexes.
  @mutex        = Mutex.new
  @mutex2       = Mutex.new
  @setup        = false
  @eager_loaded = false

  @reloading_enabled = false

  Registry.register_loader(self)
end

Class Attribute Details

.default_logger#call, ...

Returns:

  • (#call, #debug, nil)


398
399
400
# File 'lib/zeitwerk/loader.rb', line 398

def default_logger
  @default_logger
end

.mutexMutex

Returns:

  • (Mutex)


402
403
404
# File 'lib/zeitwerk/loader.rb', line 402

def mutex
  @mutex
end

Instance Attribute Details

#autoloaded_dirs<String> (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).

Returns:

  • (<String>)


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

def autoloaded_dirs
  @autoloaded_dirs
end

#autoloads{String => (Module, Symbol)} (readonly)

Maps real absolute paths for which an autoload has been set —and not executed— to their corresponding parent class or module and constant name.

"/Users/fxn/blog/app/models/user.rb"          => [Object, :User],
"/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
...

Returns:

  • ({String => (Module, Symbol)})


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

def autoloads
  @autoloads
end

#eager_load_exclusionsSet<String> (readonly)

Absolute paths of files or directories not to be eager loaded.

Returns:

  • (Set<String>)


116
117
118
# File 'lib/zeitwerk/loader.rb', line 116

def eager_load_exclusions
  @eager_load_exclusions
end

#ignoredSet<String> (readonly)

Absolute paths of files, directories, of glob patterns to be totally ignored.

Returns:

  • (Set<String>)


46
47
48
# File 'lib/zeitwerk/loader.rb', line 46

def ignored
  @ignored
end

#ignored_pathsSet<String> (readonly)

The actual collection of absolute file and directory names at the time the ignored glob patterns were expanded. Computed on setup, and recomputed on reload.

Returns:

  • (Set<String>)


54
55
56
# File 'lib/zeitwerk/loader.rb', line 54

def ignored_paths
  @ignored_paths
end

#inflector#camelize

Returns:

  • (#camelize)


15
16
17
# File 'lib/zeitwerk/loader.rb', line 15

def inflector
  @inflector
end

#lazy_subdirs{String => <String>} (readonly)

Maps constant paths of namespaces to arrays of corresponding directories.

For example, given this mapping:

"Admin" => [
  "/Users/fxn/blog/app/controllers/admin",
  "/Users/fxn/blog/app/models/admin",
  ...
]

when ‘Admin` gets defined we know that it plays the role of a namespace and that its children are spread over those directories. We’ll visit them to set up the corresponding autoloads.

Returns:

  • ({String => <String>})


110
111
112
# File 'lib/zeitwerk/loader.rb', line 110

def lazy_subdirs
  @lazy_subdirs
end

#logger#call, ...

Returns:

  • (#call, #debug, nil)


18
19
20
# File 'lib/zeitwerk/loader.rb', line 18

def logger
  @logger
end

#mutexMutex (readonly)

Returns:

  • (Mutex)


120
121
122
# File 'lib/zeitwerk/loader.rb', line 120

def mutex
  @mutex
end

#mutex2Mutex (readonly)

Returns:

  • (Mutex)


124
125
126
# File 'lib/zeitwerk/loader.rb', line 124

def mutex2
  @mutex2
end

#preloads<String> (readonly)

Absolute paths of files or directories that have to be preloaded.

Returns:

  • (<String>)


39
40
41
# File 'lib/zeitwerk/loader.rb', line 39

def preloads
  @preloads
end

#root_dirs{String => true} (readonly)

Absolute paths of the root directories. Stored in a hash to preserve order, easily handle duplicates, and also be able to have a fast lookup, needed for detecting nested paths.

"/Users/fxn/blog/app/assets"   => true,
"/Users/fxn/blog/app/channels" => true,
...

This is a private collection maintained by the loader. The public interface for it is ‘push_dir` and `dirs`.

Returns:

  • ({String => true})


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

def root_dirs
  @root_dirs
end

#tagString

Returns:

  • (String)


12
13
14
# File 'lib/zeitwerk/loader.rb', line 12

def tag
  @tag
end

#to_unload{String => (String, (Module, Symbol))} (readonly)

Stores metadata needed for unloading. Its entries look like this:

"Admin::Role" => [".../admin/role.rb", [Admin, :Role]]

The cpath as key helps implementing unloadable_cpath? The real file name is stored in order to be able to delete it from $LOADED_FEATURES, and the pair [Module, Symbol] is used to remove_const the constant from the class or module object.

If reloading is enabled, this hash is filled as constants are autoloaded or eager loaded. Otherwise, the collection remains empty.

Returns:

  • ({String => (String, (Module, Symbol))})


92
93
94
# File 'lib/zeitwerk/loader.rb', line 92

def to_unload
  @to_unload
end

Class Method Details

.all_dirs<String>

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

Returns:

  • (<String>)


432
433
434
# File 'lib/zeitwerk/loader.rb', line 432

def all_dirs
  Registry.loaders.flat_map(&:dirs).freeze
end

.eager_load_allvoid

This method returns an undefined value.

Broadcasts ‘eager_load` to all loaders.



424
425
426
# File 'lib/zeitwerk/loader.rb', line 424

def eager_load_all
  Registry.loaders.each(&:eager_load)
end

.for_gemZeitwerk::Loader

This is a shortcut for

require "zeitwerk"
loader = Zeitwerk::Loader.new
loader.tag = File.basename(__FILE__, ".rb")
loader.inflector = Zeitwerk::GemInflector.new
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.

Returns:



416
417
418
419
# File 'lib/zeitwerk/loader.rb', line 416

def for_gem
  called_from = caller_locations.first.path
  Registry.loader_for_gem(called_from)
end

Instance Method Details

#dirs<String>

Absolute paths of the root directories. This is a read-only collection, please push here via ‘push_dir`.

Returns:

  • (<String>)


165
166
167
# File 'lib/zeitwerk/loader.rb', line 165

def dirs
  root_dirs.keys.freeze
end

#do_not_eager_load(*paths) ⇒ void

This method returns an undefined value.

Let eager load ignore the given files or directories. The constants defined in those files are still autoloadable.

Parameters:

  • paths (<String, Pathname, <String, Pathname>>)


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

def do_not_eager_load(*paths)
  mutex.synchronize { eager_load_exclusions.merge(expand_paths(paths)) }
end

#eager_loadvoid

This method returns an undefined value.

Eager loads all files in the root directories, recursively. Files do not need to be in ‘$LOAD_PATH`, absolute file names are used. Ignored files are not eager loaded. You can opt-out specifically in specific files and directories with `do_not_eager_load`.



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

def eager_load
  mutex.synchronize do
    break if @eager_loaded

    queue = actual_root_dirs.reject { |dir| eager_load_exclusions.member?(dir) }
    while dir = queue.shift
      const_get_if_autoload(dir)

      each_abspath(dir) do |abspath|
        next if eager_load_exclusions.member?(abspath)

        if ruby?(abspath)
          const_get_if_autoload(abspath)
        elsif dir?(abspath)
          queue << abspath
        end
      end
    end

    autoloaded_dirs.each do |autoloaded_dir|
      Registry.unregister_autoload(autoloaded_dir)
    end
    autoloaded_dirs.clear

    @eager_loaded = true
  end
end

#enable_reloadingvoid

This method returns an undefined value.

You need to call this method before setup in order to be able to reload. There is no way to undo this, either you want to reload or you don’t.

Raises:



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/zeitwerk/loader.rb', line 193

def enable_reloading
  mutex.synchronize do
    break if @reloading_enabled

    if @setup
      raise Error, "cannot enable reloading after setup"
    else
      @reloading_enabled = true
    end
  end
end

#expand_ignored_glob_patternsvoid

This method returns an undefined value.



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

def expand_ignored_glob_patterns
  # Note that Dir.glob works with regular file names just fine. That is,
  # glob patterns technically need no wildcards.
  ignored_paths.replace(ignored.flat_map { |path| Dir.glob(path) })
end

#ignore(*paths) ⇒ void

This method returns an undefined value.

Configure files, directories, or glob patterns to be totally ignored.

Parameters:

  • paths (<String, Pathname, <String, Pathname>>)


227
228
229
# File 'lib/zeitwerk/loader.rb', line 227

def ignore(*paths)
  mutex.synchronize { ignored.merge(expand_paths(paths)) }
end

#preload(*paths) ⇒ void

This method returns an undefined value.

Files or directories to be preloaded instead of lazy loaded.

Parameters:

  • paths (<String, Pathname, <String, Pathname>>)


214
215
216
217
218
219
220
221
# File 'lib/zeitwerk/loader.rb', line 214

def preload(*paths)
  mutex.synchronize do
    expand_paths(paths).each do |abspath|
      preloads << abspath
      do_preload_abspath(abspath) if @setup
    end
  end
end

#push_dir(path) ⇒ void

This method returns an undefined value.

Pushes ‘path` to the list of root directories.

Raises ‘Zeitwerk::Error` if `path` does not exist, or if another loader in the same process already manages that directory or one of its ascendants or descendants.

Parameters:

  • path (<String, Pathname>)

Raises:



178
179
180
181
182
183
184
185
186
# File 'lib/zeitwerk/loader.rb', line 178

def push_dir(path)
  abspath = File.expand_path(path)
  if dir?(abspath)
    raise_if_conflicting_directory(abspath)
    root_dirs[abspath] = true
  else
    raise Error, "the root directory #{abspath} does not exist"
  end
end

#reloadvoid

This method returns an undefined value.

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.

Raises:



325
326
327
328
329
330
331
332
# File 'lib/zeitwerk/loader.rb', line 325

def reload
  if reloading_enabled?
    unload
    setup
  else
    raise ReloadingDisabledError, "can't reload, please call loader.enable_reloading before setup"
  end
end

#reloading_enabled?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/zeitwerk/loader.rb', line 206

def reloading_enabled?
  @reloading_enabled
end

#setupvoid

This method returns an undefined value.

Sets autoloads in the root namespace and preloads files, if any.



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/zeitwerk/loader.rb', line 242

def setup
  mutex.synchronize do
    break if @setup

    expand_ignored_glob_patterns
    actual_root_dirs.each { |root_dir| set_autoloads_in_dir(root_dir, Object) }
    do_preload

    @setup = true
  end
end

#unloadvoid

This method returns an undefined value.

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.



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

def unload
  mutex.synchronize do
    # 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 |realpath, (parent, cname)|
      if parent.autoload?(cname)
        unload_autoload(parent, cname)
      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(parent, cname)   if cdef?(parent, cname)
        unloaded_files.add(realpath) if ruby?(realpath)
      end
    end

    to_unload.each_value do |(realpath, (parent, cname))|
      unload_cref(parent, cname)   if cdef?(parent, cname)
      unloaded_files.add(realpath) if ruby?(realpath)
    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
    lazy_subdirs.clear

    Registry.on_unload(self)
    ExplicitNamespace.unregister(self)

    @setup = 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.

Parameters:

  • cpath (String)

Returns:

  • (Boolean)


382
383
384
# File 'lib/zeitwerk/loader.rb', line 382

def unloadable_cpath?(cpath)
  to_unload.key?(cpath)
end

#unloadable_cpaths<String>

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

Returns:

  • (<String>)


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

def unloadable_cpaths
  to_unload.keys.freeze
end