zeitwerk 2.6.8 → 2.7.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 418f16f6224359c716a990f72ae916c980a3f388b09ab018c0972b8558530620
4
- data.tar.gz: '04847c6c8f8f894fe3c2b3c39d175b5854b583d3a02ef5cca27a53cb1b8dafd4'
3
+ metadata.gz: '06683acd85eeb772fdb9757ae20bfb05e42ec465acfff3edcada546a399d9131'
4
+ data.tar.gz: 80e3cdede4e05607ef5c58c60a3f7824c6750dd1d19d8786352356025bde1d65
5
5
  SHA512:
6
- metadata.gz: f86272e84721cf9b8b1e07182b1b60cfe732ef2c266dbe076225d32af52fc9a30c278a14651ddbb3d435636646dd23c948d3fe0f00ed9b8393abdeb34dd40028
7
- data.tar.gz: 562738b53779310e899c2065c7046844d27daabb5f354972e4c9b65bec3e5c0cdade92d19ed815fe7b0dc01538603daa3e4dd2f0049fe9a7a2e74313c0f040f6
6
+ metadata.gz: b37beb740e461d73a1bd1ae21ec4f2bf63bb5022f3a193e2718bb8e5d4f172bf9c1222ff2a33acc47fb390a34e50b8b4b65b5b762dc5f71c1971b25720081437
7
+ data.tar.gz: 02c85e17a3ab801c925157239386c9a5a105cc1b8b9d735f64c27c89d3a0371613f2cb3f27d49647d98bf3ad8220d3a3538c2bdae215a5890145136374eb1d3e
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%3main)
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
 
