zeitwerk 2.6.2 → 2.6.10
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 +177 -45
- 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 +11 -7
- data/lib/zeitwerk/internal.rb +12 -0
- data/lib/zeitwerk/loader/callbacks.rb +6 -6
- data/lib/zeitwerk/loader/config.rb +68 -49
- data/lib/zeitwerk/loader/eager_load.rb +30 -18
- data/lib/zeitwerk/loader/helpers.rb +65 -17
- data/lib/zeitwerk/loader.rb +157 -73
- data/lib/zeitwerk/registry.rb +2 -2
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b2955613976d014a322168f1976a93b29e388b4c72ecf5acdbb0186d212fe7c
|
|
4
|
+
data.tar.gz: 57df7539d6fa37fcf41f1d37b6c2fa0a8db5af7af02333da35566fca92a03d8a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 625ef1f9a15205afa2efea139f63b5b800660d57631fda75deccd172f72e82fb6454efe7465edb8024a9a8536b463fa523ead1582dc13aac54dfddb5535e31a9
|
|
7
|
+
data.tar.gz: d0e0b1ff25b25ce5b9d3223fa01b3f04b4eefa7e316d55bad95686265f194aee24599e146c1ef9a05dd20dc16f670733e17e494ccd469ea9aef7e4a8b2fb973c
|
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,6 +25,7 @@
|
|
|
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)
|
|
@@ -55,12 +57,11 @@
|
|
|
55
57
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
|
56
58
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
|
57
59
|
- [Introspection](#introspection)
|
|
60
|
+
- [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
|
|
61
|
+
- [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
|
|
58
62
|
- [Encodings](#encodings)
|
|
59
63
|
- [Rules of thumb](#rules-of-thumb)
|
|
60
64
|
- [Debuggers](#debuggers)
|
|
61
|
-
- [debug.rb](#debugrb)
|
|
62
|
-
- [Byebug](#byebug)
|
|
63
|
-
- [Break](#break)
|
|
64
65
|
- [Pronunciation](#pronunciation)
|
|
65
66
|
- [Supported Ruby versions](#supported-ruby-versions)
|
|
66
67
|
- [Testing](#testing)
|
|
@@ -78,15 +79,15 @@
|
|
|
78
79
|
|
|
79
80
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
|
80
81
|
|
|
81
|
-
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.
|
|
82
83
|
|
|
83
|
-
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.
|
|
84
85
|
|
|
85
|
-
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.
|
|
86
87
|
|
|
87
|
-
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`.
|
|
88
89
|
|
|
89
|
-
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.
|
|
90
91
|
|
|
91
92
|
<a id="markdown-synopsis" name="synopsis"></a>
|
|
92
93
|
## Synopsis
|
|
@@ -146,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
|
|
|
146
147
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
|
147
148
|
### The idea: File paths match constant paths
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
|
150
151
|
|
|
151
152
|
```
|
|
152
153
|
lib/my_gem.rb -> MyGem
|
|
@@ -155,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
|
155
156
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
|
156
157
|
```
|
|
157
158
|
|
|
158
|
-
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.
|
|
159
160
|
|
|
160
161
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
|
161
162
|
### Inner simple constants
|
|
@@ -176,7 +177,7 @@ class HttpCrawler
|
|
|
176
177
|
end
|
|
177
178
|
```
|
|
178
179
|
|
|
179
|
-
The first example needs a custom [inflection](
|
|
180
|
+
The first example needs a custom [inflection](#inflection) rule:
|
|
180
181
|
|
|
181
182
|
```ruby
|
|
182
183
|
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
|
@@ -213,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
|
213
214
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
|
214
215
|
#### Custom root namespaces
|
|
215
216
|
|
|
216
|
-
|
|
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.
|
|
217
218
|
|
|
218
219
|
For example, given:
|
|
219
220
|
|
|
@@ -229,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
|
229
230
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
|
230
231
|
```
|
|
231
232
|
|
|
232
|
-
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.
|
|
233
234
|
|
|
234
235
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
|
235
236
|
#### Nested root directories
|
|
236
237
|
|
|
237
|
-
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.
|
|
238
239
|
|
|
239
|
-
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:
|
|
240
241
|
|
|
241
242
|
```
|
|
242
243
|
app/models/concerns/geolocatable.rb
|
|
@@ -247,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
|
247
248
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
|
248
249
|
### Implicit namespaces
|
|
249
250
|
|
|
250
|
-
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.
|
|
251
252
|
|
|
252
|
-
For
|
|
253
|
+
For instance, suppose a project includes an `admin` directory:
|
|
253
254
|
|
|
254
255
|
```
|
|
255
256
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
@@ -257,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
|
257
258
|
|
|
258
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.
|
|
259
260
|
|
|
260
|
-
|
|
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.
|
|
261
262
|
|
|
262
263
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
|
263
264
|
### Explicit namespaces
|
|
@@ -391,6 +392,12 @@ module MyGem
|
|
|
391
392
|
end
|
|
392
393
|
```
|
|
393
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
|
+
|
|
394
401
|
Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
|
|
395
402
|
|
|
396
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.
|
|
@@ -407,9 +414,64 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
|
407
414
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
|
408
415
|
```
|
|
409
416
|
|
|
410
|
-
|
|
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`.
|
|
411
434
|
|
|
412
|
-
|
|
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).
|
|
413
475
|
|
|
414
476
|
<a id="markdown-autoloading" name="autoloading"></a>
|
|
415
477
|
### Autoloading
|
|
@@ -445,10 +507,12 @@ loader.eager_load
|
|
|
445
507
|
|
|
446
508
|
That skips [ignored files and directories](#ignoring-parts-of-the-project).
|
|
447
509
|
|
|
448
|
-
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).
|
|
449
511
|
|
|
450
512
|
Eager loading is synchronized and idempotent.
|
|
451
513
|
|
|
514
|
+
Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
|
515
|
+
|
|
452
516
|
<a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
|
|
453
517
|
#### Eager load exclusions
|
|
454
518
|
|
|
@@ -484,7 +548,7 @@ This is useful when the loader is not eager loading the entire project, but you
|
|
|
484
548
|
|
|
485
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`.
|
|
486
550
|
|
|
487
|
-
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](
|
|
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.
|
|
488
552
|
|
|
489
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.
|
|
490
554
|
|
|
@@ -492,6 +556,8 @@ The method checks if a regular eager load was already executed, in which case it
|
|
|
492
556
|
|
|
493
557
|
Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
|
|
494
558
|
|
|
559
|
+
Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
|
560
|
+
|
|
495
561
|
<a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
|
|
496
562
|
#### Eager load namespaces
|
|
497
563
|
|
|
@@ -515,11 +581,11 @@ root_dir3/my_app/routes
|
|
|
515
581
|
|
|
516
582
|
where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
|
517
583
|
|
|
518
|
-
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.
|
|
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.
|
|
519
585
|
|
|
520
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.
|
|
521
587
|
|
|
522
|
-
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](
|
|
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.
|
|
523
589
|
|
|
524
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.
|
|
525
591
|
|
|
@@ -527,6 +593,8 @@ The method checks if a regular eager load was already executed, in which case it
|
|
|
527
593
|
|
|
528
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.
|
|
529
595
|
|
|
596
|
+
Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
|
597
|
+
|
|
530
598
|
<a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
|
|
531
599
|
#### Eager load namespaces shared by several loaders
|
|
532
600
|
|
|
@@ -536,10 +604,12 @@ The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namesp
|
|
|
536
604
|
Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
|
|
537
605
|
```
|
|
538
606
|
|
|
539
|
-
This may be handy, for example, if a framework supports plugins and a shared namespace needs to be eager loaded for the
|
|
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.
|
|
540
608
|
|
|
541
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.
|
|
542
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
|
+
|
|
543
613
|
<a id="markdown-global-eager-load" name="global-eager-load"></a>
|
|
544
614
|
#### Global eager load
|
|
545
615
|
|
|
@@ -555,6 +625,8 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
|
|
|
555
625
|
|
|
556
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.
|
|
557
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
|
+
|
|
558
630
|
<a id="markdown-loading-individual-files" name="loading-individual-files"></a>
|
|
559
631
|
### Loading individual files
|
|
560
632
|
|
|
@@ -566,7 +638,7 @@ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
|
|
|
566
638
|
|
|
567
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.
|
|
568
640
|
|
|
569
|
-
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](
|
|
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.
|
|
570
642
|
|
|
571
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.
|
|
572
644
|
|
|
@@ -591,7 +663,7 @@ loader.reload
|
|
|
591
663
|
|
|
592
664
|
There is no way to undo this, either you want to reload or you don't.
|
|
593
665
|
|
|
594
|
-
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`.
|
|
595
667
|
|
|
596
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.
|
|
597
669
|
|
|
@@ -664,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
|
664
736
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
|
665
737
|
```
|
|
666
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
|
+
|
|
667
764
|
Overrides need to be configured before calling `setup`.
|
|
668
765
|
|
|
669
|
-
|
|
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.
|
|
670
767
|
|
|
671
768
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
|
672
769
|
#### Zeitwerk::GemInflector
|
|
@@ -941,6 +1038,8 @@ However, sometimes it might still be convenient to tell Zeitwerk to completely i
|
|
|
941
1038
|
|
|
942
1039
|
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
|
943
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
|
+
|
|
944
1043
|
Let's see some use cases.
|
|
945
1044
|
|
|
946
1045
|
<a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
|
|
@@ -1075,8 +1174,9 @@ For technical reasons, raw constant assignment is not supported:
|
|
|
1075
1174
|
|
|
1076
1175
|
```ruby
|
|
1077
1176
|
# trip.rb
|
|
1078
|
-
Trip = Class
|
|
1079
|
-
Trip = Struct.new { ... }
|
|
1177
|
+
Trip = Class { ...} # NOT SUPPORTED
|
|
1178
|
+
Trip = Struct.new { ... } # NOT SUPPORTED
|
|
1179
|
+
Trip = Data.define { ... } # NOT SUPPORTED
|
|
1080
1180
|
```
|
|
1081
1181
|
|
|
1082
1182
|
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
|
@@ -1142,6 +1242,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
|
|
|
1142
1242
|
<a id="markdown-introspection" name="introspection"></a>
|
|
1143
1243
|
### Introspection
|
|
1144
1244
|
|
|
1245
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
|
1246
|
+
#### `Zeitwerk::Loader#dirs`
|
|
1247
|
+
|
|
1145
1248
|
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
|
1146
1249
|
|
|
1147
1250
|
```ruby
|
|
@@ -1159,8 +1262,48 @@ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
|
|
|
1159
1262
|
loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
|
|
1160
1263
|
```
|
|
1161
1264
|
|
|
1265
|
+
By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
|
|
1266
|
+
|
|
1162
1267
|
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
|
1163
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
|
+
|
|
1164
1307
|
<a id="markdown-encodings" name="encodings"></a>
|
|
1165
1308
|
### Encodings
|
|
1166
1309
|
|
|
@@ -1191,22 +1334,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
|
1191
1334
|
<a id="markdown-debuggers" name="debuggers"></a>
|
|
1192
1335
|
### Debuggers
|
|
1193
1336
|
|
|
1194
|
-
|
|
1195
|
-
#### debug.rb
|
|
1196
|
-
|
|
1197
|
-
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.
|
|
1198
|
-
|
|
1199
|
-
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).
|
|
1200
|
-
|
|
1201
|
-
<a id="markdown-byebug" name="byebug"></a>
|
|
1202
|
-
#### Byebug
|
|
1203
|
-
|
|
1204
|
-
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)).
|
|
1205
1338
|
|
|
1206
|
-
|
|
1207
|
-
#### 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.
|
|
1208
1340
|
|
|
1209
|
-
|
|
1341
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
|
1210
1342
|
|
|
1211
1343
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
|
1212
1344
|
## Pronunciation
|
data/lib/zeitwerk/error.rb
CHANGED
|
@@ -11,28 +11,28 @@ module Zeitwerk
|
|
|
11
11
|
module ExplicitNamespace # :nodoc: all
|
|
12
12
|
class << self
|
|
13
13
|
include RealModName
|
|
14
|
+
extend Internal
|
|
14
15
|
|
|
15
16
|
# Maps constant paths that correspond to explicit namespaces according to
|
|
16
17
|
# the file system, to the loader responsible for them.
|
|
17
18
|
#
|
|
18
|
-
# @private
|
|
19
19
|
# @sig Hash[String, Zeitwerk::Loader]
|
|
20
20
|
attr_reader :cpaths
|
|
21
|
+
private :cpaths
|
|
21
22
|
|
|
22
|
-
# @private
|
|
23
23
|
# @sig Mutex
|
|
24
24
|
attr_reader :mutex
|
|
25
|
+
private :mutex
|
|
25
26
|
|
|
26
|
-
# @private
|
|
27
27
|
# @sig TracePoint
|
|
28
28
|
attr_reader :tracer
|
|
29
|
+
private :tracer
|
|
29
30
|
|
|
30
31
|
# Asserts `cpath` corresponds to an explicit namespace for which `loader`
|
|
31
32
|
# is responsible.
|
|
32
33
|
#
|
|
33
|
-
# @private
|
|
34
34
|
# @sig (String, Zeitwerk::Loader) -> void
|
|
35
|
-
def register(cpath, loader)
|
|
35
|
+
internal def register(cpath, loader)
|
|
36
36
|
mutex.synchronize do
|
|
37
37
|
cpaths[cpath] = loader
|
|
38
38
|
# We check enabled? because, looking at the C source code, enabling an
|
|
@@ -41,24 +41,28 @@ module Zeitwerk
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
# @private
|
|
45
44
|
# @sig (Zeitwerk::Loader) -> void
|
|
46
|
-
def unregister_loader(loader)
|
|
45
|
+
internal def unregister_loader(loader)
|
|
47
46
|
cpaths.delete_if { |_cpath, l| l == loader }
|
|
48
47
|
disable_tracer_if_unneeded
|
|
49
48
|
end
|
|
50
49
|
|
|
51
|
-
|
|
50
|
+
# This is an internal method only used by the test suite.
|
|
51
|
+
#
|
|
52
|
+
# @sig (String) -> bool
|
|
53
|
+
internal def registered?(cpath)
|
|
54
|
+
cpaths.key?(cpath)
|
|
55
|
+
end
|
|
52
56
|
|
|
53
57
|
# @sig () -> void
|
|
54
|
-
def disable_tracer_if_unneeded
|
|
58
|
+
private def disable_tracer_if_unneeded
|
|
55
59
|
mutex.synchronize do
|
|
56
60
|
tracer.disable if cpaths.empty?
|
|
57
61
|
end
|
|
58
62
|
end
|
|
59
63
|
|
|
60
64
|
# @sig (TracePoint) -> void
|
|
61
|
-
def tracepoint_class_callback(event)
|
|
65
|
+
private def tracepoint_class_callback(event)
|
|
62
66
|
# If the class is a singleton class, we won't do anything with it so we
|
|
63
67
|
# can bail out immediately. This is several orders of magnitude faster
|
|
64
68
|
# than accessing its name.
|
|
@@ -5,8 +5,8 @@ module Zeitwerk
|
|
|
5
5
|
# @sig (String) -> void
|
|
6
6
|
def initialize(root_file)
|
|
7
7
|
namespace = File.basename(root_file, ".rb")
|
|
8
|
-
|
|
9
|
-
@version_file = File.join(
|
|
8
|
+
root_dir = File.dirname(root_file)
|
|
9
|
+
@version_file = File.join(root_dir, namespace, "version.rb")
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
# @sig (String, String) -> String
|
data/lib/zeitwerk/gem_loader.rb
CHANGED
|
@@ -3,27 +3,31 @@
|
|
|
3
3
|
module Zeitwerk
|
|
4
4
|
# @private
|
|
5
5
|
class GemLoader < Loader
|
|
6
|
+
include RealModName
|
|
7
|
+
|
|
6
8
|
# Users should not create instances directly, the public interface is
|
|
7
9
|
# `Zeitwerk::Loader.for_gem`.
|
|
8
10
|
private_class_method :new
|
|
9
11
|
|
|
10
12
|
# @private
|
|
11
13
|
# @sig (String, bool) -> Zeitwerk::GemLoader
|
|
12
|
-
def self.
|
|
13
|
-
new(root_file, warn_on_extra_files: warn_on_extra_files)
|
|
14
|
+
def self.__new(root_file, namespace:, warn_on_extra_files:)
|
|
15
|
+
new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
# @sig (String, bool) -> void
|
|
17
|
-
def initialize(root_file, warn_on_extra_files:)
|
|
19
|
+
def initialize(root_file, namespace:, warn_on_extra_files:)
|
|
18
20
|
super()
|
|
19
21
|
|
|
20
|
-
@tag
|
|
22
|
+
@tag = File.basename(root_file, ".rb")
|
|
23
|
+
@tag = real_mod_name(namespace) + "-" + @tag unless namespace.equal?(Object)
|
|
24
|
+
|
|
21
25
|
@inflector = GemInflector.new(root_file)
|
|
22
26
|
@root_file = File.expand_path(root_file)
|
|
23
|
-
@
|
|
27
|
+
@root_dir = File.dirname(root_file)
|
|
24
28
|
@warn_on_extra_files = warn_on_extra_files
|
|
25
29
|
|
|
26
|
-
push_dir(@
|
|
30
|
+
push_dir(@root_dir, namespace: namespace)
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
# @sig () -> void
|
|
@@ -38,7 +42,7 @@ module Zeitwerk
|
|
|
38
42
|
def warn_on_extra_files
|
|
39
43
|
expected_namespace_dir = @root_file.delete_suffix(".rb")
|
|
40
44
|
|
|
41
|
-
ls(@
|
|
45
|
+
ls(@root_dir) do |basename, abspath|
|
|
42
46
|
next if abspath == @root_file
|
|
43
47
|
next if abspath == expected_namespace_dir
|
|
44
48
|
|
|
@@ -11,18 +11,18 @@ module Zeitwerk::Loader::Callbacks
|
|
|
11
11
|
cref = autoloads.delete(file)
|
|
12
12
|
cpath = cpath(*cref)
|
|
13
13
|
|
|
14
|
-
# If reloading is enabled, we need to put this constant for unloading
|
|
15
|
-
# regardless of what cdef? says. In Ruby < 3.1 the internal state is not
|
|
16
|
-
# fully cleared. Module#constants still includes it, and you need to
|
|
17
|
-
# remove_const. See https://github.com/ruby/ruby/pull/4715.
|
|
18
|
-
to_unload[cpath] = [file, cref] if reloading_enabled?
|
|
19
14
|
Zeitwerk::Registry.unregister_autoload(file)
|
|
20
15
|
|
|
21
16
|
if cdef?(*cref)
|
|
22
17
|
log("constant #{cpath} loaded from file #{file}") if logger
|
|
18
|
+
to_unload[cpath] = [file, cref] if reloading_enabled?
|
|
23
19
|
run_on_load_callbacks(cpath, cget(*cref), file) unless on_load_callbacks.empty?
|
|
24
20
|
else
|
|
25
|
-
|
|
21
|
+
msg = "expected file #{file} to define constant #{cpath}, but didn't"
|
|
22
|
+
log(msg) if logger
|
|
23
|
+
crem(*cref)
|
|
24
|
+
to_unload[cpath] = [file, cref] if reloading_enabled?
|
|
25
|
+
raise Zeitwerk::NameError.new(msg, cref.last)
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|