zeitwerk 2.6.7 → 2.7.3

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: e6c7bf12c3a33195c57541b840ca6d7dbc21b19e0d7b91d8933ad37aa88b325d
4
- data.tar.gz: 4b8dd86417dd633b78c02eada9ed6956e8e708397c988237aaddecb9644ba469
3
+ metadata.gz: 1500c4f54c6ac7e64eef74c8702682764fa845533a5b16c67a48ee11781ef3e0
4
+ data.tar.gz: f7201576b8b59ab3786cd4bc6fd58f3cd3afa3d8dcc1e86477121000f06ac50a
5
5
  SHA512:
6
- metadata.gz: 4f0bc60d9914a9e815aed501a030f7e9bde7927f750f3836f96ac86a52c0699f4ca69456bd381845428f6b09adccaa085443f39bb0fdaa651437742e21d10af2
7
- data.tar.gz: 3ba55f7bb24725d156cc4043697bfcd6092e2893f1aa71296965c82ae9ed66b79c30f853b535693b6b884d7d3d26cf1b122545e395ba11c92f65552e14116248
6
+ metadata.gz: ab113d90c9a42ec5c75c6ebe68ab5a04395f1b33228de42de6952f2e673a329f30de85477977ec11d582df82f40c4dc5c08e8ba2305f46a6d07ac4e88c564887
7
+ data.tar.gz: 73fb9dc78a9a7148119b306f93a513f841e4888d421fbf178963bbfa1368a8c7c66cd8661a28e8d95d14f51dab16fb4ff886f23183ec13a74847c45c1850f30d
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
 
