zeitwerk 2.6.0 → 2.6.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +430 -51
- data/lib/zeitwerk/error.rb +6 -0
- data/lib/zeitwerk/explicit_namespace.rb +14 -10
- data/lib/zeitwerk/gem_inflector.rb +2 -2
- data/lib/zeitwerk/gem_loader.rb +12 -9
- data/lib/zeitwerk/internal.rb +12 -0
- data/lib/zeitwerk/kernel.rb +6 -7
- data/lib/zeitwerk/loader/callbacks.rb +25 -17
- data/lib/zeitwerk/loader/config.rb +95 -51
- data/lib/zeitwerk/loader/eager_load.rb +234 -0
- data/lib/zeitwerk/loader/helpers.rb +92 -21
- data/lib/zeitwerk/loader.rb +224 -129
- data/lib/zeitwerk/null_inflector.rb +5 -0
- data/lib/zeitwerk/registry.rb +2 -2
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +2 -0
- metadata +6 -3
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[](https://rubygems.org/gems/zeitwerk)
|
6
|
-
[](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3Amain)
|
7
|
+
|
7
8
|
|
8
9
|
<!-- TOC -->
|
9
10
|
|
@@ -24,14 +25,22 @@
|
|
24
25
|
- [Setup](#setup)
|
25
26
|
- [Generic](#generic)
|
26
27
|
- [for_gem](#for_gem)
|
28
|
+
- [for_gem_extension](#for_gem_extension)
|
27
29
|
- [Autoloading](#autoloading)
|
28
30
|
- [Eager loading](#eager-loading)
|
29
31
|
- [Eager load exclusions](#eager-load-exclusions)
|
32
|
+
- [Eager load directories](#eager-load-directories)
|
33
|
+
- [Eager load namespaces](#eager-load-namespaces)
|
34
|
+
- [Eager load namespaces shared by several loaders](#eager-load-namespaces-shared-by-several-loaders)
|
30
35
|
- [Global eager load](#global-eager-load)
|
36
|
+
- [Loading individual files](#loading-individual-files)
|
31
37
|
- [Reloading](#reloading)
|
38
|
+
- [Configuration and usage](#configuration-and-usage)
|
39
|
+
- [Thread-safety](#thread-safety)
|
32
40
|
- [Inflection](#inflection)
|
33
41
|
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
34
42
|
- [Zeitwerk::GemInflector](#zeitwerkgeminflector)
|
43
|
+
- [Zeitwerk::NullInflector](#zeitwerknullinflector)
|
35
44
|
- [Custom inflector](#custom-inflector)
|
36
45
|
- [Callbacks](#callbacks)
|
37
46
|
- [The on_setup callback](#the-on_setup-callback)
|
@@ -44,15 +53,17 @@
|
|
44
53
|
- [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
|
45
54
|
- [Use case: The adapter pattern](#use-case-the-adapter-pattern)
|
46
55
|
- [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
|
56
|
+
- [Shadowed files](#shadowed-files)
|
47
57
|
- [Edge cases](#edge-cases)
|
48
58
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
49
59
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
60
|
+
- [Introspection](#introspection)
|
61
|
+
- [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
|
62
|
+
- [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
|
63
|
+
- [`Zeitwerk::Loader#all_expected_cpaths`](#zeitwerkloaderall_expected_cpaths)
|
50
64
|
- [Encodings](#encodings)
|
51
65
|
- [Rules of thumb](#rules-of-thumb)
|
52
66
|
- [Debuggers](#debuggers)
|
53
|
-
- [debug.rb](#debugrb)
|
54
|
-
- [Byebug](#byebug)
|
55
|
-
- [Break](#break)
|
56
67
|
- [Pronunciation](#pronunciation)
|
57
68
|
- [Supported Ruby versions](#supported-ruby-versions)
|
58
69
|
- [Testing](#testing)
|
@@ -70,15 +81,15 @@
|
|
70
81
|
|
71
82
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
72
83
|
|
73
|
-
Given a [conventional file structure](#file-structure), Zeitwerk is
|
84
|
+
Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
|
74
85
|
|
75
|
-
Zeitwerk
|
86
|
+
Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
|
76
87
|
|
77
|
-
The gem is designed
|
88
|
+
The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
|
78
89
|
|
79
|
-
Internally, Zeitwerk
|
90
|
+
Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
|
80
91
|
|
81
|
-
Furthermore, Zeitwerk
|
92
|
+
Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
|
82
93
|
|
83
94
|
<a id="markdown-synopsis" name="synopsis"></a>
|
84
95
|
## Synopsis
|
@@ -138,7 +149,7 @@ Zeitwerk::Loader.eager_load_all
|
|
138
149
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
139
150
|
### The idea: File paths match constant paths
|
140
151
|
|
141
|
-
|
152
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
142
153
|
|
143
154
|
```
|
144
155
|
lib/my_gem.rb -> MyGem
|
@@ -147,7 +158,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
147
158
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
148
159
|
```
|
149
160
|
|
150
|
-
You can tune
|
161
|
+
You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
|
151
162
|
|
152
163
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
153
164
|
### Inner simple constants
|
@@ -168,7 +179,7 @@ class HttpCrawler
|
|
168
179
|
end
|
169
180
|
```
|
170
181
|
|
171
|
-
The first example needs a custom [inflection](
|
182
|
+
The first example needs a custom [inflection](#inflection) rule:
|
172
183
|
|
173
184
|
```ruby
|
174
185
|
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
@@ -205,7 +216,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
205
216
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
206
217
|
#### Custom root namespaces
|
207
218
|
|
208
|
-
|
219
|
+
Although `Object` is the most common root namespace, you have the flexibility to associate a different one with a specific root directory. The `push_dir` method accepts a non-anonymous class or module object as the optional `namespace` keyword argument.
|
209
220
|
|
210
221
|
For example, given:
|
211
222
|
|
@@ -221,14 +232,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
221
232
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
222
233
|
```
|
223
234
|
|
224
|
-
Please
|
235
|
+
Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
|
225
236
|
|
226
237
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
227
238
|
#### Nested root directories
|
228
239
|
|
229
|
-
Root directories
|
240
|
+
Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
|
230
241
|
|
231
|
-
Zeitwerk
|
242
|
+
Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
|
232
243
|
|
233
244
|
```
|
234
245
|
app/models/concerns/geolocatable.rb
|
@@ -239,9 +250,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
239
250
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
240
251
|
### Implicit namespaces
|
241
252
|
|
242
|
-
If a namespace
|
253
|
+
If a namespace consists only of a simple module without any code, there is no need to explicitly define it in a separate file. Zeitwerk automatically creates modules on your behalf for directories without a corresponding Ruby file.
|
243
254
|
|
244
|
-
For
|
255
|
+
For instance, suppose a project includes an `admin` directory:
|
245
256
|
|
246
257
|
```
|
247
258
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
@@ -249,7 +260,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
249
260
|
|
250
261
|
and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
|
251
262
|
|
252
|
-
|
263
|
+
To trigger this behavior, the directory must contain non-ignored Ruby files with the ".rb" extension, either directly or recursively. Otherwise, the directory is ignored. This condition is reevaluated during reloads.
|
253
264
|
|
254
265
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
255
266
|
### Explicit namespaces
|
@@ -364,7 +375,7 @@ require "zeitwerk"
|
|
364
375
|
loader = Zeitwerk::Loader.new
|
365
376
|
loader.tag = File.basename(__FILE__, ".rb")
|
366
377
|
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
367
|
-
loader.push_dir(
|
378
|
+
loader.push_dir(File.dirname(__FILE__))
|
368
379
|
```
|
369
380
|
|
370
381
|
If the main module references project constants at the top-level, Zeitwerk has to be ready to load them. Their definitions, in turn, may reference other project constants. And this is recursive. Therefore, it is important that the `setup` call happens above the main module definition:
|
@@ -383,6 +394,12 @@ module MyGem
|
|
383
394
|
end
|
384
395
|
```
|
385
396
|
|
397
|
+
Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`, which is the standard way to load a gem. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work.
|
398
|
+
|
399
|
+
`Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
400
|
+
|
401
|
+
If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](#reopening-third-party-namespaces) down below.
|
402
|
+
|
386
403
|
Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
|
387
404
|
|
388
405
|
For example, if the gem has Rails generators under `lib/generators`, by convention that directory defines a `Generators` Ruby module. If `generators` is just a container for non-autoloadable code and templates, not acting as a project namespace, you need to setup things accordingly.
|
@@ -399,9 +416,64 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
399
416
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
400
417
|
```
|
401
418
|
|
402
|
-
|
419
|
+
<a id="markdown-for_gem_extension" name="for_gem_extension"></a>
|
420
|
+
#### for_gem_extension
|
421
|
+
|
422
|
+
Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/):
|
423
|
+
|
424
|
+
* The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
|
425
|
+
* The namespace should be `Net::HTTP::NicheFeature`.
|
426
|
+
* The entry point should be `lib/net/http/niche_feature.rb`.
|
427
|
+
* Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.
|
428
|
+
|
429
|
+
The top-level file mentioned in the last point is optional. In particular, from
|
430
|
+
|
431
|
+
```ruby
|
432
|
+
gem "net-http-niche_feature"
|
433
|
+
```
|
434
|
+
|
435
|
+
if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
|
436
|
+
|
437
|
+
Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
|
438
|
+
|
439
|
+
The structure of the gem would be like this:
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
# lib/net-http-niche_feature.rb (optional)
|
443
|
+
|
444
|
+
# For technical reasons, this cannot be require_relative.
|
445
|
+
require "net/http/niche_feature"
|
446
|
+
|
447
|
+
|
448
|
+
# lib/net/http/niche_feature.rb
|
449
|
+
|
450
|
+
require "net/http"
|
451
|
+
require "zeitwerk"
|
452
|
+
|
453
|
+
loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
|
454
|
+
loader.setup
|
455
|
+
|
456
|
+
module Net::HTTP::NicheFeature
|
457
|
+
# Since the setup has been performed, at this point we are already able
|
458
|
+
# to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
|
459
|
+
include MyMixin
|
460
|
+
end
|
461
|
+
|
403
462
|
|
404
|
-
|
463
|
+
# lib/net/http/niche_feature/version.rb
|
464
|
+
|
465
|
+
module Net::HTTP::NicheFeature
|
466
|
+
VERSION = "1.0.0"
|
467
|
+
end
|
468
|
+
```
|
469
|
+
|
470
|
+
`Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
|
471
|
+
|
472
|
+
If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
|
473
|
+
|
474
|
+
Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file.
|
475
|
+
|
476
|
+
`Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
405
477
|
|
406
478
|
<a id="markdown-autoloading" name="autoloading"></a>
|
407
479
|
### Autoloading
|
@@ -437,10 +509,12 @@ loader.eager_load
|
|
437
509
|
|
438
510
|
That skips [ignored files and directories](#ignoring-parts-of-the-project).
|
439
511
|
|
440
|
-
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](
|
512
|
+
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
|
441
513
|
|
442
514
|
Eager loading is synchronized and idempotent.
|
443
515
|
|
516
|
+
Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
517
|
+
|
444
518
|
<a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
|
445
519
|
#### Eager load exclusions
|
446
520
|
|
@@ -463,6 +537,81 @@ Which may be handy if the project eager loads in the test suite to [ensure proje
|
|
463
537
|
|
464
538
|
The `force` flag does not affect ignored files and directories, those are still ignored.
|
465
539
|
|
540
|
+
<a id="markdown-eager-load-directories" name="eager-load-directories"></a>
|
541
|
+
#### Eager load directories
|
542
|
+
|
543
|
+
The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
|
544
|
+
|
545
|
+
```ruby
|
546
|
+
loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
|
547
|
+
```
|
548
|
+
|
549
|
+
This is useful when the loader is not eager loading the entire project, but you still need some subtree to be loaded for things to function properly.
|
550
|
+
|
551
|
+
Both strings and `Pathname` objects are supported as arguments. If the argument is not a directory managed by the receiver, the method raises `Zeitwerk::Error`.
|
552
|
+
|
553
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
554
|
+
|
555
|
+
`Zeitwerk::Loader#eager_load_dir` is idempotent, but compatible with reloading. If you eager load a directory and then reload, eager loading that directory will load its (current) contents again.
|
556
|
+
|
557
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
558
|
+
|
559
|
+
Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
|
560
|
+
|
561
|
+
Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
562
|
+
|
563
|
+
<a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
|
564
|
+
#### Eager load namespaces
|
565
|
+
|
566
|
+
The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
|
567
|
+
|
568
|
+
```ruby
|
569
|
+
loader.eager_load_namespace(MyApp::Routes)
|
570
|
+
```
|
571
|
+
|
572
|
+
This is useful when the loader is not eager loading the entire project, but you still need some namespace to be loaded for things to function properly.
|
573
|
+
|
574
|
+
The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
|
575
|
+
|
576
|
+
If the namespace is spread over multiple directories in the receiver's source tree, they are all eager loaded. For example, if you have a structure like
|
577
|
+
|
578
|
+
```
|
579
|
+
root_dir1/my_app/routes
|
580
|
+
root_dir2/my_app/routes
|
581
|
+
root_dir3/my_app/routes
|
582
|
+
```
|
583
|
+
|
584
|
+
where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
585
|
+
|
586
|
+
There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
|
587
|
+
|
588
|
+
This method is flexible about what it accepts. Its semantics have to be interpreted as: "_If_ you manage this namespace, or part of this namespace, please eager load what you got". In particular, if the receiver does not manage the namespace, it will simply do nothing, this is not an error condition.
|
589
|
+
|
590
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
591
|
+
|
592
|
+
`Zeitwerk::Loader#eager_load_namespace` is idempotent, but compatible with reloading. If you eager load a namespace and then reload, eager loading that namespace will load its (current) descendants again.
|
593
|
+
|
594
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
595
|
+
|
596
|
+
If root directories are assigned to custom namespaces, the method behaves as you'd expect, according to the namespacing relationship between the custom namespace and the argument.
|
597
|
+
|
598
|
+
Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
599
|
+
|
600
|
+
<a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
|
601
|
+
#### Eager load namespaces shared by several loaders
|
602
|
+
|
603
|
+
The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
|
604
|
+
|
605
|
+
```ruby
|
606
|
+
Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
|
607
|
+
```
|
608
|
+
|
609
|
+
This may be handy, for example, if a framework supports plugins and a shared namespace needs to be eager loaded for the framework to function properly.
|
610
|
+
|
611
|
+
Please, note that loaders only eager load namespaces they manage, as documented above. Therefore, this method does not allow you to eager load namespaces not managed by Zeitwerk loaders.
|
612
|
+
|
613
|
+
This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
|
614
|
+
|
466
615
|
<a id="markdown-global-eager-load" name="global-eager-load"></a>
|
467
616
|
#### Global eager load
|
468
617
|
|
@@ -478,9 +627,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
|
|
478
627
|
|
479
628
|
This method does not accept the `force` flag, since in general it wouldn't be a good idea to force eager loading in 3rd party code.
|
480
629
|
|
630
|
+
This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
|
631
|
+
|
632
|
+
<a id="markdown-loading-individual-files" name="loading-individual-files"></a>
|
633
|
+
### Loading individual files
|
634
|
+
|
635
|
+
The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
|
636
|
+
|
637
|
+
```ruby
|
638
|
+
loader.load_file("#{__dir__}/custom_web_app/routes.rb")
|
639
|
+
```
|
640
|
+
|
641
|
+
This is useful when the loader is not eager loading the entire project, but you still need an individual file to be loaded for things to function properly.
|
642
|
+
|
643
|
+
Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](#shadowed-files), or is not managed by the receiver.
|
644
|
+
|
645
|
+
`Zeitwerk::Loader#load_file` is idempotent, but compatible with reloading. If you load a file and then reload, a new call will load its (current) contents again.
|
646
|
+
|
647
|
+
If you want to eager load a directory, `Zeitwerk::Loader#eager_load_dir` is more efficient than invoking `Zeitwerk::Loader#load_file` on its files.
|
648
|
+
|
481
649
|
<a id="markdown-reloading" name="reloading"></a>
|
482
650
|
### Reloading
|
483
651
|
|
652
|
+
<a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
|
653
|
+
#### Configuration and usage
|
654
|
+
|
484
655
|
Zeitwerk is able to reload code, but you need to enable this feature:
|
485
656
|
|
486
657
|
```ruby
|
@@ -494,7 +665,7 @@ loader.reload
|
|
494
665
|
|
495
666
|
There is no way to undo this, either you want to reload or you don't.
|
496
667
|
|
497
|
-
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
|
668
|
+
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`. Attempting to reload without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
498
669
|
|
499
670
|
Generally speaking, reloading is useful while developing running services like web applications. Gems that implement regular libraries, so to speak, or services running in testing or production environments, won't normally have a use case for reloading. If reloading is not enabled, Zeitwerk is able to use less memory.
|
500
671
|
|
@@ -502,12 +673,34 @@ Reloading removes the currently loaded classes and modules and resets the loader
|
|
502
673
|
|
503
674
|
It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
|
504
675
|
|
505
|
-
|
676
|
+
<a id="markdown-thread-safety" name="thread-safety"></a>
|
677
|
+
#### Thread-safety
|
678
|
+
|
679
|
+
In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
|
680
|
+
|
681
|
+
For example, a web framework that serves each request in its own thread and has reloading enabled could create a read-write lock on boot like this:
|
506
682
|
|
507
|
-
|
508
|
-
|
683
|
+
```ruby
|
684
|
+
require "concurrent/atomic/read_write_lock"
|
685
|
+
|
686
|
+
MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
|
687
|
+
```
|
509
688
|
|
510
|
-
|
689
|
+
You acquire the lock for reading for serving each individual request:
|
690
|
+
|
691
|
+
```ruby
|
692
|
+
MyFramework::RELOAD_RW_LOCK.with_read_lock do
|
693
|
+
serve(request)
|
694
|
+
end
|
695
|
+
```
|
696
|
+
|
697
|
+
Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
|
698
|
+
|
699
|
+
```ruby
|
700
|
+
MyFramework::RELOAD_RW_LOCK.with_write_lock do
|
701
|
+
loader.reload
|
702
|
+
end
|
703
|
+
```
|
511
704
|
|
512
705
|
On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores reloadable controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes.
|
513
706
|
|
@@ -545,9 +738,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
545
738
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
546
739
|
```
|
547
740
|
|
741
|
+
Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
|
742
|
+
|
743
|
+
```ruby
|
744
|
+
loader.inflector.inflect("xml" => "XML")
|
745
|
+
```
|
746
|
+
|
747
|
+
then the following constants are expected:
|
748
|
+
|
749
|
+
```
|
750
|
+
xml.rb -> XML
|
751
|
+
foo/xml -> Foo::XML
|
752
|
+
foo/bar/xml.rb -> Foo::Bar::XML
|
753
|
+
```
|
754
|
+
|
755
|
+
As you see, any directory whose basename is exactly `xml`, and any file whose basename is exactly `xml.rb` are expected to define the constant `XML` in the corresponding namespace. On the other hand, partial matches are ignored. For example, `xml_parser.rb` would be inflected as `XmlParser` because `xml_parser` is not equal to `xml`. You'd need an additional override:
|
756
|
+
|
757
|
+
```ruby
|
758
|
+
loader.inflector.inflect(
|
759
|
+
"xml" => "XML",
|
760
|
+
"xml_parser" => "XMLParser"
|
761
|
+
)
|
762
|
+
```
|
763
|
+
|
764
|
+
If you need more flexibility, you can define a custom inflector, as explained down below.
|
765
|
+
|
548
766
|
Overrides need to be configured before calling `setup`.
|
549
767
|
|
550
|
-
|
768
|
+
The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
|
551
769
|
|
552
770
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
553
771
|
#### Zeitwerk::GemInflector
|
@@ -558,6 +776,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
|
|
558
776
|
|
559
777
|
The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
|
560
778
|
|
779
|
+
<a id="markdown-zeitwerknullinflector" name="zeitwerknullinflector"></a>
|
780
|
+
#### Zeitwerk::NullInflector
|
781
|
+
|
782
|
+
This is an experimental inflector that simply returns its input unchanged.
|
783
|
+
|
784
|
+
```ruby
|
785
|
+
loader.inflector = Zeitwerk::NullInflector.new
|
786
|
+
```
|
787
|
+
|
788
|
+
In a project using this inflector, the names of files and directories are equal to the constants they define:
|
789
|
+
|
790
|
+
```
|
791
|
+
User.rb -> User
|
792
|
+
HTMLParser.rb -> HTMLParser
|
793
|
+
Admin/Role.rb -> Admin::Role
|
794
|
+
```
|
795
|
+
|
796
|
+
Point is, you think less. Names that typically need custom configuration like acronyms no longer require your attention. What you see is what you get, simple.
|
797
|
+
|
798
|
+
This inflector is experimental since Ruby usually goes for snake case in files and directories. But hey, if you fancy giving it a whirl, go for it!
|
799
|
+
|
800
|
+
The null inflector cannot be used in Rails applications because the `main` autoloader also manages engines. However, you could subclass the default inflector and override `camelize` to return the basename untouched if it starts with an uppercase letter. Generators would not create the expected file names, but you could still experiment to see how far this approach takes you.
|
801
|
+
|
802
|
+
In case-insensitive file systems, this inflector works as long as directory listings return the expected strings. Zeitwerk lists directories using Ruby APIs like `Dir.children` or `Dir.entries`.
|
803
|
+
|
561
804
|
<a id="markdown-custom-inflector" name="custom-inflector"></a>
|
562
805
|
#### Custom inflector
|
563
806
|
|
@@ -790,7 +1033,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
|
|
790
1033
|
|
791
1034
|
If there is a logger configured, you'll see traces when autoloads are set, files loaded, and modules autovivified. While reloading, removed autoloads and unloaded objects are also traced.
|
792
1035
|
|
793
|
-
As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_.
|
1036
|
+
As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_. This allows descending into subdirectories on demand, thus avoiding unnecessary tree walks.
|
794
1037
|
|
795
1038
|
<a id="markdown-loader-tag" name="loader-tag"></a>
|
796
1039
|
#### Loader tag
|
@@ -816,12 +1059,14 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
|
|
816
1059
|
<a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
|
817
1060
|
### Ignoring parts of the project
|
818
1061
|
|
819
|
-
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
|
1062
|
+
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have the extension ".rb".
|
820
1063
|
|
821
1064
|
However, sometimes it might still be convenient to tell Zeitwerk to completely ignore some particular Ruby file or directory. That is possible with `ignore`, which accepts an arbitrary number of strings or `Pathname` objects, and also an array of them.
|
822
1065
|
|
823
1066
|
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
824
1067
|
|
1068
|
+
There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explictly if you want it ignored too.
|
1069
|
+
|
825
1070
|
Let's see some use cases.
|
826
1071
|
|
827
1072
|
<a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
|
@@ -904,10 +1149,39 @@ loader.ignore(tests)
|
|
904
1149
|
loader.setup
|
905
1150
|
```
|
906
1151
|
|
1152
|
+
<a id="markdown-shadowed-files" name="shadowed-files"></a>
|
1153
|
+
### Shadowed files
|
1154
|
+
|
1155
|
+
In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
|
1156
|
+
|
1157
|
+
```ruby
|
1158
|
+
require "foo"
|
1159
|
+
```
|
1160
|
+
|
1161
|
+
the first one found gets loaded, and the rest are ignored.
|
1162
|
+
|
1163
|
+
Zeitwerk behaves in a similar way. If `foo.rb` is present in several root directories (at the same namespace level), the constant `Foo` is autoloaded from the first one, and the rest of the files are not evaluated. If logging is enabled, you'll see something like
|
1164
|
+
|
1165
|
+
```
|
1166
|
+
file #{file} is ignored because #{previous_occurrence} has precedence
|
1167
|
+
```
|
1168
|
+
|
1169
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1170
|
+
|
1171
|
+
Even if there's only one `foo.rb`, if the constant `Foo` is already defined when Zeitwerk finds `foo.rb`, then the file is ignored too. This could happen if `Foo` was defined by a dependency, for example. If logging is enabled, you'll see something like
|
1172
|
+
|
1173
|
+
```
|
1174
|
+
file #{file} is ignored because #{constant_path} is already defined
|
1175
|
+
```
|
1176
|
+
|
1177
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1178
|
+
|
1179
|
+
Shadowing only applies to Ruby files, namespace definition can be spread over multiple directories. And you can also reopen third-party namespaces if done [orderly](#reopening-third-party-namespaces).
|
1180
|
+
|
907
1181
|
<a id="markdown-edge-cases" name="edge-cases"></a>
|
908
1182
|
### Edge cases
|
909
1183
|
|
910
|
-
|
1184
|
+
[Explicit namespaces](#explicit-namespaces) like `Trip` here:
|
911
1185
|
|
912
1186
|
```ruby
|
913
1187
|
# trip.rb
|
@@ -921,14 +1195,15 @@ module Trip::Geolocation
|
|
921
1195
|
end
|
922
1196
|
```
|
923
1197
|
|
924
|
-
|
1198
|
+
have to be defined with the `class`/`module` keywords, as in the example above.
|
925
1199
|
|
926
1200
|
For technical reasons, raw constant assignment is not supported:
|
927
1201
|
|
928
1202
|
```ruby
|
929
1203
|
# trip.rb
|
930
|
-
Trip = Class
|
931
|
-
Trip = Struct.new { ... }
|
1204
|
+
Trip = Class { ...} # NOT SUPPORTED
|
1205
|
+
Trip = Struct.new { ... } # NOT SUPPORTED
|
1206
|
+
Trip = Data.define { ... } # NOT SUPPORTED
|
932
1207
|
```
|
933
1208
|
|
934
1209
|
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
@@ -983,12 +1258,127 @@ require "active_job"
|
|
983
1258
|
require "active_job/queue_adapters"
|
984
1259
|
|
985
1260
|
require "zeitwerk"
|
986
|
-
|
1261
|
+
# By passing the flag, we acknowledge the extra directory lib/active_job
|
1262
|
+
# has to be managed by the loader and no warning has to be issued for it.
|
1263
|
+
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
987
1264
|
loader.setup
|
988
1265
|
```
|
989
1266
|
|
990
1267
|
With that, when Zeitwerk scans the file system and reaches the gem directories `lib/active_job` and `lib/active_job/queue_adapters`, it detects the corresponding modules already exist and therefore understands it does not have to manage them. The loader just descends into those directories. Eventually will reach `lib/active_job/queue_adapters/awesome_queue.rb`, and since `ActiveJob::QueueAdapters::AwesomeQueue` is unknown, Zeitwerk will manage it. Which is what happens regularly with the files in your gem. On reload, the namespaces are safe, won't be reloaded. The loader only reloads what it manages, which in this case is the adapter itself.
|
991
1268
|
|
1269
|
+
<a id="markdown-introspection" name="introspection"></a>
|
1270
|
+
### Introspection
|
1271
|
+
|
1272
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1273
|
+
#### `Zeitwerk::Loader#dirs`
|
1274
|
+
|
1275
|
+
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1276
|
+
|
1277
|
+
```ruby
|
1278
|
+
loader = Zeitwerk::Loader.new
|
1279
|
+
loader.push_dir(Pathname.new("/foo"))
|
1280
|
+
loader.dirs # => ["/foo"]
|
1281
|
+
```
|
1282
|
+
|
1283
|
+
This method accepts an optional `namespaces` keyword argument. If truthy, the method returns a hash table instead. Keys are the absolute paths of the root directories as strings. Values are their corresponding namespaces, class or module objects:
|
1284
|
+
|
1285
|
+
```ruby
|
1286
|
+
loader = Zeitwerk::Loader.new
|
1287
|
+
loader.push_dir(Pathname.new("/foo"))
|
1288
|
+
loader.push_dir(Pathname.new("/bar"), namespace: Bar)
|
1289
|
+
loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
|
1290
|
+
```
|
1291
|
+
|
1292
|
+
By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
|
1293
|
+
|
1294
|
+
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1295
|
+
|
1296
|
+
<a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
|
1297
|
+
#### `Zeitwerk::Loader#cpath_expected_at`
|
1298
|
+
|
1299
|
+
Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
|
1300
|
+
|
1301
|
+
Some examples, assuming that `app/models` is a root directory:
|
1302
|
+
|
1303
|
+
```ruby
|
1304
|
+
loader.cpath_expected_at("app/models") # => "Object"
|
1305
|
+
loader.cpath_expected_at("app/models/user.rb") # => "User"
|
1306
|
+
loader.cpath_expected_at("app/models/hotel") # => "Hotel"
|
1307
|
+
loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
|
1308
|
+
```
|
1309
|
+
|
1310
|
+
If `collapsed` is a collapsed directory:
|
1311
|
+
|
1312
|
+
```ruby
|
1313
|
+
loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
|
1314
|
+
loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
|
1315
|
+
loader.cpath_expected_at("a/b") # => "A::B"
|
1316
|
+
```
|
1317
|
+
|
1318
|
+
If the argument corresponds to an [ignored file or directory](#ignoring-parts-of-the-project), the method returns `nil`. Same if the argument is not managed by the loader.
|
1319
|
+
|
1320
|
+
`Zeitwerk::Error` is raised if the given path does not exist:
|
1321
|
+
|
1322
|
+
```ruby
|
1323
|
+
loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
|
1324
|
+
```
|
1325
|
+
|
1326
|
+
`Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
|
1327
|
+
|
1328
|
+
```ruby
|
1329
|
+
loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
|
1330
|
+
```
|
1331
|
+
|
1332
|
+
This method does not parse file contents and does not guarantee files define the returned constant path. It just says which is the _expected_ one.
|
1333
|
+
|
1334
|
+
`Zeitwerk::Loader#cpath_expected_at` is designed to be used with individual paths. If you want to know all the expected constant paths in the project, please use `Zeitwerk::Loader#all_expected_cpaths`, documented next.
|
1335
|
+
|
1336
|
+
<a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
|
1337
|
+
#### `Zeitwerk::Loader#all_expected_cpaths`
|
1338
|
+
|
1339
|
+
The method `Zeitwerk::Loader#all_expected_cpaths` returns a hash that maps the absolute paths of the files and directories managed by the receiver to their expected constant paths.
|
1340
|
+
|
1341
|
+
Ignored files, hidden files, and files whose extension is not ".rb" are not included in the result. Same for directories, hidden or ignored directories are not included in the result. Additionally, directories that contain no files with extension ".rb" (recursively) are also excluded, since those are not considered to represent Ruby namespaces.
|
1342
|
+
|
1343
|
+
For example, if `lib` is the root directory of a gem with the following contents:
|
1344
|
+
|
1345
|
+
```
|
1346
|
+
lib/.DS_Store
|
1347
|
+
lib/my_gem.rb
|
1348
|
+
lib/my_gem/version.rb
|
1349
|
+
lib/my_gem/ignored.rb
|
1350
|
+
lib/my_gem/drivers/unix.rb
|
1351
|
+
lib/my_gem/drivers/windows.rb
|
1352
|
+
lib/my_gem/collapsed/foo.rb
|
1353
|
+
lib/tasks/my_gem.rake
|
1354
|
+
```
|
1355
|
+
|
1356
|
+
`Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
|
1357
|
+
|
1358
|
+
```ruby
|
1359
|
+
{
|
1360
|
+
"/.../lib" => "Object",
|
1361
|
+
"/.../lib/my_gem.rb" => "MyGem",
|
1362
|
+
"/.../lib/my_gem" => "MyGem",
|
1363
|
+
"/.../lib/my_gem/version.rb" => "MyGem::VERSION",
|
1364
|
+
"/.../lib/my_gem/drivers" => "MyGem::Drivers",
|
1365
|
+
"/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
|
1366
|
+
"/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
|
1367
|
+
"/.../lib/my_gem/collapsed" => "MyGem"
|
1368
|
+
"/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
|
1369
|
+
}
|
1370
|
+
```
|
1371
|
+
|
1372
|
+
In the previous example we assume `lib/my_gem/ignored.rb` is ignored, and therefore it is not present in the returned hash. Also, `lib/my_gem/collapsed` is a collapsed directory, so the expected namespace at that level is still `MyGem` (this is an edge case).
|
1373
|
+
|
1374
|
+
The file `lib/.DS_Store` is hidden, hence excluded. The directory `lib/tasks` is also not present because it contains no files with extension ".rb".
|
1375
|
+
|
1376
|
+
Directory paths do not have trailing slashes.
|
1377
|
+
|
1378
|
+
The order of the hash entries is undefined.
|
1379
|
+
|
1380
|
+
This method does not parse or execute file contents and does not guarantee files define the corresponding constant paths. It just says which are the _expected_ ones.
|
1381
|
+
|
992
1382
|
<a id="markdown-encodings" name="encodings"></a>
|
993
1383
|
### Encodings
|
994
1384
|
|
@@ -1010,7 +1400,7 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1010
1400
|
|
1011
1401
|
3. In that line, if two loaders manage files that translate to the same constant in the same namespace, the first one wins, the rest are ignored. Similar to what happens with `require` and `$LOAD_PATH`, only the first occurrence matters.
|
1012
1402
|
|
1013
|
-
4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than
|
1403
|
+
4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than defines, the namespace. This is often accomplished by loading (e.g., `require`-ing) the dependency.
|
1014
1404
|
|
1015
1405
|
5. Objects stored in reloadable constants should not be cached in places that are not reloaded. For example, non-reloadable classes should not subclass a reloadable class, or mixin a reloadable module. Otherwise, after reloading, those classes or module objects would become stale. Referring to constants in dynamic places like method calls or lambdas is fine.
|
1016
1406
|
|
@@ -1019,22 +1409,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1019
1409
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1020
1410
|
### Debuggers
|
1021
1411
|
|
1022
|
-
|
1023
|
-
#### debug.rb
|
1024
|
-
|
1025
|
-
The new [debug.rb](https://github.com/ruby/debug) gem and Zeitwerk are mostly compatible. This is the new debugger that is going to ship with Ruby 3.1.
|
1026
|
-
|
1027
|
-
There's one exception, though: Due to a technical limitation of tracepoints, explicit namespaces are not autoloaded while expressions are evaluated in the REPL. See [ruby/debug#408](https://github.com/ruby/debug/issues/408).
|
1028
|
-
|
1029
|
-
<a id="markdown-byebug" name="byebug"></a>
|
1030
|
-
#### Byebug
|
1031
|
-
|
1032
|
-
Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) have a similar edge incompatibility.
|
1412
|
+
Zeitwerk and [debug.rb](https://github.com/ruby/debug) are fully compatible if CRuby is ≥ 3.1 (see [ruby/debug#558](https://github.com/ruby/debug/pull/558)).
|
1033
1413
|
|
1034
|
-
|
1035
|
-
#### Break
|
1414
|
+
[Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564). Prior to CRuby 3.1, `debug.rb` has a similar edge incompatibility.
|
1036
1415
|
|
1037
|
-
|
1416
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1038
1417
|
|
1039
1418
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1040
1419
|
## Pronunciation
|