zeitwerk 2.6.7 → 2.6.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +230 -24
- data/lib/zeitwerk/cref.rb +99 -0
- data/lib/zeitwerk/gem_inflector.rb +2 -2
- data/lib/zeitwerk/gem_loader.rb +11 -8
- data/lib/zeitwerk/kernel.rb +3 -7
- data/lib/zeitwerk/loader/callbacks.rb +29 -22
- data/lib/zeitwerk/loader/config.rb +7 -8
- data/lib/zeitwerk/loader/eager_load.rb +21 -17
- data/lib/zeitwerk/loader/helpers.rb +61 -56
- data/lib/zeitwerk/loader.rb +186 -94
- 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 +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51e7aed4aef39ce0a2caaea2f23852ea4f2e8fcb7b65819d14a4d92e7aec70d0
|
4
|
+
data.tar.gz: 7e310bac85d85018cb840339a42958b9fbc6fac566fee51e6a5c599a3cb132e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0274e4685362f6585b9fb90291561926c8b45434ea3df71858ed99c5dfbffd54e814ed6dfcc4c6e8bfd8a9f266dbe4261bf6010856558474ad7e85045851e4cd
|
7
|
+
data.tar.gz: 210f457fad164472582fc67264bed2bd981612588bd3a8cdce265b2f2c67b12d10d9d2b9abb555dead008df20d5a73955072e703305e8f55b9d4dd5877d9b693
|
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/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%
|
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,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)
|
@@ -38,6 +40,7 @@
|
|
38
40
|
- [Inflection](#inflection)
|
39
41
|
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
40
42
|
- [Zeitwerk::GemInflector](#zeitwerkgeminflector)
|
43
|
+
- [Zeitwerk::NullInflector](#zeitwerknullinflector)
|
41
44
|
- [Custom inflector](#custom-inflector)
|
42
45
|
- [Callbacks](#callbacks)
|
43
46
|
- [The on_setup callback](#the-on_setup-callback)
|
@@ -55,6 +58,9 @@
|
|
55
58
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
56
59
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
57
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)
|
58
64
|
- [Encodings](#encodings)
|
59
65
|
- [Rules of thumb](#rules-of-thumb)
|
60
66
|
- [Debuggers](#debuggers)
|
@@ -75,15 +81,15 @@
|
|
75
81
|
|
76
82
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
77
83
|
|
78
|
-
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.
|
79
85
|
|
80
|
-
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.
|
81
87
|
|
82
|
-
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.
|
83
89
|
|
84
|
-
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`.
|
85
91
|
|
86
|
-
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.
|
87
93
|
|
88
94
|
<a id="markdown-synopsis" name="synopsis"></a>
|
89
95
|
## Synopsis
|
@@ -143,7 +149,7 @@ Zeitwerk::Loader.eager_load_all
|
|
143
149
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
144
150
|
### The idea: File paths match constant paths
|
145
151
|
|
146
|
-
|
152
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
147
153
|
|
148
154
|
```
|
149
155
|
lib/my_gem.rb -> MyGem
|
@@ -152,7 +158,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
152
158
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
153
159
|
```
|
154
160
|
|
155
|
-
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.
|
156
162
|
|
157
163
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
158
164
|
### Inner simple constants
|
@@ -210,7 +216,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
210
216
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
211
217
|
#### Custom root namespaces
|
212
218
|
|
213
|
-
|
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.
|
214
220
|
|
215
221
|
For example, given:
|
216
222
|
|
@@ -226,14 +232,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
226
232
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
227
233
|
```
|
228
234
|
|
229
|
-
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.
|
230
236
|
|
231
237
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
232
238
|
#### Nested root directories
|
233
239
|
|
234
|
-
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.
|
235
241
|
|
236
|
-
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:
|
237
243
|
|
238
244
|
```
|
239
245
|
app/models/concerns/geolocatable.rb
|
@@ -244,9 +250,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
244
250
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
245
251
|
### Implicit namespaces
|
246
252
|
|
247
|
-
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.
|
248
254
|
|
249
|
-
For
|
255
|
+
For instance, suppose a project includes an `admin` directory:
|
250
256
|
|
251
257
|
```
|
252
258
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
@@ -254,7 +260,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
254
260
|
|
255
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.
|
256
262
|
|
257
|
-
|
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.
|
258
264
|
|
259
265
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
260
266
|
### Explicit namespaces
|
@@ -369,7 +375,7 @@ require "zeitwerk"
|
|
369
375
|
loader = Zeitwerk::Loader.new
|
370
376
|
loader.tag = File.basename(__FILE__, ".rb")
|
371
377
|
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
372
|
-
loader.push_dir(
|
378
|
+
loader.push_dir(File.dirname(__FILE__))
|
373
379
|
```
|
374
380
|
|
375
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:
|
@@ -410,6 +416,65 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
410
416
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
411
417
|
```
|
412
418
|
|
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
|
+
|
462
|
+
|
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).
|
477
|
+
|
413
478
|
<a id="markdown-autoloading" name="autoloading"></a>
|
414
479
|
### Autoloading
|
415
480
|
|
@@ -516,7 +581,7 @@ root_dir2/my_app/routes
|
|
516
581
|
root_dir3/my_app/routes
|
517
582
|
```
|
518
583
|
|
519
|
-
where `
|
584
|
+
where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
520
585
|
|
521
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.
|
522
587
|
|
@@ -673,9 +738,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
673
738
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
674
739
|
```
|
675
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
|
+
|
676
766
|
Overrides need to be configured before calling `setup`.
|
677
767
|
|
678
|
-
|
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.
|
679
769
|
|
680
770
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
681
771
|
#### Zeitwerk::GemInflector
|
@@ -686,6 +776,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
|
|
686
776
|
|
687
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.
|
688
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
|
+
|
689
804
|
<a id="markdown-custom-inflector" name="custom-inflector"></a>
|
690
805
|
#### Custom inflector
|
691
806
|
|
@@ -918,7 +1033,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
|
|
918
1033
|
|
919
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.
|
920
1035
|
|
921
|
-
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.
|
922
1037
|
|
923
1038
|
<a id="markdown-loader-tag" name="loader-tag"></a>
|
924
1039
|
#### Loader tag
|
@@ -944,13 +1059,13 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
|
|
944
1059
|
<a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
|
945
1060
|
### Ignoring parts of the project
|
946
1061
|
|
947
|
-
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".
|
948
1063
|
|
949
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.
|
950
1065
|
|
951
1066
|
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
952
1067
|
|
953
|
-
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
|
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 explicitly if you want it ignored too.
|
954
1069
|
|
955
1070
|
Let's see some use cases.
|
956
1071
|
|
@@ -1154,6 +1269,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
|
|
1154
1269
|
<a id="markdown-introspection" name="introspection"></a>
|
1155
1270
|
### Introspection
|
1156
1271
|
|
1272
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1273
|
+
#### `Zeitwerk::Loader#dirs`
|
1274
|
+
|
1157
1275
|
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1158
1276
|
|
1159
1277
|
```ruby
|
@@ -1175,6 +1293,92 @@ By default, ignored root directories are filtered out. If you want them included
|
|
1175
1293
|
|
1176
1294
|
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1177
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
|
+
|
1178
1382
|
<a id="markdown-encodings" name="encodings"></a>
|
1179
1383
|
### Encodings
|
1180
1384
|
|
@@ -1196,7 +1400,7 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1196
1400
|
|
1197
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.
|
1198
1402
|
|
1199
|
-
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.
|
1200
1404
|
|
1201
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.
|
1202
1406
|
|
@@ -1205,9 +1409,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1205
1409
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1206
1410
|
### Debuggers
|
1207
1411
|
|
1208
|
-
Zeitwerk
|
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)).
|
1413
|
+
|
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.
|
1209
1415
|
|
1210
|
-
[
|
1416
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1211
1417
|
|
1212
1418
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1213
1419
|
## Pronunciation
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This private class encapsulates pairs (mod, cname).
|
4
|
+
#
|
5
|
+
# Objects represent the constant cname in the class or module object mod, and
|
6
|
+
# have API to manage them that encapsulates the constants API. Examples:
|
7
|
+
#
|
8
|
+
# cref.path
|
9
|
+
# cref.set(value)
|
10
|
+
# cref.get
|
11
|
+
#
|
12
|
+
# The constant may or may not exist in mod.
|
13
|
+
class Zeitwerk::Cref
|
14
|
+
include Zeitwerk::RealModName
|
15
|
+
|
16
|
+
# @sig Symbol
|
17
|
+
attr_reader :cname
|
18
|
+
|
19
|
+
# The type of the first argument is Module because Class < Module, class
|
20
|
+
# objects are also valid.
|
21
|
+
#
|
22
|
+
# @sig (Module, Symbol) -> void
|
23
|
+
def initialize(mod, cname)
|
24
|
+
@mod = mod
|
25
|
+
@cname = cname
|
26
|
+
@path = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
if Symbol.method_defined?(:name)
|
30
|
+
# Symbol#name was introduced in Ruby 3.0. It returns always the same
|
31
|
+
# frozen object, so we may save a few string allocations.
|
32
|
+
#
|
33
|
+
# @sig () -> String
|
34
|
+
def path
|
35
|
+
@path ||= Object.equal?(@mod) ? @cname.name : "#{real_mod_name(@mod)}::#{@cname.name}"
|
36
|
+
end
|
37
|
+
else
|
38
|
+
# @sig () -> String
|
39
|
+
def path
|
40
|
+
@path ||= Object.equal?(@mod) ? @cname.to_s : "#{real_mod_name(@mod)}::#{@cname}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# The autoload? predicate takes into account the ancestor chain of the
|
45
|
+
# receiver, like const_defined? and other methods in the constants API do.
|
46
|
+
#
|
47
|
+
# For example, given
|
48
|
+
#
|
49
|
+
# class A
|
50
|
+
# autoload :X, "x.rb"
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# class B < A
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# B.autoload?(:X) returns "x.rb".
|
57
|
+
#
|
58
|
+
# We need a way to retrieve it ignoring ancestors.
|
59
|
+
#
|
60
|
+
# @sig () -> String?
|
61
|
+
if method(:autoload?).arity == 1
|
62
|
+
# @sig () -> String?
|
63
|
+
def autoload?
|
64
|
+
@mod.autoload?(@cname) if self.defined?
|
65
|
+
end
|
66
|
+
else
|
67
|
+
# @sig () -> String?
|
68
|
+
def autoload?
|
69
|
+
@mod.autoload?(@cname, false)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# @sig (String) -> bool
|
74
|
+
def autoload(abspath)
|
75
|
+
@mod.autoload(@cname, abspath)
|
76
|
+
end
|
77
|
+
|
78
|
+
# @sig () -> bool
|
79
|
+
def defined?
|
80
|
+
@mod.const_defined?(@cname, false)
|
81
|
+
end
|
82
|
+
|
83
|
+
# @sig (Object) -> Object
|
84
|
+
def set(value)
|
85
|
+
@mod.const_set(@cname, value)
|
86
|
+
end
|
87
|
+
|
88
|
+
# @raise [NameError]
|
89
|
+
# @sig () -> Object
|
90
|
+
def get
|
91
|
+
@mod.const_get(@cname, false)
|
92
|
+
end
|
93
|
+
|
94
|
+
# @raise [NameError]
|
95
|
+
# @sig () -> void
|
96
|
+
def remove
|
97
|
+
@mod.__send__(:remove_const, @cname)
|
98
|
+
end
|
99
|
+
end
|
@@ -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,13 +42,12 @@ 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, ftype|
|
42
46
|
next if abspath == @root_file
|
43
47
|
next if abspath == expected_namespace_dir
|
44
48
|
|
45
49
|
basename_without_ext = basename.delete_suffix(".rb")
|
46
50
|
cname = inflector.camelize(basename_without_ext, abspath).to_sym
|
47
|
-
ftype = dir?(abspath) ? "directory" : "file"
|
48
51
|
|
49
52
|
warn(<<~EOS)
|
50
53
|
WARNING: Zeitwerk defines the constant #{cname} after the #{ftype}
|
data/lib/zeitwerk/kernel.rb
CHANGED
@@ -14,10 +14,6 @@ module Kernel
|
|
14
14
|
# should not require anything. But if someone has legacy require calls around,
|
15
15
|
# they will work as expected, and in a compatible way. This feature is by now
|
16
16
|
# EXPERIMENTAL and UNDOCUMENTED.
|
17
|
-
#
|
18
|
-
# We cannot decorate with prepend + super because Kernel has already been
|
19
|
-
# included in Object, and changes in ancestors don't get propagated into
|
20
|
-
# already existing ancestor chains on Ruby < 3.0.
|
21
17
|
alias_method :zeitwerk_original_require, :require
|
22
18
|
class << self
|
23
19
|
alias_method :zeitwerk_original_require, :require
|
@@ -28,10 +24,10 @@ module Kernel
|
|
28
24
|
if loader = Zeitwerk::Registry.loader_for(path)
|
29
25
|
if path.end_with?(".rb")
|
30
26
|
required = zeitwerk_original_require(path)
|
31
|
-
loader.
|
27
|
+
loader.__on_file_autoloaded(path) if required
|
32
28
|
required
|
33
29
|
else
|
34
|
-
loader.
|
30
|
+
loader.__on_dir_autoloaded(path)
|
35
31
|
true
|
36
32
|
end
|
37
33
|
else
|
@@ -39,7 +35,7 @@ module Kernel
|
|
39
35
|
if required
|
40
36
|
abspath = $LOADED_FEATURES.last
|
41
37
|
if loader = Zeitwerk::Registry.loader_for(abspath)
|
42
|
-
loader.
|
38
|
+
loader.__on_file_autoloaded(abspath)
|
43
39
|
end
|
44
40
|
end
|
45
41
|
required
|