zeitwerk 2.6.7 → 2.6.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +180 -23
- data/lib/zeitwerk/gem_inflector.rb +2 -2
- data/lib/zeitwerk/gem_loader.rb +11 -7
- data/lib/zeitwerk/kernel.rb +3 -7
- data/lib/zeitwerk/loader/callbacks.rb +16 -8
- data/lib/zeitwerk/loader/config.rb +7 -8
- data/lib/zeitwerk/loader/eager_load.rb +9 -3
- data/lib/zeitwerk/loader/helpers.rb +44 -0
- data/lib/zeitwerk/loader.rb +102 -40
- 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 +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: 5d4ce4eb7136dafe17130fc5d895e525d80ae947f02dd9420b5bc85a4d6f0dfd
|
4
|
+
data.tar.gz: edcac638955fef258ad36a358b78da6831a715ed46446848e39f9f1d8fb7de0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0a6e64c56635c9c6a8c9b24bdeb7509e4a7f14489f32aae18b02221c23b95b34096184c78d2d1509130ce75031ed0f3a7751b78e43d9b72122440f07cf980bc
|
7
|
+
data.tar.gz: 06440147c101a95ac2c8b7dcd43920a3efc3da2f58b902c157730a449d57b6ef74e0d3db7f3d2735be816a74ceb774e2d4075741556c7e2ba7fa00b8130c1723
|
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,8 @@
|
|
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)
|
58
63
|
- [Encodings](#encodings)
|
59
64
|
- [Rules of thumb](#rules-of-thumb)
|
60
65
|
- [Debuggers](#debuggers)
|
@@ -75,15 +80,15 @@
|
|
75
80
|
|
76
81
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
77
82
|
|
78
|
-
Given a [conventional file structure](#file-structure), Zeitwerk is
|
83
|
+
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
84
|
|
80
|
-
Zeitwerk
|
85
|
+
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
86
|
|
82
|
-
The gem is designed
|
87
|
+
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
88
|
|
84
|
-
Internally, Zeitwerk
|
89
|
+
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
90
|
|
86
|
-
Furthermore, Zeitwerk
|
91
|
+
Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
|
87
92
|
|
88
93
|
<a id="markdown-synopsis" name="synopsis"></a>
|
89
94
|
## Synopsis
|
@@ -143,7 +148,7 @@ Zeitwerk::Loader.eager_load_all
|
|
143
148
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
144
149
|
### The idea: File paths match constant paths
|
145
150
|
|
146
|
-
|
151
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
147
152
|
|
148
153
|
```
|
149
154
|
lib/my_gem.rb -> MyGem
|
@@ -152,7 +157,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
152
157
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
153
158
|
```
|
154
159
|
|
155
|
-
You can tune
|
160
|
+
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
161
|
|
157
162
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
158
163
|
### Inner simple constants
|
@@ -210,7 +215,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
210
215
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
211
216
|
#### Custom root namespaces
|
212
217
|
|
213
|
-
|
218
|
+
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
219
|
|
215
220
|
For example, given:
|
216
221
|
|
@@ -226,14 +231,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
226
231
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
227
232
|
```
|
228
233
|
|
229
|
-
Please
|
234
|
+
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
235
|
|
231
236
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
232
237
|
#### Nested root directories
|
233
238
|
|
234
|
-
Root directories
|
239
|
+
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
240
|
|
236
|
-
Zeitwerk
|
241
|
+
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
242
|
|
238
243
|
```
|
239
244
|
app/models/concerns/geolocatable.rb
|
@@ -244,9 +249,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
244
249
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
245
250
|
### Implicit namespaces
|
246
251
|
|
247
|
-
If a namespace
|
252
|
+
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
253
|
|
249
|
-
For
|
254
|
+
For instance, suppose a project includes an `admin` directory:
|
250
255
|
|
251
256
|
```
|
252
257
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
@@ -254,7 +259,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
254
259
|
|
255
260
|
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
261
|
|
257
|
-
|
262
|
+
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
263
|
|
259
264
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
260
265
|
### Explicit namespaces
|
@@ -369,7 +374,7 @@ require "zeitwerk"
|
|
369
374
|
loader = Zeitwerk::Loader.new
|
370
375
|
loader.tag = File.basename(__FILE__, ".rb")
|
371
376
|
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
372
|
-
loader.push_dir(
|
377
|
+
loader.push_dir(File.dirname(__FILE__))
|
373
378
|
```
|
374
379
|
|
375
380
|
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 +415,65 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
410
415
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
411
416
|
```
|
412
417
|
|
418
|
+
<a id="markdown-for_gem_extension" name="for_gem_extension"></a>
|
419
|
+
#### for_gem_extension
|
420
|
+
|
421
|
+
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/):
|
422
|
+
|
423
|
+
* The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
|
424
|
+
* The namespace should be `Net::HTTP::NicheFeature`.
|
425
|
+
* The entry point should be `lib/net/http/niche_feature.rb`.
|
426
|
+
* 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.
|
427
|
+
|
428
|
+
The top-level file mentioned in the last point is optional. In particular, from
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
gem "net-http-niche_feature"
|
432
|
+
```
|
433
|
+
|
434
|
+
if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
|
435
|
+
|
436
|
+
Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
|
437
|
+
|
438
|
+
The structure of the gem would be like this:
|
439
|
+
|
440
|
+
```ruby
|
441
|
+
# lib/net-http-niche_feature.rb (optional)
|
442
|
+
|
443
|
+
# For technical reasons, this cannot be require_relative.
|
444
|
+
require "net/http/niche_feature"
|
445
|
+
|
446
|
+
|
447
|
+
# lib/net/http/niche_feature.rb
|
448
|
+
|
449
|
+
require "net/http"
|
450
|
+
require "zeitwerk"
|
451
|
+
|
452
|
+
loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
|
453
|
+
loader.setup
|
454
|
+
|
455
|
+
module Net::HTTP::NicheFeature
|
456
|
+
# Since the setup has been performed, at this point we are already able
|
457
|
+
# to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
|
458
|
+
include MyMixin
|
459
|
+
end
|
460
|
+
|
461
|
+
|
462
|
+
# lib/net/http/niche_feature/version.rb
|
463
|
+
|
464
|
+
module Net::HTTP::NicheFeature
|
465
|
+
VERSION = "1.0.0"
|
466
|
+
end
|
467
|
+
```
|
468
|
+
|
469
|
+
`Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
|
470
|
+
|
471
|
+
If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
|
472
|
+
|
473
|
+
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.
|
474
|
+
|
475
|
+
`Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
476
|
+
|
413
477
|
<a id="markdown-autoloading" name="autoloading"></a>
|
414
478
|
### Autoloading
|
415
479
|
|
@@ -516,7 +580,7 @@ root_dir2/my_app/routes
|
|
516
580
|
root_dir3/my_app/routes
|
517
581
|
```
|
518
582
|
|
519
|
-
where `
|
583
|
+
where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
520
584
|
|
521
585
|
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
586
|
|
@@ -673,9 +737,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
673
737
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
674
738
|
```
|
675
739
|
|
740
|
+
Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
|
741
|
+
|
742
|
+
```ruby
|
743
|
+
loader.inflector.inflect("xml" => "XML")
|
744
|
+
```
|
745
|
+
|
746
|
+
then the following constants are expected:
|
747
|
+
|
748
|
+
```
|
749
|
+
xml.rb -> XML
|
750
|
+
foo/xml -> Foo::XML
|
751
|
+
foo/bar/xml.rb -> Foo::Bar::XML
|
752
|
+
```
|
753
|
+
|
754
|
+
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:
|
755
|
+
|
756
|
+
```ruby
|
757
|
+
loader.inflector.inflect(
|
758
|
+
"xml" => "XML",
|
759
|
+
"xml_parser" => "XMLParser"
|
760
|
+
)
|
761
|
+
```
|
762
|
+
|
763
|
+
If you need more flexibility, you can define a custom inflector, as explained down below.
|
764
|
+
|
676
765
|
Overrides need to be configured before calling `setup`.
|
677
766
|
|
678
|
-
|
767
|
+
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
768
|
|
680
769
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
681
770
|
#### Zeitwerk::GemInflector
|
@@ -686,6 +775,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
|
|
686
775
|
|
687
776
|
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
777
|
|
778
|
+
<a id="markdown-zeitwerknullinflector" name="zeitwerknullinflector"></a>
|
779
|
+
#### Zeitwerk::NullInflector
|
780
|
+
|
781
|
+
This is an experimental inflector that simply returns its input unchanged.
|
782
|
+
|
783
|
+
```ruby
|
784
|
+
loader.inflector = Zeitwerk::NullInflector.new
|
785
|
+
```
|
786
|
+
|
787
|
+
In a project using this inflector, the names of files and directories are equal to the constants they define:
|
788
|
+
|
789
|
+
```
|
790
|
+
User.rb -> User
|
791
|
+
HTMLParser.rb -> HTMLParser
|
792
|
+
Admin/Role.rb -> Admin::Role
|
793
|
+
```
|
794
|
+
|
795
|
+
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.
|
796
|
+
|
797
|
+
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!
|
798
|
+
|
799
|
+
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.
|
800
|
+
|
801
|
+
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`.
|
802
|
+
|
689
803
|
<a id="markdown-custom-inflector" name="custom-inflector"></a>
|
690
804
|
#### Custom inflector
|
691
805
|
|
@@ -918,7 +1032,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
|
|
918
1032
|
|
919
1033
|
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
1034
|
|
921
|
-
As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_.
|
1035
|
+
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
1036
|
|
923
1037
|
<a id="markdown-loader-tag" name="loader-tag"></a>
|
924
1038
|
#### Loader tag
|
@@ -944,7 +1058,7 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
|
|
944
1058
|
<a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
|
945
1059
|
### Ignoring parts of the project
|
946
1060
|
|
947
|
-
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
|
1061
|
+
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have the extension ".rb".
|
948
1062
|
|
949
1063
|
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
1064
|
|
@@ -1154,6 +1268,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
|
|
1154
1268
|
<a id="markdown-introspection" name="introspection"></a>
|
1155
1269
|
### Introspection
|
1156
1270
|
|
1271
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1272
|
+
#### `Zeitwerk::Loader#dirs`
|
1273
|
+
|
1157
1274
|
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1158
1275
|
|
1159
1276
|
```ruby
|
@@ -1175,6 +1292,44 @@ By default, ignored root directories are filtered out. If you want them included
|
|
1175
1292
|
|
1176
1293
|
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1177
1294
|
|
1295
|
+
<a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
|
1296
|
+
#### `Zeitwerk::Loader#cpath_expected_at`
|
1297
|
+
|
1298
|
+
Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
|
1299
|
+
|
1300
|
+
Some examples, assuming that `app/models` is a root directory:
|
1301
|
+
|
1302
|
+
```ruby
|
1303
|
+
loader.cpath_expected_at("app/models") # => "Object"
|
1304
|
+
loader.cpath_expected_at("app/models/user.rb") # => "User"
|
1305
|
+
loader.cpath_expected_at("app/models/hotel") # => "Hotel"
|
1306
|
+
loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
|
1307
|
+
```
|
1308
|
+
|
1309
|
+
If `collapsed` is a collapsed directory:
|
1310
|
+
|
1311
|
+
```ruby
|
1312
|
+
loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
|
1313
|
+
loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
|
1314
|
+
loader.cpath_expected_at("a/b") # => "A::B"
|
1315
|
+
```
|
1316
|
+
|
1317
|
+
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.
|
1318
|
+
|
1319
|
+
`Zeitwerk::Error` is raised if the given path does not exist:
|
1320
|
+
|
1321
|
+
```ruby
|
1322
|
+
loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
|
1323
|
+
```
|
1324
|
+
|
1325
|
+
`Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
|
1326
|
+
|
1327
|
+
```ruby
|
1328
|
+
loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
|
1329
|
+
```
|
1330
|
+
|
1331
|
+
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.
|
1332
|
+
|
1178
1333
|
<a id="markdown-encodings" name="encodings"></a>
|
1179
1334
|
### Encodings
|
1180
1335
|
|
@@ -1196,7 +1351,7 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1196
1351
|
|
1197
1352
|
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
1353
|
|
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
|
1354
|
+
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
1355
|
|
1201
1356
|
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
1357
|
|
@@ -1205,9 +1360,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1205
1360
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1206
1361
|
### Debuggers
|
1207
1362
|
|
1208
|
-
Zeitwerk
|
1363
|
+
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)).
|
1364
|
+
|
1365
|
+
[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
1366
|
|
1210
|
-
[
|
1367
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1211
1368
|
|
1212
1369
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1213
1370
|
## Pronunciation
|
@@ -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
|
|
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
|
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
module Zeitwerk::Loader::Callbacks
|
4
4
|
include Zeitwerk::RealModName
|
5
|
+
extend Zeitwerk::Internal
|
5
6
|
|
6
7
|
# Invoked from our decorated Kernel#require when a managed file is autoloaded.
|
7
8
|
#
|
8
|
-
# @private
|
9
9
|
# @sig (String) -> void
|
10
|
-
def on_file_autoloaded(file)
|
10
|
+
internal def on_file_autoloaded(file)
|
11
11
|
cref = autoloads.delete(file)
|
12
12
|
cpath = cpath(*cref)
|
13
13
|
|
@@ -20,8 +20,16 @@ module Zeitwerk::Loader::Callbacks
|
|
20
20
|
else
|
21
21
|
msg = "expected file #{file} to define constant #{cpath}, but didn't"
|
22
22
|
log(msg) if logger
|
23
|
+
|
24
|
+
# Ruby still keeps the autoload defined, but we remove it because the
|
25
|
+
# contract in Zeitwerk is more strict.
|
23
26
|
crem(*cref)
|
27
|
+
|
28
|
+
# Since the expected constant was not defined, there is nothing to unload.
|
29
|
+
# However, if the exception is rescued and reloading is enabled, we still
|
30
|
+
# need to deleted the file from $LOADED_FEATURES.
|
24
31
|
to_unload[cpath] = [file, cref] if reloading_enabled?
|
32
|
+
|
25
33
|
raise Zeitwerk::NameError.new(msg, cref.last)
|
26
34
|
end
|
27
35
|
end
|
@@ -29,11 +37,11 @@ module Zeitwerk::Loader::Callbacks
|
|
29
37
|
# Invoked from our decorated Kernel#require when a managed directory is
|
30
38
|
# autoloaded.
|
31
39
|
#
|
32
|
-
# @private
|
33
40
|
# @sig (String) -> void
|
34
|
-
def on_dir_autoloaded(dir)
|
35
|
-
# Module#autoload does not serialize concurrent requires, and
|
36
|
-
# directories ourselves
|
41
|
+
internal def on_dir_autoloaded(dir)
|
42
|
+
# Module#autoload does not serialize concurrent requires in CRuby < 3.2, and
|
43
|
+
# we handle directories ourselves without going through Kernel#require, so
|
44
|
+
# the callback needs to account for concurrency.
|
37
45
|
#
|
38
46
|
# Multi-threading would introduce a race condition here in which thread t1
|
39
47
|
# autovivifies the module, and while autoloads for its children are being
|
@@ -43,7 +51,7 @@ module Zeitwerk::Loader::Callbacks
|
|
43
51
|
# That not only would reassign the constant (undesirable per se) but, worse,
|
44
52
|
# the module object created by t2 wouldn't have any of the autoloads for its
|
45
53
|
# children, since t1 would have correctly deleted its namespace_dirs entry.
|
46
|
-
|
54
|
+
dirs_autoload_monitor.synchronize do
|
47
55
|
if cref = autoloads.delete(dir)
|
48
56
|
autovivified_module = cref[0].const_set(cref[1], Module.new)
|
49
57
|
cpath = autovivified_module.name
|
@@ -73,7 +81,7 @@ module Zeitwerk::Loader::Callbacks
|
|
73
81
|
def on_namespace_loaded(namespace)
|
74
82
|
if dirs = namespace_dirs.delete(real_mod_name(namespace))
|
75
83
|
dirs.each do |dir|
|
76
|
-
|
84
|
+
define_autoloads_for_dir(dir, namespace)
|
77
85
|
end
|
78
86
|
end
|
79
87
|
end
|
@@ -109,8 +109,7 @@ module Zeitwerk::Loader::Config
|
|
109
109
|
# @raise [Zeitwerk::Error]
|
110
110
|
# @sig (String | Pathname, Module) -> void
|
111
111
|
def push_dir(path, namespace: Object)
|
112
|
-
# Note that Class < Module.
|
113
|
-
unless namespace.is_a?(Module)
|
112
|
+
unless namespace.is_a?(Module) # Note that Class < Module.
|
114
113
|
raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
|
115
114
|
end
|
116
115
|
|
@@ -298,9 +297,9 @@ module Zeitwerk::Loader::Config
|
|
298
297
|
# Common use case.
|
299
298
|
return false if ignored_paths.empty?
|
300
299
|
|
301
|
-
walk_up(abspath) do |
|
302
|
-
return true if ignored_path?(
|
303
|
-
return false if roots.key?(
|
300
|
+
walk_up(abspath) do |path|
|
301
|
+
return true if ignored_path?(path)
|
302
|
+
return false if roots.key?(path)
|
304
303
|
end
|
305
304
|
|
306
305
|
false
|
@@ -328,9 +327,9 @@ module Zeitwerk::Loader::Config
|
|
328
327
|
# Optimize this common use case.
|
329
328
|
return false if eager_load_exclusions.empty?
|
330
329
|
|
331
|
-
walk_up(abspath) do |
|
332
|
-
return true if eager_load_exclusions.member?(
|
333
|
-
return false if roots.key?(
|
330
|
+
walk_up(abspath) do |path|
|
331
|
+
return true if eager_load_exclusions.member?(path)
|
332
|
+
return false if roots.key?(path)
|
334
333
|
end
|
335
334
|
|
336
335
|
false
|
@@ -45,8 +45,10 @@ module Zeitwerk::Loader::EagerLoad
|
|
45
45
|
|
46
46
|
break if root_namespace = roots[dir]
|
47
47
|
|
48
|
+
basename = File.basename(dir)
|
49
|
+
return if hidden?(basename)
|
50
|
+
|
48
51
|
unless collapse?(dir)
|
49
|
-
basename = File.basename(dir)
|
50
52
|
cnames << inflector.camelize(basename, dir).to_sym
|
51
53
|
end
|
52
54
|
end
|
@@ -119,6 +121,8 @@ module Zeitwerk::Loader::EagerLoad
|
|
119
121
|
raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(abspath)
|
120
122
|
|
121
123
|
basename = File.basename(abspath, ".rb")
|
124
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
|
125
|
+
|
122
126
|
base_cname = inflector.camelize(basename, abspath).to_sym
|
123
127
|
|
124
128
|
root_namespace = nil
|
@@ -129,8 +133,10 @@ module Zeitwerk::Loader::EagerLoad
|
|
129
133
|
|
130
134
|
break if root_namespace = roots[dir]
|
131
135
|
|
136
|
+
basename = File.basename(dir)
|
137
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
|
138
|
+
|
132
139
|
unless collapse?(dir)
|
133
|
-
basename = File.basename(dir)
|
134
140
|
cnames << inflector.camelize(basename, dir).to_sym
|
135
141
|
end
|
136
142
|
end
|
@@ -165,7 +171,7 @@ module Zeitwerk::Loader::EagerLoad
|
|
165
171
|
next if honour_exclusions && eager_load_exclusions.member?(abspath)
|
166
172
|
|
167
173
|
if ruby?(abspath)
|
168
|
-
if (cref = autoloads[abspath])
|
174
|
+
if (cref = autoloads[abspath])
|
169
175
|
cget(*cref)
|
170
176
|
end
|
171
177
|
else
|
@@ -140,4 +140,48 @@ module Zeitwerk::Loader::Helpers
|
|
140
140
|
private def crem(parent, cname)
|
141
141
|
parent.__send__(:remove_const, cname)
|
142
142
|
end
|
143
|
+
|
144
|
+
CNAME_VALIDATOR = Module.new
|
145
|
+
private_constant :CNAME_VALIDATOR
|
146
|
+
|
147
|
+
# @raise [Zeitwerk::NameError]
|
148
|
+
# @sig (String, String) -> Symbol
|
149
|
+
private def cname_for(basename, abspath)
|
150
|
+
cname = inflector.camelize(basename, abspath)
|
151
|
+
|
152
|
+
unless cname.is_a?(String)
|
153
|
+
raise TypeError, "#{inflector.class}#camelize must return a String, received #{cname.inspect}"
|
154
|
+
end
|
155
|
+
|
156
|
+
if cname.include?("::")
|
157
|
+
raise Zeitwerk::NameError.new(<<~MESSAGE, cname)
|
158
|
+
wrong constant name #{cname} inferred by #{inflector.class} from
|
159
|
+
|
160
|
+
#{abspath}
|
161
|
+
|
162
|
+
#{inflector.class}#camelize should return a simple constant name without "::"
|
163
|
+
MESSAGE
|
164
|
+
end
|
165
|
+
|
166
|
+
begin
|
167
|
+
CNAME_VALIDATOR.const_defined?(cname, false)
|
168
|
+
rescue ::NameError => error
|
169
|
+
path_type = ruby?(abspath) ? "file" : "directory"
|
170
|
+
|
171
|
+
raise Zeitwerk::NameError.new(<<~MESSAGE, error.name)
|
172
|
+
#{error.message} inferred by #{inflector.class} from #{path_type}
|
173
|
+
|
174
|
+
#{abspath}
|
175
|
+
|
176
|
+
Possible ways to address this:
|
177
|
+
|
178
|
+
* Tell Zeitwerk to ignore this particular #{path_type}.
|
179
|
+
* Tell Zeitwerk to ignore one of its parent directories.
|
180
|
+
* Rename the #{path_type} to comply with the naming conventions.
|
181
|
+
* Modify the inflector to handle this case.
|
182
|
+
MESSAGE
|
183
|
+
end
|
184
|
+
|
185
|
+
cname.to_sym
|
186
|
+
end
|
143
187
|
end
|
data/lib/zeitwerk/loader.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "monitor"
|
3
4
|
require "set"
|
4
5
|
|
5
6
|
module Zeitwerk
|
@@ -91,9 +92,9 @@ module Zeitwerk
|
|
91
92
|
attr_reader :mutex
|
92
93
|
private :mutex
|
93
94
|
|
94
|
-
# @sig
|
95
|
-
attr_reader :
|
96
|
-
private :
|
95
|
+
# @sig Monitor
|
96
|
+
attr_reader :dirs_autoload_monitor
|
97
|
+
private :dirs_autoload_monitor
|
97
98
|
|
98
99
|
def initialize
|
99
100
|
super
|
@@ -103,11 +104,12 @@ module Zeitwerk
|
|
103
104
|
@to_unload = {}
|
104
105
|
@namespace_dirs = Hash.new { |h, cpath| h[cpath] = [] }
|
105
106
|
@shadowed_files = Set.new
|
106
|
-
@mutex = Mutex.new
|
107
|
-
@mutex2 = Mutex.new
|
108
107
|
@setup = false
|
109
108
|
@eager_loaded = false
|
110
109
|
|
110
|
+
@mutex = Mutex.new
|
111
|
+
@dirs_autoload_monitor = Monitor.new
|
112
|
+
|
111
113
|
Registry.register_loader(self)
|
112
114
|
end
|
113
115
|
|
@@ -119,7 +121,7 @@ module Zeitwerk
|
|
119
121
|
break if @setup
|
120
122
|
|
121
123
|
actual_roots.each do |root_dir, root_namespace|
|
122
|
-
|
124
|
+
define_autoloads_for_dir(root_dir, root_namespace)
|
123
125
|
end
|
124
126
|
|
125
127
|
on_setup_callbacks.each(&:call)
|
@@ -228,6 +230,54 @@ module Zeitwerk
|
|
228
230
|
setup
|
229
231
|
end
|
230
232
|
|
233
|
+
# @sig (String | Pathname) -> String?
|
234
|
+
def cpath_expected_at(path)
|
235
|
+
abspath = File.expand_path(path)
|
236
|
+
|
237
|
+
raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)
|
238
|
+
|
239
|
+
return unless dir?(abspath) || ruby?(abspath)
|
240
|
+
return if ignored_path?(abspath)
|
241
|
+
|
242
|
+
paths = []
|
243
|
+
|
244
|
+
if ruby?(abspath)
|
245
|
+
basename = File.basename(abspath, ".rb")
|
246
|
+
return if hidden?(basename)
|
247
|
+
|
248
|
+
paths << [basename, abspath]
|
249
|
+
walk_up_from = File.dirname(abspath)
|
250
|
+
else
|
251
|
+
walk_up_from = abspath
|
252
|
+
end
|
253
|
+
|
254
|
+
root_namespace = nil
|
255
|
+
|
256
|
+
walk_up(walk_up_from) do |dir|
|
257
|
+
break if root_namespace = roots[dir]
|
258
|
+
return if ignored_path?(dir)
|
259
|
+
|
260
|
+
basename = File.basename(dir)
|
261
|
+
return if hidden?(basename)
|
262
|
+
|
263
|
+
paths << [basename, abspath] unless collapse?(dir)
|
264
|
+
end
|
265
|
+
|
266
|
+
return unless root_namespace
|
267
|
+
|
268
|
+
if paths.empty?
|
269
|
+
real_mod_name(root_namespace)
|
270
|
+
else
|
271
|
+
cnames = paths.reverse_each.map { |b, a| cname_for(b, a) }
|
272
|
+
|
273
|
+
if root_namespace == Object
|
274
|
+
cnames.join("::")
|
275
|
+
else
|
276
|
+
"#{real_mod_name(root_namespace)}::#{cnames.join("::")}"
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
231
281
|
# Says if the given constant path would be unloaded on reload. This
|
232
282
|
# predicate returns `false` if reloading is disabled.
|
233
283
|
#
|
@@ -264,12 +314,15 @@ module Zeitwerk
|
|
264
314
|
# --- Class methods ---------------------------------------------------------------------------
|
265
315
|
|
266
316
|
class << self
|
317
|
+
include RealModName
|
318
|
+
|
267
319
|
# @sig #call | #debug | nil
|
268
320
|
attr_accessor :default_logger
|
269
321
|
|
270
322
|
# This is a shortcut for
|
271
323
|
#
|
272
324
|
# require "zeitwerk"
|
325
|
+
#
|
273
326
|
# loader = Zeitwerk::Loader.new
|
274
327
|
# loader.tag = File.basename(__FILE__, ".rb")
|
275
328
|
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
@@ -284,7 +337,36 @@ module Zeitwerk
|
|
284
337
|
# @sig (bool) -> Zeitwerk::GemLoader
|
285
338
|
def for_gem(warn_on_extra_files: true)
|
286
339
|
called_from = caller_locations(1, 1).first.path
|
287
|
-
Registry.loader_for_gem(called_from, warn_on_extra_files: warn_on_extra_files)
|
340
|
+
Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
|
341
|
+
end
|
342
|
+
|
343
|
+
# This is a shortcut for
|
344
|
+
#
|
345
|
+
# require "zeitwerk"
|
346
|
+
#
|
347
|
+
# loader = Zeitwerk::Loader.new
|
348
|
+
# loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb")
|
349
|
+
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
350
|
+
# loader.push_dir(__dir__, namespace: namespace)
|
351
|
+
#
|
352
|
+
# except that this method returns the same object in subsequent calls from
|
353
|
+
# the same file, in the unlikely case the gem wants to be able to reload.
|
354
|
+
#
|
355
|
+
# This method returns a subclass of Zeitwerk::Loader, but the exact type
|
356
|
+
# is private, client code can only rely on the interface.
|
357
|
+
#
|
358
|
+
# @sig (bool) -> Zeitwerk::GemLoader
|
359
|
+
def for_gem_extension(namespace)
|
360
|
+
unless namespace.is_a?(Module) # Note that Class < Module.
|
361
|
+
raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
|
362
|
+
end
|
363
|
+
|
364
|
+
unless real_mod_name(namespace)
|
365
|
+
raise Zeitwerk::Error, "extending anonymous namespaces is unsupported"
|
366
|
+
end
|
367
|
+
|
368
|
+
called_from = caller_locations(1, 1).first.path
|
369
|
+
Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false)
|
288
370
|
end
|
289
371
|
|
290
372
|
# Broadcasts `eager_load` to all loaders. Those that have not been setup
|
@@ -325,36 +407,17 @@ module Zeitwerk
|
|
325
407
|
end
|
326
408
|
|
327
409
|
# @sig (String, Module) -> void
|
328
|
-
private def
|
410
|
+
private def define_autoloads_for_dir(dir, parent)
|
329
411
|
ls(dir) do |basename, abspath|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
412
|
+
if ruby?(basename)
|
413
|
+
basename.delete_suffix!(".rb")
|
414
|
+
autoload_file(parent, cname_for(basename, abspath), abspath)
|
415
|
+
else
|
416
|
+
if collapse?(abspath)
|
417
|
+
define_autoloads_for_dir(abspath, parent)
|
335
418
|
else
|
336
|
-
|
337
|
-
set_autoloads_in_dir(abspath, parent)
|
338
|
-
else
|
339
|
-
cname = inflector.camelize(basename, abspath).to_sym
|
340
|
-
autoload_subdir(parent, cname, abspath)
|
341
|
-
end
|
419
|
+
autoload_subdir(parent, cname_for(basename, abspath), abspath)
|
342
420
|
end
|
343
|
-
rescue ::NameError => error
|
344
|
-
path_type = ruby?(abspath) ? "file" : "directory"
|
345
|
-
|
346
|
-
raise NameError.new(<<~MESSAGE, error.name)
|
347
|
-
#{error.message} inferred by #{inflector.class} from #{path_type}
|
348
|
-
|
349
|
-
#{abspath}
|
350
|
-
|
351
|
-
Possible ways to address this:
|
352
|
-
|
353
|
-
* Tell Zeitwerk to ignore this particular #{path_type}.
|
354
|
-
* Tell Zeitwerk to ignore one of its parent directories.
|
355
|
-
* Rename the #{path_type} to comply with the naming conventions.
|
356
|
-
* Modify the inflector to handle this case.
|
357
|
-
MESSAGE
|
358
421
|
end
|
359
422
|
end
|
360
423
|
end
|
@@ -380,12 +443,12 @@ module Zeitwerk
|
|
380
443
|
elsif !cdef?(parent, cname)
|
381
444
|
# First time we find this namespace, set an autoload for it.
|
382
445
|
namespace_dirs[cpath(parent, cname)] << subdir
|
383
|
-
|
446
|
+
define_autoload(parent, cname, subdir)
|
384
447
|
else
|
385
448
|
# For whatever reason the constant that corresponds to this namespace has
|
386
449
|
# already been defined, we have to recurse.
|
387
450
|
log("the namespace #{cpath(parent, cname)} already exists, descending into #{subdir}") if logger
|
388
|
-
|
451
|
+
define_autoloads_for_dir(subdir, cget(parent, cname))
|
389
452
|
end
|
390
453
|
end
|
391
454
|
|
@@ -408,7 +471,7 @@ module Zeitwerk
|
|
408
471
|
shadowed_files << file
|
409
472
|
log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger
|
410
473
|
else
|
411
|
-
|
474
|
+
define_autoload(parent, cname, file)
|
412
475
|
end
|
413
476
|
end
|
414
477
|
|
@@ -422,12 +485,12 @@ module Zeitwerk
|
|
422
485
|
|
423
486
|
log("earlier autoload for #{cpath(parent, cname)} discarded, it is actually an explicit namespace defined in #{file}") if logger
|
424
487
|
|
425
|
-
|
488
|
+
define_autoload(parent, cname, file)
|
426
489
|
register_explicit_namespace(cpath(parent, cname))
|
427
490
|
end
|
428
491
|
|
429
492
|
# @sig (Module, Symbol, String) -> void
|
430
|
-
private def
|
493
|
+
private def define_autoload(parent, cname, abspath)
|
431
494
|
parent.autoload(cname, abspath)
|
432
495
|
|
433
496
|
if logger
|
@@ -479,7 +542,6 @@ module Zeitwerk
|
|
479
542
|
raise Error,
|
480
543
|
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
|
481
544
|
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
482
|
-
EOS
|
483
545
|
end
|
484
546
|
end
|
485
547
|
end
|
data/lib/zeitwerk/registry.rb
CHANGED
@@ -86,8 +86,8 @@ module Zeitwerk
|
|
86
86
|
#
|
87
87
|
# @private
|
88
88
|
# @sig (String) -> Zeitwerk::Loader
|
89
|
-
def loader_for_gem(root_file, warn_on_extra_files:)
|
90
|
-
gem_loaders_by_root_file[root_file] ||= GemLoader.
|
89
|
+
def loader_for_gem(root_file, namespace:, warn_on_extra_files:)
|
90
|
+
gem_loaders_by_root_file[root_file] ||= GemLoader.__new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
|
91
91
|
end
|
92
92
|
|
93
93
|
# @private
|
data/lib/zeitwerk/version.rb
CHANGED
data/lib/zeitwerk.rb
CHANGED
@@ -9,6 +9,7 @@ module Zeitwerk
|
|
9
9
|
require_relative "zeitwerk/explicit_namespace"
|
10
10
|
require_relative "zeitwerk/inflector"
|
11
11
|
require_relative "zeitwerk/gem_inflector"
|
12
|
+
require_relative "zeitwerk/null_inflector"
|
12
13
|
require_relative "zeitwerk/kernel"
|
13
14
|
require_relative "zeitwerk/error"
|
14
15
|
require_relative "zeitwerk/version"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zeitwerk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.
|
4
|
+
version: 2.6.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Noria
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Zeitwerk implements constant autoloading with Ruby semantics. Each gem
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/zeitwerk/loader/config.rb
|
36
36
|
- lib/zeitwerk/loader/eager_load.rb
|
37
37
|
- lib/zeitwerk/loader/helpers.rb
|
38
|
+
- lib/zeitwerk/null_inflector.rb
|
38
39
|
- lib/zeitwerk/real_mod_name.rb
|
39
40
|
- lib/zeitwerk/registry.rb
|
40
41
|
- lib/zeitwerk/version.rb
|
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
- !ruby/object:Gem::Version
|
62
63
|
version: '0'
|
63
64
|
requirements: []
|
64
|
-
rubygems_version: 3.
|
65
|
+
rubygems_version: 3.5.5
|
65
66
|
signing_key:
|
66
67
|
specification_version: 4
|
67
68
|
summary: Efficient and thread-safe constant autoloader
|