zeitwerk 2.6.6 → 2.7.0

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: 220865ab50f0336d05c8b907a08a94a8bf92843ba688c1a9686163a190b0754e
4
- data.tar.gz: a9d62662351c60a3b264a68726a84c41fc4b6f92eaae855fb4887e3eb4cf14a1
3
+ metadata.gz: 79a3e282ee7602f7d40c75c1299b24de4de890ee90a22cd37ed9c7a660936074
4
+ data.tar.gz: bd60115bc776137770e16ee527e4a07e815af547d1d5de851039e0187d50eff8
5
5
  SHA512:
6
- metadata.gz: b802eaabb27e6268eafa8d35d4402819551203c5fd2a1692d9a9bae65075bf668f5aecfaf3fa6c763987796b60fd5bde4704aebda4a8bfa1c74a526c27264ab5
7
- data.tar.gz: c4a53a60b49e5cb83aee148271b26fee8de15fd072327e52363438b0531d9953961305375ec727cc3a0ff2c021254eb081056350165922bd9f6f8b1e1b3e96f3
6
+ metadata.gz: ebdab4575a6e35591d1603ed11dc31f77eeffb15c8270f54719eb0850f3641efa968bebd8f02c32330ca768b8211d9aeb972fd41e0efac710faa023a0d62a841
7
+ data.tar.gz: 6d58d0256f35d6ac6445cba6619943aa4b3974905eb28588284f14aadd27304b1f11fc46d4f0905074c8888e52eabc03e312054930b702c875856d6c0a6a0694
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/workflow/status/fxn/zeitwerk/CI?event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions?query=event%3Apush)
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,16 +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
- - [debug.rb](#debugrb)
62
- - [Byebug](#byebug)
63
- - [Break](#break)
64
65
  - [Pronunciation](#pronunciation)
65
66
  - [Supported Ruby versions](#supported-ruby-versions)
66
67
  - [Testing](#testing)
@@ -78,15 +79,15 @@
78
79
 
79
80
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
80
81
 
81
- Given a [conventional file structure](#file-structure), Zeitwerk is 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.
82
83
 
83
- 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.
84
85
 
85
- 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.
86
87
 
87
- 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`.
88
89
 
89
- 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.
90
91
 
91
92
  <a id="markdown-synopsis" name="synopsis"></a>
92
93
  ## Synopsis
@@ -146,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
146
147
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
147
148
  ### The idea: File paths match constant paths
148
149
 
149
- 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:
150
151
 
151
152
  ```
152
153
  lib/my_gem.rb -> MyGem
@@ -155,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
155
156
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
156
157
  ```
157
158
 
158
- You can tune 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.
159
160
 
160
161
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
161
162
  ### Inner simple constants
@@ -176,7 +177,7 @@ class HttpCrawler
176
177
  end
177
178
  ```
178
179
 
179
- The first example needs a custom [inflection](https://github.com/fxn/zeitwerk#inflection) rule:
180
+ The first example needs a custom [inflection](#inflection) rule:
180
181
 
181
182
  ```ruby
182
183
  loader.inflector.inflect("max_retries" => "MAX_RETRIES")
@@ -213,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
213
214
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
214
215
  #### Custom root namespaces
215
216
 
216
- 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.
217
218
 
218
219
  For example, given:
219
220
 
@@ -229,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
229
230
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
230
231
  ```
231
232
 
232
- Please, 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.
233
234
 
234
235
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
235
236
  #### Nested root directories
236
237
 
237
- Root directories 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.
238
239
 
239
- 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:
240
241
 
241
242
  ```
242
243
  app/models/concerns/geolocatable.rb
@@ -247,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
247
248
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
248
249
  ### Implicit namespaces
249
250
 
250
- If a namespace 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.
251
252
 
252
- For example, if a project has an `admin` directory:
253
+ For instance, suppose a project includes an `admin` directory:
253
254
 
254
255
  ```
255
256
  app/controllers/admin/users_controller.rb -> Admin::UsersController
@@ -257,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
257
258
 
258
259
  and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
259
260
 
260
- 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.
261
262
 
262
263
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
263
264
  ### Explicit namespaces
@@ -372,7 +373,7 @@ require "zeitwerk"
372
373
  loader = Zeitwerk::Loader.new
373
374
  loader.tag = File.basename(__FILE__, ".rb")
374
375
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
375
- loader.push_dir(__dir__)
376
+ loader.push_dir(File.dirname(__FILE__))
376
377
  ```
377
378
 
378
379
  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:
@@ -391,6 +392,12 @@ module MyGem
391
392
  end
392
393
  ```
393
394
 
395
+ Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`, which is the standard way to load a gem. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work.
396
+
397
+ `Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
398
+
399
+ If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](#reopening-third-party-namespaces) down below.
400
+
394
401
  Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
395
402
 
396
403
  For example, if the gem has Rails generators under `lib/generators`, by convention that directory defines a `Generators` Ruby module. If `generators` is just a container for non-autoloadable code and templates, not acting as a project namespace, you need to setup things accordingly.
@@ -407,9 +414,64 @@ Otherwise, there's a flag to say the extra stuff is OK:
407
414
  Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
408
415
  ```
409
416
 
410
- This method is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
417
+ <a id="markdown-for_gem_extension" name="for_gem_extension"></a>
418
+ #### for_gem_extension
419
+
420
+ Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/):
421
+
422
+ * The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
423
+ * The namespace should be `Net::HTTP::NicheFeature`.
424
+ * The entry point should be `lib/net/http/niche_feature.rb`.
425
+ * Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.
426
+
427
+ The top-level file mentioned in the last point is optional. In particular, from
428
+
429
+ ```ruby
430
+ gem "net-http-niche_feature"
431
+ ```
432
+
433
+ if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
434
+
435
+ Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
436
+
437
+ The structure of the gem would be like this:
438
+
439
+ ```ruby
440
+ # lib/net-http-niche_feature.rb (optional)
441
+
442
+ # For technical reasons, this cannot be require_relative.
443
+ require "net/http/niche_feature"
444
+
445
+
446
+ # lib/net/http/niche_feature.rb
447
+
448
+ require "net/http"
449
+ require "zeitwerk"
450
+
451
+ loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
452
+ loader.setup
453
+
454
+ module Net::HTTP::NicheFeature
455
+ # Since the setup has been performed, at this point we are already able
456
+ # to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
457
+ include MyMixin
458
+ end
459
+
460
+
461
+ # lib/net/http/niche_feature/version.rb
462
+
463
+ module Net::HTTP::NicheFeature
464
+ VERSION = "1.0.0"
465
+ end
466
+ ```
467
+
468
+ `Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
469
+
470
+ If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
411
471
 
412
- If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](https://github.com/fxn/zeitwerk#reopening-third-party-namespaces) down below.
472
+ Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file.
473
+
474
+ `Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
413
475
 
414
476
  <a id="markdown-autoloading" name="autoloading"></a>
415
477
  ### Autoloading
@@ -445,7 +507,7 @@ loader.eager_load
445
507
 
446
508
  That skips [ignored files and directories](#ignoring-parts-of-the-project).
447
509
 
448
- In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](https://github.com/fxn/zeitwerk#synopsis).
510
+ In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
449
511
 
450
512
  Eager loading is synchronized and idempotent.
451
513
 
@@ -486,7 +548,7 @@ This is useful when the loader is not eager loading the entire project, but you
486
548
 
487
549
  Both strings and `Pathname` objects are supported as arguments. If the argument is not a directory managed by the receiver, the method raises `Zeitwerk::Error`.
488
550
 
489
- [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
551
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
490
552
 
491
553
  `Zeitwerk::Loader#eager_load_dir` is idempotent, but compatible with reloading. If you eager load a directory and then reload, eager loading that directory will load its (current) contents again.
492
554
 
@@ -517,13 +579,13 @@ root_dir2/my_app/routes
517
579
  root_dir3/my_app/routes
518
580
  ```
519
581
 
520
- where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
582
+ where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
521
583
 
522
- There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
584
+ There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
523
585
 
524
586
  This method is flexible about what it accepts. Its semantics have to be interpreted as: "_If_ you manage this namespace, or part of this namespace, please eager load what you got". In particular, if the receiver does not manage the namespace, it will simply do nothing, this is not an error condition.
525
587
 
526
- [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
588
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
527
589
 
528
590
  `Zeitwerk::Loader#eager_load_namespace` is idempotent, but compatible with reloading. If you eager load a namespace and then reload, eager loading that namespace will load its (current) descendants again.
529
591
 
@@ -576,7 +638,7 @@ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
576
638
 
577
639
  This is useful when the loader is not eager loading the entire project, but you still need an individual file to be loaded for things to function properly.
578
640
 
579
- Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](https://github.com/fxn/zeitwerk#shadowed-files), or is not managed by the receiver.
641
+ Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](#shadowed-files), or is not managed by the receiver.
580
642
 
581
643
  `Zeitwerk::Loader#load_file` is idempotent, but compatible with reloading. If you load a file and then reload, a new call will load its (current) contents again.
582
644
 
@@ -674,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
674
736
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
675
737
  ```
676
738
 
739
+ Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
740
+
741
+ ```ruby
742
+ loader.inflector.inflect("xml" => "XML")
743
+ ```
744
+
745
+ then the following constants are expected:
746
+
747
+ ```
748
+ xml.rb -> XML
749
+ foo/xml -> Foo::XML
750
+ foo/bar/xml.rb -> Foo::Bar::XML
751
+ ```
752
+
753
+ As you see, any directory whose basename is exactly `xml`, and any file whose basename is exactly `xml.rb` are expected to define the constant `XML` in the corresponding namespace. On the other hand, partial matches are ignored. For example, `xml_parser.rb` would be inflected as `XmlParser` because `xml_parser` is not equal to `xml`. You'd need an additional override:
754
+
755
+ ```ruby
756
+ loader.inflector.inflect(
757
+ "xml" => "XML",
758
+ "xml_parser" => "XMLParser"
759
+ )
760
+ ```
761
+
762
+ If you need more flexibility, you can define a custom inflector, as explained down below.
763
+
677
764
  Overrides need to be configured before calling `setup`.
678
765
 
679
- 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.
766
+ The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
680
767
 
681
768
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
682
769
  #### Zeitwerk::GemInflector
@@ -687,6 +774,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
687
774
 
688
775
  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.
689
776
 
777
+ <a id="markdown-zeitwerknullinflector" name="zeitwerknullinflector"></a>
778
+ #### Zeitwerk::NullInflector
779
+
780
+ This is an experimental inflector that simply returns its input unchanged.
781
+
782
+ ```ruby
783
+ loader.inflector = Zeitwerk::NullInflector.new
784
+ ```
785
+
786
+ In a project using this inflector, the names of files and directories are equal to the constants they define:
787
+
788
+ ```
789
+ User.rb -> User
790
+ HTMLParser.rb -> HTMLParser
791
+ Admin/Role.rb -> Admin::Role
792
+ ```
793
+
794
+ 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.
795
+
796
+ 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!
797
+
798
+ 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.
799
+
800
+ 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`.
801
+
690
802
  <a id="markdown-custom-inflector" name="custom-inflector"></a>
691
803
  #### Custom inflector
692
804
 
@@ -919,7 +1031,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
919
1031
 
920
1032
  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.
921
1033
 
922
- 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.
1034
+ 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.
923
1035
 
924
1036
  <a id="markdown-loader-tag" name="loader-tag"></a>
925
1037
  #### Loader tag
@@ -945,12 +1057,14 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
945
1057
  <a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
946
1058
  ### Ignoring parts of the project
947
1059
 
948
- Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
1060
+ Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have the extension ".rb".
949
1061
 
950
1062
  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.
951
1063
 
952
1064
  You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
953
1065
 
1066
+ 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.
1067
+
954
1068
  Let's see some use cases.
955
1069
 
956
1070
  <a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
@@ -1062,35 +1176,6 @@ file #{file} is ignored because #{constant_path} is already defined
1062
1176
 
1063
1177
  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).
1064
1178
 
1065
- <a id="markdown-edge-cases" name="edge-cases"></a>
1066
- ### Edge cases
1067
-
1068
- [Explicit namespaces](#explicit-namespaces) like `Trip` here:
1069
-
1070
- ```ruby
1071
- # trip.rb
1072
- class Trip
1073
- include Geolocation
1074
- end
1075
-
1076
- # trip/geolocation.rb
1077
- module Trip::Geolocation
1078
- ...
1079
- end
1080
- ```
1081
-
1082
- have to be defined with the `class`/`module` keywords, as in the example above.
1083
-
1084
- For technical reasons, raw constant assignment is not supported:
1085
-
1086
- ```ruby
1087
- # trip.rb
1088
- Trip = Class.new { ... } # NOT SUPPORTED
1089
- Trip = Struct.new { ... } # NOT SUPPORTED
1090
- ```
1091
-
1092
- This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
1093
-
1094
1179
  <a id="markdown-beware-of-circular-dependencies" name="beware-of-circular-dependencies"></a>
1095
1180
  ### Beware of circular dependencies
1096
1181
 
@@ -1152,6 +1237,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
1152
1237
  <a id="markdown-introspection" name="introspection"></a>
1153
1238
  ### Introspection
1154
1239
 
1240
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1241
+ #### `Zeitwerk::Loader#dirs`
1242
+
1155
1243
  The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1156
1244
 
1157
1245
  ```ruby
@@ -1169,8 +1257,96 @@ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
1169
1257
  loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
1170
1258
  ```
1171
1259
 
1260
+ By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
1261
+
1172
1262
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1173
1263
 
1264
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1265
+ #### `Zeitwerk::Loader#cpath_expected_at`
1266
+
1267
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1268
+
1269
+ Some examples, assuming that `app/models` is a root directory:
1270
+
1271
+ ```ruby
1272
+ loader.cpath_expected_at("app/models") # => "Object"
1273
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1274
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1275
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1276
+ ```
1277
+
1278
+ If `collapsed` is a collapsed directory:
1279
+
1280
+ ```ruby
1281
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1282
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1283
+ loader.cpath_expected_at("a/b") # => "A::B"
1284
+ ```
1285
+
1286
+ 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.
1287
+
1288
+ `Zeitwerk::Error` is raised if the given path does not exist:
1289
+
1290
+ ```ruby
1291
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1292
+ ```
1293
+
1294
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1295
+
1296
+ ```ruby
1297
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1298
+ ```
1299
+
1300
+ 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.
1301
+
1302
+ `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.
1303
+
1304
+ <a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
1305
+ #### `Zeitwerk::Loader#all_expected_cpaths`
1306
+
1307
+ 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.
1308
+
1309
+ 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.
1310
+
1311
+ For example, if `lib` is the root directory of a gem with the following contents:
1312
+
1313
+ ```
1314
+ lib/.DS_Store
1315
+ lib/my_gem.rb
1316
+ lib/my_gem/version.rb
1317
+ lib/my_gem/ignored.rb
1318
+ lib/my_gem/drivers/unix.rb
1319
+ lib/my_gem/drivers/windows.rb
1320
+ lib/my_gem/collapsed/foo.rb
1321
+ lib/tasks/my_gem.rake
1322
+ ```
1323
+
1324
+ `Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
1325
+
1326
+ ```ruby
1327
+ {
1328
+ "/.../lib" => "Object",
1329
+ "/.../lib/my_gem.rb" => "MyGem",
1330
+ "/.../lib/my_gem" => "MyGem",
1331
+ "/.../lib/my_gem/version.rb" => "MyGem::VERSION",
1332
+ "/.../lib/my_gem/drivers" => "MyGem::Drivers",
1333
+ "/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
1334
+ "/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
1335
+ "/.../lib/my_gem/collapsed" => "MyGem"
1336
+ "/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
1337
+ }
1338
+ ```
1339
+
1340
+ 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).
1341
+
1342
+ 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".
1343
+
1344
+ Directory paths do not have trailing slashes.
1345
+
1346
+ The order of the hash entries is undefined.
1347
+
1348
+ 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.
1349
+
1174
1350
  <a id="markdown-encodings" name="encodings"></a>
1175
1351
  ### Encodings
1176
1352
 
@@ -1192,32 +1368,12 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1192
1368
 
1193
1369
  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.
1194
1370
 
1195
- 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.
1371
+ 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.
1196
1372
 
1197
1373
  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.
1198
1374
 
1199
1375
  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.
1200
1376
 
1201
- <a id="markdown-debuggers" name="debuggers"></a>
1202
- ### Debuggers
1203
-
1204
- <a id="markdown-debugrb" name="debugrb"></a>
1205
- #### debug.rb
1206
-
1207
- The new [debug.rb](https://github.com/ruby/debug) gem and Zeitwerk are mostly compatible. This is the new debugger that is going to ship with Ruby 3.1.
1208
-
1209
- There's one exception, though: Due to a technical limitation of tracepoints, explicit namespaces are not autoloaded while expressions are evaluated in the REPL. See [ruby/debug#408](https://github.com/ruby/debug/issues/408).
1210
-
1211
- <a id="markdown-byebug" name="byebug"></a>
1212
- #### Byebug
1213
-
1214
- Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) have a similar edge incompatibility.
1215
-
1216
- <a id="markdown-break" name="break"></a>
1217
- #### Break
1218
-
1219
- Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger.
1220
-
1221
1377
  <a id="markdown-pronunciation" name="pronunciation"></a>
1222
1378
  ## Pronunciation
1223
1379
 
@@ -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.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
@@ -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.on_file_autoloaded(abspath)
38
+ loader.__on_file_autoloaded(abspath)
43
39
  end
44
40
  end
45
41
  required
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zeitwerk::ConstAdded
4
+ def const_added(cname)
5
+ if loader = Zeitwerk::ExplicitNamespace.__loader_for(self, cname)
6
+ loader.on_namespace_loaded(const_get(cname, false))
7
+ end
8
+ super
9
+ end
10
+
11
+ Module.prepend(self)
12
+ end