zeitwerk 2.5.4 → 2.6.18

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.
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,14 +25,22 @@
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)
32
+ - [Eager load directories](#eager-load-directories)
33
+ - [Eager load namespaces](#eager-load-namespaces)
34
+ - [Eager load namespaces shared by several loaders](#eager-load-namespaces-shared-by-several-loaders)
30
35
  - [Global eager load](#global-eager-load)
36
+ - [Loading individual files](#loading-individual-files)
31
37
  - [Reloading](#reloading)
38
+ - [Configuration and usage](#configuration-and-usage)
39
+ - [Thread-safety](#thread-safety)
32
40
  - [Inflection](#inflection)
33
41
  - [Zeitwerk::Inflector](#zeitwerkinflector)
34
42
  - [Zeitwerk::GemInflector](#zeitwerkgeminflector)
43
+ - [Zeitwerk::NullInflector](#zeitwerknullinflector)
35
44
  - [Custom inflector](#custom-inflector)
36
45
  - [Callbacks](#callbacks)
37
46
  - [The on_setup callback](#the-on_setup-callback)
@@ -44,20 +53,24 @@
44
53
  - [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
45
54
  - [Use case: The adapter pattern](#use-case-the-adapter-pattern)
46
55
  - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
56
+ - [Shadowed files](#shadowed-files)
47
57
  - [Edge cases](#edge-cases)
48
58
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
49
59
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
60
+ - [Introspection](#introspection)
61
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
62
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
63
+ - [`Zeitwerk::Loader#all_expected_cpaths`](#zeitwerkloaderall_expected_cpaths)
64
+ - [Encodings](#encodings)
50
65
  - [Rules of thumb](#rules-of-thumb)
51
66
  - [Debuggers](#debuggers)
52
- - [debug.rb](#debugrb)
53
- - [Byebug](#byebug)
54
- - [Break](#break)
55
67
  - [Pronunciation](#pronunciation)
56
68
  - [Supported Ruby versions](#supported-ruby-versions)
57
69
  - [Testing](#testing)
58
70
  - [Motivation](#motivation)
59
71
  - [Kernel#require is brittle](#kernelrequire-is-brittle)
60
72
  - [Rails autoloading was brittle](#rails-autoloading-was-brittle)
73
+ - [Awards](#awards)
61
74
  - [Thanks](#thanks)
62
75
  - [License](#license)
63
76
 
@@ -68,15 +81,15 @@
68
81
 
69
82
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
70
83
 
71
- 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.
84
+ Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
72
85
 
73
- 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.
86
+ Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
74
87
 
75
- 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.
88
+ The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
76
89
 
77
- 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`.
90
+ Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
78
91
 
79
- Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
92
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
80
93
 
81
94
  <a id="markdown-synopsis" name="synopsis"></a>
82
95
  ## Synopsis
@@ -136,7 +149,7 @@ Zeitwerk::Loader.eager_load_all
136
149
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
137
150
  ### The idea: File paths match constant paths
138
151
 
139
- To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
152
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
140
153
 
141
154
  ```
142
155
  lib/my_gem.rb -> MyGem
@@ -145,7 +158,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
145
158
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
146
159
  ```
147
160
 
148
- 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.
161
+ You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
149
162
 
150
163
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
151
164
  ### Inner simple constants
@@ -166,7 +179,7 @@ class HttpCrawler
166
179
  end
167
180
  ```
168
181
 
169
- The first example needs a custom [inflection](https://github.com/fxn/zeitwerk#inflection) rule:
182
+ The first example needs a custom [inflection](#inflection) rule:
170
183
 
171
184
  ```ruby
172
185
  loader.inflector.inflect("max_retries" => "MAX_RETRIES")
@@ -203,7 +216,7 @@ serializers/user_serializer.rb -> UserSerializer
203
216
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
204
217
  #### Custom root namespaces
205
218
 
206
- 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 class or module object in the optional `namespace` keyword argument.
219
+ Although `Object` is the most common root namespace, you have the flexibility to associate a different one with a specific root directory. The `push_dir` method accepts a non-anonymous class or module object as the optional `namespace` keyword argument.
207
220
 
208
221
  For example, given:
209
222
 
@@ -219,14 +232,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
219
232
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
220
233
  ```
221
234
 
222
- 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.
235
+ Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
223
236
 
224
237
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
225
238
  #### Nested root directories
226
239
 
227
- 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.
240
+ Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
228
241
 
229
- 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:
242
+ Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
230
243
 
231
244
  ```
232
245
  app/models/concerns/geolocatable.rb
@@ -237,13 +250,17 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
237
250
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
238
251
  ### Implicit namespaces
239
252
 
240
- Directories without a matching Ruby file get modules autovivified automatically by Zeitwerk. For example, in
253
+ If a namespace consists only of a simple module without any code, there is no need to explicitly define it in a separate file. Zeitwerk automatically creates modules on your behalf for directories without a corresponding Ruby file.
254
+
255
+ For instance, suppose a project includes an `admin` directory:
241
256
 
242
257
  ```
243
258
  app/controllers/admin/users_controller.rb -> Admin::UsersController
244
259
  ```
245
260
 
246
- `Admin` is autovivified as a module on demand, you do not need to define an `Admin` class or module in an `admin.rb` file explicitly.
261
+ and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
262
+
263
+ To trigger this behavior, the directory must contain non-ignored Ruby files with the ".rb" extension, either directly or recursively. Otherwise, the directory is ignored. This condition is reevaluated during reloads.
247
264
 
248
265
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
249
266
  ### Explicit namespaces
@@ -349,8 +366,6 @@ lib/my_gem/foo.rb # MyGem::Foo
349
366
 
350
367
  Neither a gemspec nor a version file are technically required, this helper works as long as the code is organized using that standard structure.
351
368
 
352
- 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.
353
-
354
369
  Conceptually, `for_gem` translates to:
355
370
 
356
371
  ```ruby
@@ -360,11 +375,9 @@ require "zeitwerk"
360
375
  loader = Zeitwerk::Loader.new
361
376
  loader.tag = File.basename(__FILE__, ".rb")
362
377
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
363
- loader.push_dir(__dir__)
378
+ loader.push_dir(File.dirname(__FILE__))
364
379
  ```
365
380
 
366
- except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload.
367
-
368
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:
369
382
 
370
383
  ```ruby
@@ -381,6 +394,87 @@ module MyGem
381
394
  end
382
395
  ```
383
396
 
397
+ 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.
398
+
399
+ `Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
400
+
401
+ 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.
402
+
403
+ Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
404
+
405
+ 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.
406
+
407
+ If the warning is legit, just tell the loader to ignore the offending file or directory:
408
+
409
+ ```ruby
410
+ loader.ignore("#{__dir__}/generators")
411
+ ```
412
+
413
+ Otherwise, there's a flag to say the extra stuff is OK:
414
+
415
+ ```ruby
416
+ Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
417
+ ```
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
+
384
478
  <a id="markdown-autoloading" name="autoloading"></a>
385
479
  ### Autoloading
386
480
 
@@ -415,10 +509,12 @@ loader.eager_load
415
509
 
416
510
  That skips [ignored files and directories](#ignoring-parts-of-the-project).
417
511
 
418
- 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).
512
+ In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
419
513
 
420
514
  Eager loading is synchronized and idempotent.
421
515
 
516
+ Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
517
+
422
518
  <a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
423
519
  #### Eager load exclusions
424
520
 
@@ -441,6 +537,81 @@ Which may be handy if the project eager loads in the test suite to [ensure proje
441
537
 
442
538
  The `force` flag does not affect ignored files and directories, those are still ignored.
443
539
 
540
+ <a id="markdown-eager-load-directories" name="eager-load-directories"></a>
541
+ #### Eager load directories
542
+
543
+ The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
544
+
545
+ ```ruby
546
+ loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
547
+ ```
548
+
549
+ This is useful when the loader is not eager loading the entire project, but you still need some subtree to be loaded for things to function properly.
550
+
551
+ 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`.
552
+
553
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
554
+
555
+ `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.
556
+
557
+ The method checks if a regular eager load was already executed, in which case it returns fast.
558
+
559
+ Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
560
+
561
+ Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
562
+
563
+ <a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
564
+ #### Eager load namespaces
565
+
566
+ The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
567
+
568
+ ```ruby
569
+ loader.eager_load_namespace(MyApp::Routes)
570
+ ```
571
+
572
+ This is useful when the loader is not eager loading the entire project, but you still need some namespace to be loaded for things to function properly.
573
+
574
+ The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
575
+
576
+ If the namespace is spread over multiple directories in the receiver's source tree, they are all eager loaded. For example, if you have a structure like
577
+
578
+ ```
579
+ root_dir1/my_app/routes
580
+ root_dir2/my_app/routes
581
+ root_dir3/my_app/routes
582
+ ```
583
+
584
+ where `root_dir{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
585
+
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.
587
+
588
+ 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.
589
+
590
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
591
+
592
+ `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.
593
+
594
+ The method checks if a regular eager load was already executed, in which case it returns fast.
595
+
596
+ If root directories are assigned to custom namespaces, the method behaves as you'd expect, according to the namespacing relationship between the custom namespace and the argument.
597
+
598
+ Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
599
+
600
+ <a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
601
+ #### Eager load namespaces shared by several loaders
602
+
603
+ The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
604
+
605
+ ```ruby
606
+ Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
607
+ ```
608
+
609
+ This may be handy, for example, if a framework supports plugins and a shared namespace needs to be eager loaded for the framework to function properly.
610
+
611
+ Please, note that loaders only eager load namespaces they manage, as documented above. Therefore, this method does not allow you to eager load namespaces not managed by Zeitwerk loaders.
612
+
613
+ This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
614
+
444
615
  <a id="markdown-global-eager-load" name="global-eager-load"></a>
445
616
  #### Global eager load
446
617
 
@@ -456,9 +627,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
456
627
 
457
628
  This method does not accept the `force` flag, since in general it wouldn't be a good idea to force eager loading in 3rd party code.
458
629
 
630
+ This method does not require that all registered loaders have `setup` already invoked, since that is out of your control. If there's any in that state, it is simply skipped.
631
+
632
+ <a id="markdown-loading-individual-files" name="loading-individual-files"></a>
633
+ ### Loading individual files
634
+
635
+ The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
636
+
637
+ ```ruby
638
+ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
639
+ ```
640
+
641
+ 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.
642
+
643
+ 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.
644
+
645
+ `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.
646
+
647
+ If you want to eager load a directory, `Zeitwerk::Loader#eager_load_dir` is more efficient than invoking `Zeitwerk::Loader#load_file` on its files.
648
+
459
649
  <a id="markdown-reloading" name="reloading"></a>
460
650
  ### Reloading
461
651
 
652
+ <a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
653
+ #### Configuration and usage
654
+
462
655
  Zeitwerk is able to reload code, but you need to enable this feature:
463
656
 
464
657
  ```ruby
@@ -472,7 +665,7 @@ loader.reload
472
665
 
473
666
  There is no way to undo this, either you want to reload or you don't.
474
667
 
475
- Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
668
+ Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`. Attempting to reload without previously calling `setup` raises `Zeitwerk::SetupRequired`.
476
669
 
477
670
  Generally speaking, reloading is useful while developing running services like web applications. Gems that implement regular libraries, so to speak, or services running in testing or production environments, won't normally have a use case for reloading. If reloading is not enabled, Zeitwerk is able to use less memory.
478
671
 
@@ -480,9 +673,36 @@ Reloading removes the currently loaded classes and modules and resets the loader
480
673
 
481
674
  It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
482
675
 
483
- In order for reloading to be thread-safe, you need to implement some coordination. For example, a web framework that serves each request with its own thread may have a globally accessible RW lock. When a request comes in, the framework acquires the lock for reading at the beginning, and the code in the framework that calls `loader.reload` needs to acquire the lock for writing.
676
+ <a id="markdown-thread-safety" name="thread-safety"></a>
677
+ #### Thread-safety
484
678
 
485
- On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes.
679
+ In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
680
+
681
+ For example, a web framework that serves each request in its own thread and has reloading enabled could create a read-write lock on boot like this:
682
+
683
+ ```ruby
684
+ require "concurrent/atomic/read_write_lock"
685
+
686
+ MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
687
+ ```
688
+
689
+ You acquire the lock for reading for serving each individual request:
690
+
691
+ ```ruby
692
+ MyFramework::RELOAD_RW_LOCK.with_read_lock do
693
+ serve(request)
694
+ end
695
+ ```
696
+
697
+ Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
698
+
699
+ ```ruby
700
+ MyFramework::RELOAD_RW_LOCK.with_write_lock do
701
+ loader.reload
702
+ end
703
+ ```
704
+
705
+ On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores reloadable controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes.
486
706
 
487
707
  <a id="markdown-inflection" name="inflection"></a>
488
708
  ### Inflection
@@ -518,9 +738,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
518
738
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
519
739
  ```
520
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
+
521
766
  Overrides need to be configured before calling `setup`.
522
767
 
523
- 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.
524
769
 
525
770
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
526
771
  #### Zeitwerk::GemInflector
@@ -531,6 +776,31 @@ This inflector is like the basic one, except it expects `lib/my_gem/version.rb`
531
776
 
532
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.
533
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
+
534
804
  <a id="markdown-custom-inflector" name="custom-inflector"></a>
535
805
  #### Custom inflector
536
806
 
@@ -763,7 +1033,7 @@ Zeitwerk::Loader.default_logger = method(:puts)
763
1033
 
764
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.
765
1035
 
766
- 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.
767
1037
 
768
1038
  <a id="markdown-loader-tag" name="loader-tag"></a>
769
1039
  #### Loader tag
@@ -789,12 +1059,14 @@ Zeitwerk@my_gem: constant MyGem::Foo loaded from ...
789
1059
  <a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
790
1060
  ### Ignoring parts of the project
791
1061
 
792
- 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".
793
1063
 
794
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.
795
1065
 
796
1066
  You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
797
1067
 
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.
1069
+
798
1070
  Let's see some use cases.
799
1071
 
800
1072
  <a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
@@ -877,10 +1149,39 @@ loader.ignore(tests)
877
1149
  loader.setup
878
1150
  ```
879
1151
 
1152
+ <a id="markdown-shadowed-files" name="shadowed-files"></a>
1153
+ ### Shadowed files
1154
+
1155
+ In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
1156
+
1157
+ ```ruby
1158
+ require "foo"
1159
+ ```
1160
+
1161
+ the first one found gets loaded, and the rest are ignored.
1162
+
1163
+ Zeitwerk behaves in a similar way. If `foo.rb` is present in several root directories (at the same namespace level), the constant `Foo` is autoloaded from the first one, and the rest of the files are not evaluated. If logging is enabled, you'll see something like
1164
+
1165
+ ```
1166
+ file #{file} is ignored because #{previous_occurrence} has precedence
1167
+ ```
1168
+
1169
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1170
+
1171
+ Even if there's only one `foo.rb`, if the constant `Foo` is already defined when Zeitwerk finds `foo.rb`, then the file is ignored too. This could happen if `Foo` was defined by a dependency, for example. If logging is enabled, you'll see something like
1172
+
1173
+ ```
1174
+ file #{file} is ignored because #{constant_path} is already defined
1175
+ ```
1176
+
1177
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1178
+
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).
1180
+
880
1181
  <a id="markdown-edge-cases" name="edge-cases"></a>
881
1182
  ### Edge cases
882
1183
 
883
- A class or module that acts as a namespace:
1184
+ [Explicit namespaces](#explicit-namespaces) like `Trip` here:
884
1185
 
885
1186
  ```ruby
886
1187
  # trip.rb
@@ -894,14 +1195,15 @@ module Trip::Geolocation
894
1195
  end
895
1196
  ```
896
1197
 
897
- has to be defined with the `class` or `module` keywords, as in the example above.
1198
+ have to be defined with the `class`/`module` keywords, as in the example above.
898
1199
 
899
1200
  For technical reasons, raw constant assignment is not supported:
900
1201
 
901
1202
  ```ruby
902
1203
  # trip.rb
903
- Trip = Class.new { ... } # NOT SUPPORTED
904
- Trip = Struct.new { ... } # NOT SUPPORTED
1204
+ Trip = Class { ...} # NOT SUPPORTED
1205
+ Trip = Struct.new { ... } # NOT SUPPORTED
1206
+ Trip = Data.define { ... } # NOT SUPPORTED
905
1207
  ```
906
1208
 
907
1209
  This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
@@ -956,12 +1258,139 @@ require "active_job"
956
1258
  require "active_job/queue_adapters"
957
1259
 
958
1260
  require "zeitwerk"
959
- loader = Zeitwerk::Loader.for_gem
1261
+ # By passing the flag, we acknowledge the extra directory lib/active_job
1262
+ # has to be managed by the loader and no warning has to be issued for it.
1263
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
960
1264
  loader.setup
961
1265
  ```
962
1266
 
963
1267
  With that, when Zeitwerk scans the file system and reaches the gem directories `lib/active_job` and `lib/active_job/queue_adapters`, it detects the corresponding modules already exist and therefore understands it does not have to manage them. The loader just descends into those directories. Eventually will reach `lib/active_job/queue_adapters/awesome_queue.rb`, and since `ActiveJob::QueueAdapters::AwesomeQueue` is unknown, Zeitwerk will manage it. Which is what happens regularly with the files in your gem. On reload, the namespaces are safe, won't be reloaded. The loader only reloads what it manages, which in this case is the adapter itself.
964
1268
 
1269
+ <a id="markdown-introspection" name="introspection"></a>
1270
+ ### Introspection
1271
+
1272
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1273
+ #### `Zeitwerk::Loader#dirs`
1274
+
1275
+ The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1276
+
1277
+ ```ruby
1278
+ loader = Zeitwerk::Loader.new
1279
+ loader.push_dir(Pathname.new("/foo"))
1280
+ loader.dirs # => ["/foo"]
1281
+ ```
1282
+
1283
+ This method accepts an optional `namespaces` keyword argument. If truthy, the method returns a hash table instead. Keys are the absolute paths of the root directories as strings. Values are their corresponding namespaces, class or module objects:
1284
+
1285
+ ```ruby
1286
+ loader = Zeitwerk::Loader.new
1287
+ loader.push_dir(Pathname.new("/foo"))
1288
+ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
1289
+ loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
1290
+ ```
1291
+
1292
+ By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
1293
+
1294
+ These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1295
+
1296
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1297
+ #### `Zeitwerk::Loader#cpath_expected_at`
1298
+
1299
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1300
+
1301
+ Some examples, assuming that `app/models` is a root directory:
1302
+
1303
+ ```ruby
1304
+ loader.cpath_expected_at("app/models") # => "Object"
1305
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1306
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1307
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1308
+ ```
1309
+
1310
+ If `collapsed` is a collapsed directory:
1311
+
1312
+ ```ruby
1313
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1314
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1315
+ loader.cpath_expected_at("a/b") # => "A::B"
1316
+ ```
1317
+
1318
+ If the argument corresponds to an [ignored file or directory](#ignoring-parts-of-the-project), the method returns `nil`. Same if the argument is not managed by the loader.
1319
+
1320
+ `Zeitwerk::Error` is raised if the given path does not exist:
1321
+
1322
+ ```ruby
1323
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1324
+ ```
1325
+
1326
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1327
+
1328
+ ```ruby
1329
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1330
+ ```
1331
+
1332
+ This method does not parse file contents and does not guarantee files define the returned constant path. It just says which is the _expected_ one.
1333
+
1334
+ `Zeitwerk::Loader#cpath_expected_at` is designed to be used with individual paths. If you want to know all the expected constant paths in the project, please use `Zeitwerk::Loader#all_expected_cpaths`, documented next.
1335
+
1336
+ <a id="markdown-zeitwerkloaderall_expected_cpaths" name="zeitwerkloaderall_expected_cpaths"></a>
1337
+ #### `Zeitwerk::Loader#all_expected_cpaths`
1338
+
1339
+ The method `Zeitwerk::Loader#all_expected_cpaths` returns a hash that maps the absolute paths of the files and directories managed by the receiver to their expected constant paths.
1340
+
1341
+ Ignored files, hidden files, and files whose extension is not ".rb" are not included in the result. Same for directories, hidden or ignored directories are not included in the result. Additionally, directories that contain no files with extension ".rb" (recursively) are also excluded, since those are not considered to represent Ruby namespaces.
1342
+
1343
+ For example, if `lib` is the root directory of a gem with the following contents:
1344
+
1345
+ ```
1346
+ lib/.DS_Store
1347
+ lib/my_gem.rb
1348
+ lib/my_gem/version.rb
1349
+ lib/my_gem/ignored.rb
1350
+ lib/my_gem/drivers/unix.rb
1351
+ lib/my_gem/drivers/windows.rb
1352
+ lib/my_gem/collapsed/foo.rb
1353
+ lib/tasks/my_gem.rake
1354
+ ```
1355
+
1356
+ `Zeitwerk::Loader#all_expected_cpaths` would return (maybe in a different order):
1357
+
1358
+ ```ruby
1359
+ {
1360
+ "/.../lib" => "Object",
1361
+ "/.../lib/my_gem.rb" => "MyGem",
1362
+ "/.../lib/my_gem" => "MyGem",
1363
+ "/.../lib/my_gem/version.rb" => "MyGem::VERSION",
1364
+ "/.../lib/my_gem/drivers" => "MyGem::Drivers",
1365
+ "/.../lib/my_gem/drivers/unix.rb" => "MyGem::Drivers::Unix",
1366
+ "/.../lib/my_gem/drivers/windows.rb" => "MyGem::Drivers::Windows",
1367
+ "/.../lib/my_gem/collapsed" => "MyGem"
1368
+ "/.../lib/my_gem/collapsed/foo.rb" => "MyGem::Foo"
1369
+ }
1370
+ ```
1371
+
1372
+ In the previous example we assume `lib/my_gem/ignored.rb` is ignored, and therefore it is not present in the returned hash. Also, `lib/my_gem/collapsed` is a collapsed directory, so the expected namespace at that level is still `MyGem` (this is an edge case).
1373
+
1374
+ The file `lib/.DS_Store` is hidden, hence excluded. The directory `lib/tasks` is also not present because it contains no files with extension ".rb".
1375
+
1376
+ Directory paths do not have trailing slashes.
1377
+
1378
+ The order of the hash entries is undefined.
1379
+
1380
+ This method does not parse or execute file contents and does not guarantee files define the corresponding constant paths. It just says which are the _expected_ ones.
1381
+
1382
+ <a id="markdown-encodings" name="encodings"></a>
1383
+ ### Encodings
1384
+
1385
+ Zeitwerk supports projects whose files and file system are in UTF-8. The encoding of the file system can be checked this way:
1386
+
1387
+ ```
1388
+ % ruby -e "puts Encoding.find('filesystem')"
1389
+ UTF-8
1390
+ ```
1391
+
1392
+ The test suite passes on Windows with codepage `Windows-1252` if all the involved absolute paths are ASCII. Other supersets of ASCII may work too, but you have to try.
1393
+
965
1394
  <a id="markdown-rules-of-thumb" name="rules-of-thumb"></a>
966
1395
  ### Rules of thumb
967
1396
 
@@ -971,7 +1400,7 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
971
1400
 
972
1401
  3. In that line, if two loaders manage files that translate to the same constant in the same namespace, the first one wins, the rest are ignored. Similar to what happens with `require` and `$LOAD_PATH`, only the first occurrence matters.
973
1402
 
974
- 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.
1403
+ 4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than defines, the namespace. This is often accomplished by loading (e.g., `require`-ing) the dependency.
975
1404
 
976
1405
  5. Objects stored in reloadable constants should not be cached in places that are not reloaded. For example, non-reloadable classes should not subclass a reloadable class, or mixin a reloadable module. Otherwise, after reloading, those classes or module objects would become stale. Referring to constants in dynamic places like method calls or lambdas is fine.
977
1406
 
@@ -980,22 +1409,11 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
980
1409
  <a id="markdown-debuggers" name="debuggers"></a>
981
1410
  ### Debuggers
982
1411
 
983
- <a id="markdown-debugrb" name="debugrb"></a>
984
- #### debug.rb
985
-
986
- 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.
987
-
988
- 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).
989
-
990
- <a id="markdown-byebug" name="byebug"></a>
991
- #### Byebug
1412
+ Zeitwerk and [debug.rb](https://github.com/ruby/debug) are fully compatible if CRuby is ≥ 3.1 (see [ruby/debug#558](https://github.com/ruby/debug/pull/558)).
992
1413
 
993
- Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) have a similar edge incompatibility.
1414
+ [Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564). Prior to CRuby 3.1, `debug.rb` has a similar edge incompatibility.
994
1415
 
995
- <a id="markdown-break" name="break"></a>
996
- #### Break
997
-
998
- Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger.
1416
+ [Break](https://github.com/gsamokovarov/break) is fully compatible.
999
1417
 
1000
1418
  <a id="markdown-pronunciation" name="pronunciation"></a>
1001
1419
  ## Pronunciation
@@ -1054,6 +1472,11 @@ With Zeitwerk, you just name things following conventions and done. Things are a
1054
1472
 
1055
1473
  Autoloading in Rails was based on `const_missing` up to Rails 5. That callback lacks fundamental information like the nesting or the resolution algorithm being used. Because of that, Rails autoloading was not able to match Ruby's semantics, and that introduced a [series of issues](https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#common-gotchas). Zeitwerk is based on a different technique and fixed Rails autoloading starting with Rails 6.
1056
1474
 
1475
+ <a id="markdown-awards" name="awards"></a>
1476
+ ## Awards
1477
+
1478
+ Zeitwerk has been awarded an "Outstanding Performance Award" Fukuoka Ruby Award 2022.
1479
+
1057
1480
  <a id="markdown-thanks" name="thanks"></a>
1058
1481
  ## Thanks
1059
1482