@@ -39,6 +40,7 @@
39
40
  - [Inflection](#inflection)
40
41
  - [Zeitwerk::Inflector](#zeitwerkinflector)
41
42
  - [Zeitwerk::GemInflector](#zeitwerkgeminflector)
43
+ - [Zeitwerk::NullInflector](#zeitwerknullinflector)
42
44
  - [Custom inflector](#custom-inflector)
43
45
  - [Callbacks](#callbacks)
44
46
  - [The on_setup callback](#the-on_setup-callback)
@@ -52,13 +54,15 @@
52
54
  - [Use case: The adapter pattern](#use-case-the-adapter-pattern)
53
55
  - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
54
56
  - [Shadowed files](#shadowed-files)
55
- - [Edge cases](#edge-cases)
56
57
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
57
58
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
58
59
  - [Introspection](#introspection)
60
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
61
+ - [Autoloaded Constants](#autoloaded-constants)
62
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
63
+ - [`Zeitwerk::Loader#all_expected_cpaths`](#zeitwerkloaderall_expected_cpaths)
59
64
  - [Encodings](#encodings)
60
65
  - [Rules of thumb](#rules-of-thumb)
61
- - [Debuggers](#debuggers)
62
66
  - [Pronunciation](#pronunciation)
63
67
  - [Supported Ruby versions](#supported-ruby-versions)
64
68
  - [Testing](#testing)
@@ -76,15 +80,15 @@
76
80
 
77
81
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
78
82
 
79
- Given a [conventional file structure](#file-structure), Zeitwerk is able to load 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, rather, you can streamline your programming knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and matches Ruby's semantics for constants.
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.
80
84
 
81
- Zeitwerk is also able to reload code, which may be handy while developing web applications. Coordination is needed to reload in a thread-safe manner. The documentation below explains how to do this.
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.
82
86
 
83
- The gem is designed so that any project, gem dependency, application, etc. can have their own independent loader, coexisting in the same process, managing their own project trees, and independent of each other. Each loader has its own configuration, inflector, and optional logger.
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.
84
88
 
85
- Internally, Zeitwerk issues `require` calls exclusively using absolute file names, so there are no costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk do not even need to be in `$LOAD_PATH`.
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`.
86
90
 
87
- Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
91
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
88
92
 
89
93
  <a id="markdown-synopsis" name="synopsis"></a>
90
94
  ## Synopsis
@@ -144,7 +148,7 @@ Zeitwerk::Loader.eager_load_all
144
148
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
145
149
  ### The idea: File paths match constant paths
146
150
 
147
- To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
151
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
148
152
 
149
153
  ```
150
154
  lib/my_gem.rb -> MyGem
@@ -153,7 +157,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
153
157
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
154
158
  ```
155
159
 
156
- You can tune that a bit by [collapsing directories](#collapsing-directories), or by [ignoring parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
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.
157
161
 
158
162
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
159
163
  ### Inner simple constants
@@ -198,7 +202,7 @@ For example, given
198
202
 
199
203
  ```ruby
200
204
  loader.push_dir("#{__dir__}/models")
201
- loader.push_dir("#{__dir__}/serializers"))
205
+ loader.push_dir("#{__dir__}/serializers")
202
206
  ```
203
207
 
204
208
  these are the expected classes and modules being defined by these files:
@@ -211,7 +215,7 @@ serializers/user_serializer.rb -> UserSerializer
211
215
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
212
216
  #### Custom root namespaces
213
217
 
214
- While `Object` is by far the most common root namespace, you can associate a different one to a particular root directory. The method `push_dir` accepts a non-anonymous class or module object in the optional `namespace` keyword argument.
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.
215
219
 
216
220
  For example, given:
217
221
 
@@ -227,14 +231,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
227
231
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
228
232
  ```
229
233
 
230
- Please, note that the given root namespace must be non-reloadable, though autoloaded constants in that namespace can be. That is, if you associate `app/api` with an existing `Api` module, that module should not be reloadable. However, if the project defines and autoloads the class `Api::Deliveries`, that one can be reloaded.
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.
231
235
 
232
236
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
233
237
  #### Nested root directories
234
238
 
235
- Root directories should not be ideally nested, but Zeitwerk supports them because in Rails, for example, both `app/models` and `app/models/concerns` belong to the autoload paths.
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.
236
240
 
237
- Zeitwerk detects nested root directories, and treats them as roots only. In the example above, `concerns` is not considered to be a namespace below `app/models`. For example, the file:
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:
238
242
 
239
243
  ```
240
244
  app/models/concerns/geolocatable.rb
@@ -245,9 +249,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
245
249
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
246
250
  ### Implicit namespaces
247
251
 
248
- If a namespace is just a simple module with no code, you do not need to define it in a file: Directories without a matching Ruby file get modules created automatically on your behalf.
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.
249
253
 
250
- For example, if a project has an `admin` directory:
254
+ For instance, suppose a project includes an `admin` directory:
251
255
 
252
256
  ```
253
257
  app/controllers/admin/users_controller.rb -> Admin::UsersController
@@ -255,7 +259,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
255
259
 
256
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.
257
261
 
258
- For this to happen, the directory has to contain non-ignored Ruby files with extension `.rb`, directly or recursively, otherwise it is ignored. This condition is evaluated again on reloads.
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.
259
263
 
260
264
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
261
265
  ### Explicit namespaces
@@ -278,6 +282,8 @@ class Hotel < ApplicationRecord
278
282
  end
279
283
  ```
280
284
 
285
+ When autoloaded, Zeitwerk verifies the expected constant (`Hotel` in the example) stores a class or module object. If it doesn't, `Zeitwerk::Error` is raised.
286
+
281
287
  An explicit namespace must be managed by one single loader. Loaders that reopen namespaces owned by other projects are responsible for loading their constants before setup.
282
288
 
283
289
  <a id="markdown-collapsing-directories" name="collapsing-directories"></a>
@@ -370,7 +376,7 @@ require "zeitwerk"
370
376
  loader = Zeitwerk::Loader.new
371
377
  loader.tag = File.basename(__FILE__, ".rb")
372
378
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
373
- loader.push_dir(__dir__)
379
+ loader.push_dir(File.dirname(__FILE__))
374
380
  ```
375
381
 
376
382
  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:
@@ -576,7 +582,7 @@ root_dir2/my_app/routes
576
582
  root_dir3/my_app/routes
577
583
  ```
578
584
 
579
- where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
585
+ where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
580
586
 
581
587
  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.
582
588
 
@@ -733,9 +739,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
733
739
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
734
740
  ```
735
741
 
742
+ Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
743
+
744
+ ```ruby
745
+ loader.inflector.inflect("xml" => "XML")
746
+ ```
747
+
748
+ then the following constants are expected:
749
+
750
+ ```
751
+ xml.rb -> XML
752
+ foo/xml -> Foo::XML
753
+ foo/bar/xml.rb -> Foo::Bar::XML
754
+ ```
755
+
756
+ 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:
757
+
758
+ ```ruby
759
+ loader.inflector.inflect(
760
+ "xml" => "XML",
761
+ "xml_parser" => "XMLParser"
762
+ )
763
+ ```
764
+
765
+ If you need more flexibility, you can define a custom inflector, as explained down below.
766
+
736
767
  Overrides need to be configured before calling `setup`.
737
768
 
738
- 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.
769
+ 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.
739
770
 
740
771
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
741
772
  #### Zeitwerk::GemInflector
@@ -746,6 +777,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
746
777
 
747
778
  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.
748
779
 
780
+ <a id="markdown-zeitwerknullinflector" name="zeitwerknullinflector"></a>
781
+ #### Zeitwerk::NullInflector
782
+
783
+ This is an experimental inflector that simply returns its input unchanged.
784
+
785
+ ```ruby
786
+ loader.inflector = Zeitwerk::NullInflector.new
787
+ ```
788
+
789
+ In a project using this inflector, the names of files and directories are equal to the constants they define:
790
+
791
+ ```
792
+ User.rb -> User
793
+ HTMLParser.rb -> HTMLParser
794
+ Admin/Role.rb -> Admin::Role
795
+ ```
796
+
797
+ 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.
798
+
799
+ 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!
800
+
801
+ 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.
802
+
803
+ 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`.
804
+
749
805
  <a id="markdown-custom-inflector" name="custom-inflector"></a>
750
806
  #### Custom inflector
751
807
 
@@ -978,7 +1034,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
978
1034
 
979
1035
  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.
980
1036
 
981
- As a curiosity, if your project has namespaces you'll notice in the traces Zeitwerk sets autoloads for _directories_. That's a technique used to be able to descend into subdirectories on demand, avoiding that way unnecessary tree walks.
1037
+ 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.
982
1038
 
983
1039
  <a id="markdown-loader-tag" name="loader-tag"></a>
984
1040
  #### Loader tag
@@ -1004,13 +1060,13 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
1004
1060
  <a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
1005
1061
  ### Ignoring parts of the project
1006
1062
 
1007
- Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
1063
+ Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have the extension ".rb".
1008
1064
 
1009
1065
  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.
1010
1066
 
1011
1067
  You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
1012
1068
 
1013
- There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explictly if you want it ignored too.
1069
+ 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.
1014
1070
 
1015
1071
  Let's see some use cases.
1016
1072
 
@@ -1123,36 +1179,6 @@ file #{file} is ignored because #{constant_path} is already defined
1123
1179
 
1124
1180
  Shadowing only applies to Ruby files, namespace definition can be spread over multiple directories. And you can also reopen third-party namespaces if done [orderly](#reopening-third-party-namespaces).
1125
1181
 
1126
- <a id="markdown-edge-cases" name="edge-cases"></a>
1127
- ### Edge cases
1128
-
1129
- [Explicit namespaces](#explicit-namespaces) like `Trip` here:
1130
-
1131
- ```ruby
1132
- # trip.rb
1133
- class Trip
1134
- include Geolocation
1135
- end
1136
-
1137
- # trip/geolocation.rb
1138
- module Trip::Geolocation
1139
- ...
1140
- end
1141
- ```
1142
-
1143
- have to be defined with the `class`/`module` keywords, as in the example above.
1144
-
1145
- For technical reasons, raw constant assignment is not supported:
1146
-
1147
- ```ruby
1148
- # trip.rb
1149
- Trip = Class { ...} # NOT SUPPORTED
1150
- Trip = Struct.new { ... } # NOT SUPPORTED
1151
- Trip = Data.define { ... } # NOT SUPPORTED
1152
- ```
1153
-
1154
- This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
1155
-
1156
1182
  <a id="markdown-beware-of-circular-dependencies" name="beware-of-circular-dependencies"></a>
1157
1183
  ### Beware of circular dependencies
1158
1184
 
@@ -1214,6 +1240,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
1214
1240
  <a id="markdown-introspection" name="introspection"></a>
1215
1241
  ### Introspection
1216
1242
 
1243
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1244
+ #### `Zeitwerk::Loader#dirs`
1245
+
1217
1246
  The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1218
1247
 
1219
1248
  ```ruby
@@ -1235,6 +1264,104 @@ By default, ignored root directories are filtered out. If you want them included
1235
1264
 
1236
1265
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1237
1266
 
1267
+ <a id="markdown-autoloaded-constants" name="autoloaded-constants"></a>
1268
+ #### Autoloaded Constants
1269
+
1270
+ Zeitwerk does not keep track of autoloaded constants to minimize its memory footprint, but you can collect them with `on_load` if you will:
1271
+
1272
+ ```ruby
1273
+ autoloaded_cpaths = []
1274
+ loader.on_load do |cpath, _value, _abspath|
1275
+ autoloaded_cpaths << cpath
1276
+ end
1277
+ ```
1278
+
1279
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1280
+ #### `Zeitwerk::Loader#cpath_expected_at`
1281
+
1282
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1283
+
1284
+ Some examples, assuming that `app/models` is a root directory:
1285
+
1286
+ ```ruby
1287
+ loader.cpath_expected_at("app/models") # => "Object"
1288
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1289
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1290
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1291
+ ```
1292
+
1293
+ If `collapsed` is a collapsed directory:
1294
+
1295
+ ```ruby
1296
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1297
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1298
+ loader.cpath_expected_at("a/b") # => "A::B"
1299
+ ```
1300
+
1301
+ 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.
1302
+
1303
+ `Zeitwerk::Error` is raised if the given path does not exist:
1304
+
1305
+ ```ruby
1306
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1307
+ ```
1308
+
1309
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1310
+
1311
+ ```ruby
1312
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1313
+ ```
1314
+
1315
+ 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.
1316
+
1317
+ `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.
1318
+
1319
+ <a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
1320
+ #### `Zeitwerk::Loader#all_expected_cpaths`
1321
+
1322
+ 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.
1323
+
1324
+ 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.
1325
+
1326
+ For example, if `lib` is the root directory of a gem with the following contents:
1327
+
1328
+ ```
1329
+ lib/.DS_Store
1330
+ lib/my_gem.rb
1331
+ lib/my_gem/version.rb
1332
+ lib/my_gem/ignored.rb
1333
+ lib/my_gem/drivers/unix.rb
1334
+ lib/my_gem/drivers/windows.rb
1335
+ lib/my_gem/collapsed/foo.rb
1336
+ lib/tasks/my_gem.rake
1337
+ ```
1338
+
1339
+ `Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
1340
+
1341
+ ```ruby
1342
+ {
1343
+ "/.../lib" => "Object",
1344
+ "/.../lib/my_gem.rb" => "MyGem",
1345
+ "/.../lib/my_gem" => "MyGem",
1346
+ "/.../lib/my_gem/version.rb" => "MyGem::VERSION",
1347
+ "/.../lib/my_gem/drivers" => "MyGem::Drivers",
1348
+ "/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
1349
+ "/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
1350
+ "/.../lib/my_gem/collapsed" => "MyGem",
1351
+ "/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
1352
+ }
1353
+ ```
1354
+
1355
+ 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).
1356
+
1357
+ 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".
1358
+
1359
+ Directory paths do not have trailing slashes.
1360
+
1361
+ The order of the hash entries is undefined.
1362
+
1363
+ 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.
1364
+
1238
1365
  <a id="markdown-encodings" name="encodings"></a>
1239
1366
  ### Encodings
1240
1367
 
@@ -1256,19 +1383,12 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1256
1383
 
1257
1384
  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.
1258
1385
 
1259
- 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 define. This is often accomplished just loading the dependency.
1386
+ 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.
1260
1387
 
1261
1388
  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.
1262
1389
 
1263
1390
  6. In a given process, ideally, there should be at most one loader with reloading enabled. Technically, you can have more, but it may get tricky if one refers to constants managed by the other one. Do that only if you know what you are doing.
1264
1391
 
1265
- <a id="markdown-debuggers" name="debuggers"></a>
1266
- ### Debuggers
1267
-
1268
- Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
1269
-
1270
- [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).
1271
-
1272
1392
  <a id="markdown-pronunciation" name="pronunciation"></a>
1273
1393
  ## Pronunciation
1274
1394
 
@@ -1277,9 +1397,12 @@ Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](h
1277
1397
  <a id="markdown-supported-ruby-versions" name="supported-ruby-versions"></a>
1278
1398
  ## Supported Ruby versions
1279
1399
 
1280
- Zeitwerk works with CRuby 2.5 and above.
1400
+ Starting with version 2.7, Zeitwerk requires Ruby 3.2 or newer.
1281
1401
 
1282
- On TruffleRuby all is good except for thread-safety. Right now, in TruffleRuby `Module#autoload` does not block threads accessing a constant that is being autoloaded. CRuby prevents such access to avoid concurrent threads from seeing partial evaluations of the corresponding file. Zeitwerk inherits autoloading thread-safety from this property. This is not an issue if your project gets eager loaded, or if you lazy load in single-threaded environments. (See https://github.com/oracle/truffleruby/issues/2431.)
1402
+ Zeitwerk 2.7 requires TruffleRuby 24.1.2+ due to https://github.com/oracle/truffleruby/issues/3683.
1403
+ Alternatively, TruffleRuby users can use a `< 2.7` version constraint for the `zeitwerk` gem.
1404
+ As of this writing, [autoloading is not fully thread-safe yet on TruffleRuby](https://github.com/oracle/truffleruby/issues/2431).
1405
+ If your program is multi-threaded, you need to eager load before threads are created.
1283
1406
 
1284
1407
  JRuby 9.3.0.0 is almost there. As of this writing, the test suite of Zeitwerk passes on JRuby except for three tests. (See https://github.com/jruby/jruby/issues/6781.)
1285
1408
 
@@ -1298,6 +1421,12 @@ To run one particular suite, pass its file name as an argument:
1298
1421
  bin/test test/lib/zeitwerk/test_eager_load.rb
1299
1422
  ```
1300
1423
 
1424
+ That also accepts a line number:
1425
+
1426
+ ```
1427
+ bin/test test/lib/zeitwerk/test_eager_load.rb:52
1428
+ ```
1429
+
1301
1430
  Furthermore, the project has a development dependency on [`minitest-focus`](https://github.com/seattlerb/minitest-focus). To run an individual test mark it with `focus`:
1302
1431
 
1303
1432
  ```ruby
@@ -14,32 +14,28 @@ 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
24
20
  end
25
21
 
26
- # @sig (String) -> true | false
22
+ #: (String) -> bool
27
23
  def require(path)
28
- if loader = Zeitwerk::Registry.loader_for(path)
24
+ if loader = Zeitwerk::Registry.autoloads.registered?(path)
29
25
  if path.end_with?(".rb")
30
26
  required = zeitwerk_original_require(path)
31
- loader.on_file_autoloaded(path) if required
27
+ loader.__on_file_autoloaded(path) if required
32
28
  required
33
29
  else
34
- loader.on_dir_autoloaded(path)
30
+ loader.__on_dir_autoloaded(path)
35
31
  true
36
32
  end
37
33
  else
38
34
  required = zeitwerk_original_require(path)
39
35
  if required
40
36
  abspath = $LOADED_FEATURES.last
41
- if loader = Zeitwerk::Registry.loader_for(abspath)
42
- loader.on_file_autoloaded(abspath)
37
+ if loader = Zeitwerk::Registry.autoloads.registered?(abspath)
38
+ loader.__on_file_autoloaded(abspath)
43
39
  end
44
40
  end
45
41
  required
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zeitwerk::ConstAdded # :nodoc:
4
+ #: (Symbol) -> void
5
+ def const_added(cname)
6
+ if loader = Zeitwerk::Registry.explicit_namespaces.loader_for(self, cname)
7
+ namespace = const_get(cname, false)
8
+ cref = Zeitwerk::Cref.new(self, cname)
9
+
10
+ unless namespace.is_a?(Module)
11
+ raise Zeitwerk::Error, "#{cref} is expected to be a namespace, should be a class or module (got #{namespace.class})"
12
+ end
13
+
14
+ loader.__on_namespace_loaded(cref, namespace)
15
+ end
16
+ super
17
+ end
18
+
19
+ Module.prepend(self)
20
+ end