@@ -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)
@@ -51,13 +54,14 @@
51
54
  - [Use case: The adapter pattern](#use-case-the-adapter-pattern)
52
55
  - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
53
56
  - [Shadowed files](#shadowed-files)
54
- - [Edge cases](#edge-cases)
55
57
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
56
58
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
57
59
  - [Introspection](#introspection)
60
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
61
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
62
+ - [`Zeitwerk::Loader#all_expected_cpaths`](#zeitwerkloaderall_expected_cpaths)
58
63
  - [Encodings](#encodings)
59
64
  - [Rules of thumb](#rules-of-thumb)
60
- - [Debuggers](#debuggers)
61
65
  - [Pronunciation](#pronunciation)
62
66
  - [Supported Ruby versions](#supported-ruby-versions)
63
67
  - [Testing](#testing)
@@ -75,15 +79,15 @@
75
79
 
76
80
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
77
81
 
78
- 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.
82
+ Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
79
83
 
80
- 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.
84
+ Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
81
85
 
82
- 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.
86
+ The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
83
87
 
84
- 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`.
88
+ Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
85
89
 
86
- Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
90
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
87
91
 
88
92
  <a id="markdown-synopsis" name="synopsis"></a>
89
93
  ## Synopsis
@@ -143,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
143
147
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
144
148
  ### The idea: File paths match constant paths
145
149
 
146
- To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
150
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
147
151
 
148
152
  ```
149
153
  lib/my_gem.rb -> MyGem
@@ -152,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
152
156
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
153
157
  ```
154
158
 
155
- 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.
159
+ You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
156
160
 
157
161
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
158
162
  ### Inner simple constants
@@ -210,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
210
214
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
211
215
  #### Custom root namespaces
212
216
 
213
- 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.
217
+ Although `Object` is the most common root namespace, you have the flexibility to associate a different one with a specific root directory. The `push_dir` method accepts a non-anonymous class or module object as the optional `namespace` keyword argument.
214
218
 
215
219
  For example, given:
216
220
 
@@ -226,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
226
230
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
227
231
  ```
228
232
 
229
- 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.
233
+ Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
230
234
 
231
235
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
232
236
  #### Nested root directories
233
237
 
234
- 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.
238
+ Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
235
239
 
236
- 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:
240
+ Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
237
241
 
238
242
  ```
239
243
  app/models/concerns/geolocatable.rb
@@ -244,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
244
248
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
245
249
  ### Implicit namespaces
246
250
 
247
- 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.
251
+ If a namespace consists only of a simple module without any code, there is no need to explicitly define it in a separate file. Zeitwerk automatically creates modules on your behalf for directories without a corresponding Ruby file.
248
252
 
249
- For example, if a project has an `admin` directory:
253
+ For instance, suppose a project includes an `admin` directory:
250
254
 
251
255
  ```
252
256
  app/controllers/admin/users_controller.rb -> Admin::UsersController
@@ -254,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
254
258
 
255
259
  and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
256
260
 
257
- 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.
261
+ To trigger this behavior, the directory must contain non-ignored Ruby files with the ".rb" extension, either directly or recursively. Otherwise, the directory is ignored. This condition is reevaluated during reloads.
258
262
 
259
263
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
260
264
  ### Explicit namespaces
@@ -277,6 +281,8 @@ class Hotel < ApplicationRecord
277
281
  end
278
282
  ```
279
283
 
284
+ 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.
285
+
280
286
  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.
281
287
 
282
288
  <a id="markdown-collapsing-directories" name="collapsing-directories"></a>
@@ -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(__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 `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
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
- 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.
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_. That's a technique used to be able to descend into subdirectories on demand, avoiding that way unnecessary tree walks.
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 explictly if you want it ignored too.
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
 
@@ -1063,36 +1178,6 @@ file #{file} is ignored because #{constant_path} is already defined
1063
1178
 
1064
1179
  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).
1065
1180
 
1066
- <a id="markdown-edge-cases" name="edge-cases"></a>
1067
- ### Edge cases
1068
-
1069
- [Explicit namespaces](#explicit-namespaces) like `Trip` here:
1070
-
1071
- ```ruby
1072
- # trip.rb
1073
- class Trip
1074
- include Geolocation
1075
- end
1076
-
1077
- # trip/geolocation.rb
1078
- module Trip::Geolocation
1079
- ...
1080
- end
1081
- ```
1082
-
1083
- have to be defined with the `class`/`module` keywords, as in the example above.
1084
-
1085
- For technical reasons, raw constant assignment is not supported:
1086
-
1087
- ```ruby
1088
- # trip.rb
1089
- Trip = Class { ...} # NOT SUPPORTED
1090
- Trip = Struct.new { ... } # NOT SUPPORTED
1091
- Trip = Data.define { ... } # NOT SUPPORTED
1092
- ```
1093
-
1094
- This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
1095
-
1096
1181
  <a id="markdown-beware-of-circular-dependencies" name="beware-of-circular-dependencies"></a>
1097
1182
  ### Beware of circular dependencies
1098
1183
 
@@ -1154,6 +1239,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
1154
1239
  <a id="markdown-introspection" name="introspection"></a>
1155
1240
  ### Introspection
1156
1241
 
1242
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1243
+ #### `Zeitwerk::Loader#dirs`
1244
+
1157
1245
  The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1158
1246
 
1159
1247
  ```ruby
@@ -1175,6 +1263,92 @@ By default, ignored root directories are filtered out. If you want them included
1175
1263
 
1176
1264
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1177
1265
 
1266
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1267
+ #### `Zeitwerk::Loader#cpath_expected_at`
1268
+
1269
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1270
+
1271
+ Some examples, assuming that `app/models` is a root directory:
1272
+
1273
+ ```ruby
1274
+ loader.cpath_expected_at("app/models") # => "Object"
1275
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1276
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1277
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1278
+ ```
1279
+
1280
+ If `collapsed` is a collapsed directory:
1281
+
1282
+ ```ruby
1283
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1284
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1285
+ loader.cpath_expected_at("a/b") # => "A::B"
1286
+ ```
1287
+
1288
+ 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.
1289
+
1290
+ `Zeitwerk::Error` is raised if the given path does not exist:
1291
+
1292
+ ```ruby
1293
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1294
+ ```
1295
+
1296
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1297
+
1298
+ ```ruby
1299
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1300
+ ```
1301
+
1302
+ 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.
1303
+
1304
+ `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.
1305
+
1306
+ <a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
1307
+ #### `Zeitwerk::Loader#all_expected_cpaths`
1308
+
1309
+ 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.
1310
+
1311
+ 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.
1312
+
1313
+ For example, if `lib` is the root directory of a gem with the following contents:
1314
+
1315
+ ```
1316
+ lib/.DS_Store
1317
+ lib/my_gem.rb
1318
+ lib/my_gem/version.rb
1319
+ lib/my_gem/ignored.rb
1320
+ lib/my_gem/drivers/unix.rb
1321
+ lib/my_gem/drivers/windows.rb
1322
+ lib/my_gem/collapsed/foo.rb
1323
+ lib/tasks/my_gem.rake
1324
+ ```
1325
+
1326
+ `Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
1327
+
1328
+ ```ruby
1329
+ {
1330
+ "/.../lib" => "Object",
1331
+ "/.../lib/my_gem.rb" => "MyGem",
1332
+ "/.../lib/my_gem" => "MyGem",
1333
+ "/.../lib/my_gem/version.rb" => "MyGem::VERSION",
1334
+ "/.../lib/my_gem/drivers" => "MyGem::Drivers",
1335
+ "/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
1336
+ "/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
1337
+ "/.../lib/my_gem/collapsed" => "MyGem"
1338
+ "/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
1339
+ }
1340
+ ```
1341
+
1342
+ 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).
1343
+
1344
+ 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".
1345
+
1346
+ Directory paths do not have trailing slashes.
1347
+
1348
+ The order of the hash entries is undefined.
1349
+
1350
+ 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.
1351
+
1178
1352
  <a id="markdown-encodings" name="encodings"></a>
1179
1353
  ### Encodings
1180
1354
 
@@ -1196,19 +1370,12 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1196
1370
 
1197
1371
  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
1372
 
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 define. This is often accomplished just loading the dependency.
1373
+ 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
1374
 
1201
1375
  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
1376
 
1203
1377
  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.
1204
1378
 
1205
- <a id="markdown-debuggers" name="debuggers"></a>
1206
- ### Debuggers
1207
-
1208
- Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
1209
-
1210
- [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).
1211
-
1212
1379
  <a id="markdown-pronunciation" name="pronunciation"></a>
1213
1380
  ## Pronunciation
1214
1381
 
@@ -1217,9 +1384,12 @@ Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](h
1217
1384
  <a id="markdown-supported-ruby-versions" name="supported-ruby-versions"></a>
1218
1385
  ## Supported Ruby versions
1219
1386
 
1220
- Zeitwerk works with CRuby 2.5 and above.
1387
+ Starting with version 2.7, Zeitwerk requires Ruby 3.2 or newer.
1221
1388
 
1222
- 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.)
1389
+ Zeitwerk 2.7 requires TruffleRuby 24.1.2+ due to https://github.com/oracle/truffleruby/issues/3683.
1390
+ Alternatively, TruffleRuby users can use a `< 2.7` version constraint for the `zeitwerk` gem.
1391
+ As of this writing, [autoloading is not fully thread-safe yet on TruffleRuby](https://github.com/oracle/truffleruby/issues/2431).
1392
+ If your program is multi-threaded, you need to eager load before threads are created.
1223
1393
 
1224
1394
  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.)
1225
1395
 
@@ -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