zeitwerk 2.6.0 → 2.6.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +351 -47
- 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 -8
- data/lib/zeitwerk/internal.rb +12 -0
- data/lib/zeitwerk/kernel.rb +6 -3
- 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 +74 -16
- data/lib/zeitwerk/loader.rb +196 -135
- data/lib/zeitwerk/registry.rb +2 -2
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a9048a5ba05f448e5406080995aab9709a3dd7c8dd32aad3aa0ba4f544b69c4
|
4
|
+
data.tar.gz: 581cf27f70c82b754a1baadf7a41f6c87c069ae2e78bd26b4ec6e02cfa2795b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45327b148bab210d941ffeae022b70e5c2f2be4372f2192a08859faeed81d2cc5702b05d6e4efcf676a8fb7b451c46200ac372051c68ba84cfc31ed9339f3ede
|
7
|
+
data.tar.gz: 8e3266d7641f58b6a8f74abfa913c14ae4a79d4efd177578de4e7e144a21adc33d33279b7d2771cdc57937cfedfb1f5e193bb9e57c18ea90956b342b516e9bd9
|
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[![Gem Version](https://img.shields.io/gem/v/zeitwerk.svg?style=for-the-badge)](https://rubygems.org/gems/zeitwerk)
|
6
|
-
[![Build Status](https://img.shields.io/github/workflow/status/fxn/zeitwerk/
|
6
|
+
[![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3Amain)
|
7
|
+
|
7
8
|
|
8
9
|
<!-- TOC -->
|
9
10
|
|
@@ -24,11 +25,18 @@
|
|
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)
|
@@ -44,15 +52,16 @@
|
|
44
52
|
- [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
|
45
53
|
- [Use case: The adapter pattern](#use-case-the-adapter-pattern)
|
46
54
|
- [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
|
55
|
+
- [Shadowed files](#shadowed-files)
|
47
56
|
- [Edge cases](#edge-cases)
|
48
57
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
49
58
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
59
|
+
- [Introspection](#introspection)
|
60
|
+
- [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
|
61
|
+
- [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
|
50
62
|
- [Encodings](#encodings)
|
51
63
|
- [Rules of thumb](#rules-of-thumb)
|
52
64
|
- [Debuggers](#debuggers)
|
53
|
-
- [debug.rb](#debugrb)
|
54
|
-
- [Byebug](#byebug)
|
55
|
-
- [Break](#break)
|
56
65
|
- [Pronunciation](#pronunciation)
|
57
66
|
- [Supported Ruby versions](#supported-ruby-versions)
|
58
67
|
- [Testing](#testing)
|
@@ -70,15 +79,15 @@
|
|
70
79
|
|
71
80
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
72
81
|
|
73
|
-
Given a [conventional file structure](#file-structure), Zeitwerk is
|
82
|
+
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
83
|
|
75
|
-
Zeitwerk
|
84
|
+
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
85
|
|
77
|
-
The gem is designed
|
86
|
+
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
87
|
|
79
|
-
Internally, Zeitwerk
|
88
|
+
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
89
|
|
81
|
-
Furthermore, Zeitwerk
|
90
|
+
Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
|
82
91
|
|
83
92
|
<a id="markdown-synopsis" name="synopsis"></a>
|
84
93
|
## Synopsis
|
@@ -138,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
|
|
138
147
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
139
148
|
### The idea: File paths match constant paths
|
140
149
|
|
141
|
-
|
150
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
142
151
|
|
143
152
|
```
|
144
153
|
lib/my_gem.rb -> MyGem
|
@@ -147,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
147
156
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
148
157
|
```
|
149
158
|
|
150
|
-
You can tune
|
159
|
+
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
160
|
|
152
161
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
153
162
|
### Inner simple constants
|
@@ -168,7 +177,7 @@ class HttpCrawler
|
|
168
177
|
end
|
169
178
|
```
|
170
179
|
|
171
|
-
The first example needs a custom [inflection](
|
180
|
+
The first example needs a custom [inflection](#inflection) rule:
|
172
181
|
|
173
182
|
```ruby
|
174
183
|
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
@@ -205,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
205
214
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
206
215
|
#### Custom root namespaces
|
207
216
|
|
208
|
-
|
217
|
+
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
218
|
|
210
219
|
For example, given:
|
211
220
|
|
@@ -221,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
221
230
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
222
231
|
```
|
223
232
|
|
224
|
-
Please
|
233
|
+
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
234
|
|
226
235
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
227
236
|
#### Nested root directories
|
228
237
|
|
229
|
-
Root directories
|
238
|
+
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
239
|
|
231
|
-
Zeitwerk
|
240
|
+
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
241
|
|
233
242
|
```
|
234
243
|
app/models/concerns/geolocatable.rb
|
@@ -239,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
239
248
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
240
249
|
### Implicit namespaces
|
241
250
|
|
242
|
-
If a namespace
|
251
|
+
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
252
|
|
244
|
-
For
|
253
|
+
For instance, suppose a project includes an `admin` directory:
|
245
254
|
|
246
255
|
```
|
247
256
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
@@ -249,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
249
258
|
|
250
259
|
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
260
|
|
252
|
-
|
261
|
+
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
262
|
|
254
263
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
255
264
|
### Explicit namespaces
|
@@ -383,6 +392,12 @@ module MyGem
|
|
383
392
|
end
|
384
393
|
```
|
385
394
|
|
395
|
+
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.
|
396
|
+
|
397
|
+
`Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
398
|
+
|
399
|
+
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.
|
400
|
+
|
386
401
|
Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
|
387
402
|
|
388
403
|
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 +414,64 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
399
414
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
400
415
|
```
|
401
416
|
|
402
|
-
|
417
|
+
<a id="markdown-for_gem_extension" name="for_gem_extension"></a>
|
418
|
+
#### for_gem_extension
|
419
|
+
|
420
|
+
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/):
|
421
|
+
|
422
|
+
* The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
|
423
|
+
* The namespace should be `Net::HTTP::NicheFeature`.
|
424
|
+
* The entry point should be `lib/net/http/niche_feature.rb`.
|
425
|
+
* 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.
|
426
|
+
|
427
|
+
The top-level file mentioned in the last point is optional. In particular, from
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
gem "net-http-niche_feature"
|
431
|
+
```
|
432
|
+
|
433
|
+
if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
|
403
434
|
|
404
|
-
|
435
|
+
Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
|
436
|
+
|
437
|
+
The structure of the gem would be like this:
|
438
|
+
|
439
|
+
```ruby
|
440
|
+
# lib/net-http-niche_feature.rb (optional)
|
441
|
+
|
442
|
+
# For technical reasons, this cannot be require_relative.
|
443
|
+
require "net/http/niche_feature"
|
444
|
+
|
445
|
+
|
446
|
+
# lib/net/http/niche_feature.rb
|
447
|
+
|
448
|
+
require "net/http"
|
449
|
+
require "zeitwerk"
|
450
|
+
|
451
|
+
loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
|
452
|
+
loader.setup
|
453
|
+
|
454
|
+
module Net::HTTP::NicheFeature
|
455
|
+
# Since the setup has been performed, at this point we are already able
|
456
|
+
# to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
|
457
|
+
include MyMixin
|
458
|
+
end
|
459
|
+
|
460
|
+
|
461
|
+
# lib/net/http/niche_feature/version.rb
|
462
|
+
|
463
|
+
module Net::HTTP::NicheFeature
|
464
|
+
VERSION = "1.0.0"
|
465
|
+
end
|
466
|
+
```
|
467
|
+
|
468
|
+
`Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
|
469
|
+
|
470
|
+
If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
|
471
|
+
|
472
|
+
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.
|
473
|
+
|
474
|
+
`Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
405
475
|
|
406
476
|
<a id="markdown-autoloading" name="autoloading"></a>
|
407
477
|
### Autoloading
|
@@ -437,10 +507,12 @@ loader.eager_load
|
|
437
507
|
|
438
508
|
That skips [ignored files and directories](#ignoring-parts-of-the-project).
|
439
509
|
|
440
|
-
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](
|
510
|
+
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
|
441
511
|
|
442
512
|
Eager loading is synchronized and idempotent.
|
443
513
|
|
514
|
+
Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
515
|
+
|
444
516
|
<a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
|
445
517
|
#### Eager load exclusions
|
446
518
|
|
@@ -463,6 +535,81 @@ Which may be handy if the project eager loads in the test suite to [ensure proje
|
|
463
535
|
|
464
536
|
The `force` flag does not affect ignored files and directories, those are still ignored.
|
465
537
|
|
538
|
+
<a id="markdown-eager-load-directories" name="eager-load-directories"></a>
|
539
|
+
#### Eager load directories
|
540
|
+
|
541
|
+
The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
|
542
|
+
|
543
|
+
```ruby
|
544
|
+
loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
|
545
|
+
```
|
546
|
+
|
547
|
+
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.
|
548
|
+
|
549
|
+
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`.
|
550
|
+
|
551
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
552
|
+
|
553
|
+
`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.
|
554
|
+
|
555
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
556
|
+
|
557
|
+
Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
|
558
|
+
|
559
|
+
Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
560
|
+
|
561
|
+
<a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
|
562
|
+
#### Eager load namespaces
|
563
|
+
|
564
|
+
The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
|
565
|
+
|
566
|
+
```ruby
|
567
|
+
loader.eager_load_namespace(MyApp::Routes)
|
568
|
+
```
|
569
|
+
|
570
|
+
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.
|
571
|
+
|
572
|
+
The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
|
573
|
+
|
574
|
+
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
|
575
|
+
|
576
|
+
```
|
577
|
+
root_dir1/my_app/routes
|
578
|
+
root_dir2/my_app/routes
|
579
|
+
root_dir3/my_app/routes
|
580
|
+
```
|
581
|
+
|
582
|
+
where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
583
|
+
|
584
|
+
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.
|
585
|
+
|
586
|
+
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.
|
587
|
+
|
588
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
589
|
+
|
590
|
+
`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.
|
591
|
+
|
592
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
593
|
+
|
594
|
+
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.
|
595
|
+
|
596
|
+
Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
597
|
+
|
598
|
+
<a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
|
599
|
+
#### Eager load namespaces shared by several loaders
|
600
|
+
|
601
|
+
The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
|
602
|
+
|
603
|
+
```ruby
|
604
|
+
Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
|
605
|
+
```
|
606
|
+
|
607
|
+
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.
|
608
|
+
|
609
|
+
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.
|
610
|
+
|
611
|
+
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.
|
612
|
+
|
466
613
|
<a id="markdown-global-eager-load" name="global-eager-load"></a>
|
467
614
|
#### Global eager load
|
468
615
|
|
@@ -478,9 +625,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
|
|
478
625
|
|
479
626
|
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
627
|
|
628
|
+
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.
|
629
|
+
|
630
|
+
<a id="markdown-loading-individual-files" name="loading-individual-files"></a>
|
631
|
+
### Loading individual files
|
632
|
+
|
633
|
+
The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
|
634
|
+
|
635
|
+
```ruby
|
636
|
+
loader.load_file("#{__dir__}/custom_web_app/routes.rb")
|
637
|
+
```
|
638
|
+
|
639
|
+
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.
|
640
|
+
|
641
|
+
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.
|
642
|
+
|
643
|
+
`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.
|
644
|
+
|
645
|
+
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.
|
646
|
+
|
481
647
|
<a id="markdown-reloading" name="reloading"></a>
|
482
648
|
### Reloading
|
483
649
|
|
650
|
+
<a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
|
651
|
+
#### Configuration and usage
|
652
|
+
|
484
653
|
Zeitwerk is able to reload code, but you need to enable this feature:
|
485
654
|
|
486
655
|
```ruby
|
@@ -494,7 +663,7 @@ loader.reload
|
|
494
663
|
|
495
664
|
There is no way to undo this, either you want to reload or you don't.
|
496
665
|
|
497
|
-
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
|
666
|
+
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
667
|
|
499
668
|
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
669
|
|
@@ -502,12 +671,34 @@ Reloading removes the currently loaded classes and modules and resets the loader
|
|
502
671
|
|
503
672
|
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
673
|
|
505
|
-
|
674
|
+
<a id="markdown-thread-safety" name="thread-safety"></a>
|
675
|
+
#### Thread-safety
|
506
676
|
|
507
|
-
|
508
|
-
* You should not autoload while another thread is reloading.
|
677
|
+
In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
|
509
678
|
|
510
|
-
|
679
|
+
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:
|
680
|
+
|
681
|
+
```ruby
|
682
|
+
require "concurrent/atomic/read_write_lock"
|
683
|
+
|
684
|
+
MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
|
685
|
+
```
|
686
|
+
|
687
|
+
You acquire the lock for reading for serving each individual request:
|
688
|
+
|
689
|
+
```ruby
|
690
|
+
MyFramework::RELOAD_RW_LOCK.with_read_lock do
|
691
|
+
serve(request)
|
692
|
+
end
|
693
|
+
```
|
694
|
+
|
695
|
+
Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
|
696
|
+
|
697
|
+
```ruby
|
698
|
+
MyFramework::RELOAD_RW_LOCK.with_write_lock do
|
699
|
+
loader.reload
|
700
|
+
end
|
701
|
+
```
|
511
702
|
|
512
703
|
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
704
|
|
@@ -545,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
545
736
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
546
737
|
```
|
547
738
|
|
739
|
+
Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
|
740
|
+
|
741
|
+
```ruby
|
742
|
+
loader.inflector.inflect("xml" => "XML")
|
743
|
+
```
|
744
|
+
|
745
|
+
then the following constants are expected:
|
746
|
+
|
747
|
+
```
|
748
|
+
xml.rb -> XML
|
749
|
+
foo/xml -> Foo::XML
|
750
|
+
foo/bar/xml.rb -> Foo::Bar::XML
|
751
|
+
```
|
752
|
+
|
753
|
+
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:
|
754
|
+
|
755
|
+
```ruby
|
756
|
+
loader.inflector.inflect(
|
757
|
+
"xml" => "XML",
|
758
|
+
"xml_parser" => "XMLParser"
|
759
|
+
)
|
760
|
+
```
|
761
|
+
|
762
|
+
If you need more flexibility, you can define a custom inflector, as explained down below.
|
763
|
+
|
548
764
|
Overrides need to be configured before calling `setup`.
|
549
765
|
|
550
|
-
|
766
|
+
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
767
|
|
552
768
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
553
769
|
#### Zeitwerk::GemInflector
|
@@ -822,6 +1038,8 @@ However, sometimes it might still be convenient to tell Zeitwerk to completely i
|
|
822
1038
|
|
823
1039
|
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
824
1040
|
|
1041
|
+
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.
|
1042
|
+
|
825
1043
|
Let's see some use cases.
|
826
1044
|
|
827
1045
|
<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 +1122,39 @@ loader.ignore(tests)
|
|
904
1122
|
loader.setup
|
905
1123
|
```
|
906
1124
|
|
1125
|
+
<a id="markdown-shadowed-files" name="shadowed-files"></a>
|
1126
|
+
### Shadowed files
|
1127
|
+
|
1128
|
+
In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
|
1129
|
+
|
1130
|
+
```ruby
|
1131
|
+
require "foo"
|
1132
|
+
```
|
1133
|
+
|
1134
|
+
the first one found gets loaded, and the rest are ignored.
|
1135
|
+
|
1136
|
+
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
|
1137
|
+
|
1138
|
+
```
|
1139
|
+
file #{file} is ignored because #{previous_occurrence} has precedence
|
1140
|
+
```
|
1141
|
+
|
1142
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1143
|
+
|
1144
|
+
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
|
1145
|
+
|
1146
|
+
```
|
1147
|
+
file #{file} is ignored because #{constant_path} is already defined
|
1148
|
+
```
|
1149
|
+
|
1150
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1151
|
+
|
1152
|
+
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).
|
1153
|
+
|
907
1154
|
<a id="markdown-edge-cases" name="edge-cases"></a>
|
908
1155
|
### Edge cases
|
909
1156
|
|
910
|
-
|
1157
|
+
[Explicit namespaces](#explicit-namespaces) like `Trip` here:
|
911
1158
|
|
912
1159
|
```ruby
|
913
1160
|
# trip.rb
|
@@ -921,14 +1168,15 @@ module Trip::Geolocation
|
|
921
1168
|
end
|
922
1169
|
```
|
923
1170
|
|
924
|
-
|
1171
|
+
have to be defined with the `class`/`module` keywords, as in the example above.
|
925
1172
|
|
926
1173
|
For technical reasons, raw constant assignment is not supported:
|
927
1174
|
|
928
1175
|
```ruby
|
929
1176
|
# trip.rb
|
930
|
-
Trip = Class
|
931
|
-
Trip = Struct.new { ... }
|
1177
|
+
Trip = Class { ...} # NOT SUPPORTED
|
1178
|
+
Trip = Struct.new { ... } # NOT SUPPORTED
|
1179
|
+
Trip = Data.define { ... } # NOT SUPPORTED
|
932
1180
|
```
|
933
1181
|
|
934
1182
|
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
@@ -983,12 +1231,79 @@ require "active_job"
|
|
983
1231
|
require "active_job/queue_adapters"
|
984
1232
|
|
985
1233
|
require "zeitwerk"
|
986
|
-
|
1234
|
+
# By passing the flag, we acknowledge the extra directory lib/active_job
|
1235
|
+
# has to be managed by the loader and no warning has to be issued for it.
|
1236
|
+
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
987
1237
|
loader.setup
|
988
1238
|
```
|
989
1239
|
|
990
1240
|
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
1241
|
|
1242
|
+
<a id="markdown-introspection" name="introspection"></a>
|
1243
|
+
### Introspection
|
1244
|
+
|
1245
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1246
|
+
#### `Zeitwerk::Loader#dirs`
|
1247
|
+
|
1248
|
+
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1249
|
+
|
1250
|
+
```ruby
|
1251
|
+
loader = Zeitwerk::Loader.new
|
1252
|
+
loader.push_dir(Pathname.new("/foo"))
|
1253
|
+
loader.dirs # => ["/foo"]
|
1254
|
+
```
|
1255
|
+
|
1256
|
+
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:
|
1257
|
+
|
1258
|
+
```ruby
|
1259
|
+
loader = Zeitwerk::Loader.new
|
1260
|
+
loader.push_dir(Pathname.new("/foo"))
|
1261
|
+
loader.push_dir(Pathname.new("/bar"), namespace: Bar)
|
1262
|
+
loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
|
1263
|
+
```
|
1264
|
+
|
1265
|
+
By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
|
1266
|
+
|
1267
|
+
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1268
|
+
|
1269
|
+
<a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
|
1270
|
+
#### `Zeitwerk::Loader#cpath_expected_at`
|
1271
|
+
|
1272
|
+
Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
|
1273
|
+
|
1274
|
+
Some examples, assuming that `app/models` is a root directory:
|
1275
|
+
|
1276
|
+
```ruby
|
1277
|
+
loader.cpath_expected_at("app/models") # => "Object"
|
1278
|
+
loader.cpath_expected_at("app/models/user.rb") # => "User"
|
1279
|
+
loader.cpath_expected_at("app/models/hotel") # => "Hotel"
|
1280
|
+
loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
|
1281
|
+
```
|
1282
|
+
|
1283
|
+
If `collapsed` is a collapsed directory:
|
1284
|
+
|
1285
|
+
```ruby
|
1286
|
+
loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
|
1287
|
+
loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
|
1288
|
+
loader.cpath_expected_at("a/b") # => "A::B"
|
1289
|
+
```
|
1290
|
+
|
1291
|
+
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.
|
1292
|
+
|
1293
|
+
`Zeitwerk::Error` is raised if the given path does not exist:
|
1294
|
+
|
1295
|
+
```ruby
|
1296
|
+
loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
|
1297
|
+
```
|
1298
|
+
|
1299
|
+
`Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
|
1300
|
+
|
1301
|
+
```ruby
|
1302
|
+
loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
|
1303
|
+
```
|
1304
|
+
|
1305
|
+
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.
|
1306
|
+
|
992
1307
|
<a id="markdown-encodings" name="encodings"></a>
|
993
1308
|
### Encodings
|
994
1309
|
|
@@ -1019,22 +1334,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1019
1334
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1020
1335
|
### Debuggers
|
1021
1336
|
|
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.
|
1337
|
+
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
1338
|
|
1034
|
-
|
1035
|
-
#### Break
|
1339
|
+
[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
1340
|
|
1037
|
-
|
1341
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1038
1342
|
|
1039
1343
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1040
1344
|
## Pronunciation
|
data/lib/zeitwerk/error.rb
CHANGED