zeitwerk 2.5.0 → 2.6.12

Sign up to get free protection for your applications and to get access to all the features.
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,11 +25,18 @@
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)
@@ -44,20 +52,23 @@
44
52
  - [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
45
53
  - [Use case: The adapter pattern](#use-case-the-adapter-pattern)
46
54
  - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
55
+ - [Shadowed files](#shadowed-files)
47
56
  - [Edge cases](#edge-cases)
48
57
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
49
58
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
59
+ - [Introspection](#introspection)
60
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
61
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
62
+ - [Encodings](#encodings)
50
63
  - [Rules of thumb](#rules-of-thumb)
51
64
  - [Debuggers](#debuggers)
52
- - [debug.rb](#debugrb)
53
- - [Break](#break)
54
- - [Byebug](#byebug)
55
65
  - [Pronunciation](#pronunciation)
56
66
  - [Supported Ruby versions](#supported-ruby-versions)
57
67
  - [Testing](#testing)
58
68
  - [Motivation](#motivation)
59
69
  - [Kernel#require is brittle](#kernelrequire-is-brittle)
60
70
  - [Rails autoloading was brittle](#rails-autoloading-was-brittle)
71
+ - [Awards](#awards)
61
72
  - [Thanks](#thanks)
62
73
  - [License](#license)
63
74
 
@@ -68,15 +79,15 @@
68
79
 
69
80
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
70
81
 
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.
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.
72
83
 
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.
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.
74
85
 
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.
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.
76
87
 
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`.
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`.
78
89
 
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.
90
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
80
91
 
81
92
  <a id="markdown-synopsis" name="synopsis"></a>
82
93
  ## Synopsis
@@ -136,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
136
147
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
137
148
  ### The idea: File paths match constant paths
138
149
 
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:
150
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
140
151
 
141
152
  ```
142
153
  lib/my_gem.rb -> MyGem
@@ -145,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
145
156
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
146
157
  ```
147
158
 
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.
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.
149
160
 
150
161
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
151
162
  ### Inner simple constants
@@ -166,7 +177,7 @@ class HttpCrawler
166
177
  end
167
178
  ```
168
179
 
169
- The first example needs a custom [inflection](https://github.com/fxn/zeitwerk#inflection) rule:
180
+ The first example needs a custom [inflection](#inflection) rule:
170
181
 
171
182
  ```ruby
172
183
  loader.inflector.inflect("max_retries" => "MAX_RETRIES")
@@ -203,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
203
214
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
204
215
  #### Custom root namespaces
205
216
 
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.
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.
207
218
 
208
219
  For example, given:
209
220
 
@@ -219,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
219
230
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
220
231
  ```
221
232
 
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.
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.
223
234
 
224
235
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
225
236
  #### Nested root directories
226
237
 
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.
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.
228
239
 
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:
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:
230
241
 
231
242
  ```
232
243
  app/models/concerns/geolocatable.rb
@@ -237,13 +248,17 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
237
248
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
238
249
  ### Implicit namespaces
239
250
 
240
- Directories without a matching Ruby file get modules autovivified automatically by Zeitwerk. For example, in
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.
252
+
253
+ For instance, suppose a project includes an `admin` directory:
241
254
 
242
255
  ```
243
256
  app/controllers/admin/users_controller.rb -> Admin::UsersController
244
257
  ```
245
258
 
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.
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.
260
+
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.
247
262
 
248
263
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
249
264
  ### Explicit namespaces
@@ -349,8 +364,6 @@ lib/my_gem/foo.rb # MyGem::Foo
349
364
 
350
365
  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
366
 
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
367
  Conceptually, `for_gem` translates to:
355
368
 
356
369
  ```ruby
@@ -363,8 +376,6 @@ loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
363
376
  loader.push_dir(__dir__)
364
377
  ```
365
378
 
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
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:
369
380
 
370
381
  ```ruby
@@ -381,6 +392,87 @@ module MyGem
381
392
  end
382
393
  ```
383
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
+
401
+ Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
402
+
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.
404
+
405
+ If the warning is legit, just tell the loader to ignore the offending file or directory:
406
+
407
+ ```ruby
408
+ loader.ignore("#{__dir__}/generators")
409
+ ```
410
+
411
+ Otherwise, there's a flag to say the extra stuff is OK:
412
+
413
+ ```ruby
414
+ Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
415
+ ```
416
+
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`.
471
+
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).
475
+
384
476
  <a id="markdown-autoloading" name="autoloading"></a>
385
477
  ### Autoloading
386
478
 
@@ -415,10 +507,12 @@ loader.eager_load
415
507
 
416
508
  That skips [ignored files and directories](#ignoring-parts-of-the-project).
417
509
 
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).
510
+ In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
419
511
 
420
512
  Eager loading is synchronized and idempotent.
421
513
 
514
+ Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
515
+
422
516
  <a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
423
517
  #### Eager load exclusions
424
518
 
@@ -437,10 +531,85 @@ However, that can be overridden with `force`:
437
531
  loader.eager_load(force: true) # database adapters are eager loaded
438
532
  ```
439
533
 
440
- Which may be handy if the project eager loads in the test suite to [ensure project layour compliance](#testing-compliance).
534
+ Which may be handy if the project eager loads in the test suite to [ensure project layout compliance](#testing-compliance).
441
535
 
442
536
  The `force` flag does not affect ignored files and directories, those are still ignored.
443
537
 
538
+ <a id="markdown-eager-load-directories" name="eager-load-directories"></a>
539
+ #### Eager load directories
540
+
541
+ The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
542
+
543
+ ```ruby
544
+ loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
545
+ ```
546
+
547
+ 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.
548
+
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`.
550
+
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.
552
+
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.
554
+
555
+ The method checks if a regular eager load was already executed, in which case it returns fast.
556
+
557
+ Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
558
+
559
+ Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
560
+
561
+ <a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
562
+ #### Eager load namespaces
563
+
564
+ The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
565
+
566
+ ```ruby
567
+ loader.eager_load_namespace(MyApp::Routes)
568
+ ```
569
+
570
+ 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.
571
+
572
+ The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
573
+
574
+ 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
575
+
576
+ ```
577
+ root_dir1/my_app/routes
578
+ root_dir2/my_app/routes
579
+ root_dir3/my_app/routes
580
+ ```
581
+
582
+ where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
583
+
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.
585
+
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.
587
+
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.
589
+
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.
591
+
592
+ The method checks if a regular eager load was already executed, in which case it returns fast.
593
+
594
+ 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.
595
+
596
+ Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
597
+
598
+ <a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
599
+ #### Eager load namespaces shared by several loaders
600
+
601
+ The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
602
+
603
+ ```ruby
604
+ Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
605
+ ```
606
+
607
+ 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.
608
+
609
+ 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.
610
+
611
+ 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.
612
+
444
613
  <a id="markdown-global-eager-load" name="global-eager-load"></a>
445
614
  #### Global eager load
446
615
 
@@ -456,9 +625,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
456
625
 
457
626
  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
627
 
628
+ 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.
629
+
630
+ <a id="markdown-loading-individual-files" name="loading-individual-files"></a>
631
+ ### Loading individual files
632
+
633
+ The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
634
+
635
+ ```ruby
636
+ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
637
+ ```
638
+
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.
640
+
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.
642
+
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.
644
+
645
+ 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.
646
+
459
647
  <a id="markdown-reloading" name="reloading"></a>
460
648
  ### Reloading
461
649
 
650
+ <a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
651
+ #### Configuration and usage
652
+
462
653
  Zeitwerk is able to reload code, but you need to enable this feature:
463
654
 
464
655
  ```ruby
@@ -472,7 +663,7 @@ loader.reload
472
663
 
473
664
  There is no way to undo this, either you want to reload or you don't.
474
665
 
475
- Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
666
+ 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
667
 
477
668
  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
669
 
@@ -480,9 +671,36 @@ Reloading removes the currently loaded classes and modules and resets the loader
480
671
 
481
672
  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
673
 
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.
674
+ <a id="markdown-thread-safety" name="thread-safety"></a>
675
+ #### Thread-safety
676
+
677
+ In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
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
+ 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:
680
+
681
+ ```ruby
682
+ require "concurrent/atomic/read_write_lock"
683
+
684
+ MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
685
+ ```
686
+
687
+ You acquire the lock for reading for serving each individual request:
688
+
689
+ ```ruby
690
+ MyFramework::RELOAD_RW_LOCK.with_read_lock do
691
+ serve(request)
692
+ end
693
+ ```
694
+
695
+ Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
696
+
697
+ ```ruby
698
+ MyFramework::RELOAD_RW_LOCK.with_write_lock do
699
+ loader.reload
700
+ end
701
+ ```
702
+
703
+ 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
704
 
487
705
  <a id="markdown-inflection" name="inflection"></a>
488
706
  ### Inflection
@@ -518,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
518
736
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
519
737
  ```
520
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
+
521
764
  Overrides need to be configured before calling `setup`.
522
765
 
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.
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.
524
767
 
525
768
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
526
769
  #### Zeitwerk::GemInflector
@@ -795,6 +1038,8 @@ However, sometimes it might still be convenient to tell Zeitwerk to completely i
795
1038
 
796
1039
  You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
797
1040
 
1041
+ 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.
1042
+
798
1043
  Let's see some use cases.
799
1044
 
800
1045
  <a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
@@ -810,7 +1055,9 @@ Kernel.module_eval do
810
1055
  end
811
1056
  ```
812
1057
 
813
- That file does not define a constant path after the path name and you need to tell Zeitwerk:
1058
+ `Kernel` is already defined by Ruby so the module cannot be autoloaded. Also, that file does not define a constant path after the path name. Therefore, Zeitwerk should not process it at all.
1059
+
1060
+ The extension can still coexist with the rest of the project, you only need to tell Zeitwerk to ignore it:
814
1061
 
815
1062
  ```ruby
816
1063
  kernel_ext = "#{__dir__}/my_gem/core_ext/kernel.rb"
@@ -826,6 +1073,14 @@ loader.ignore(core_ext)
826
1073
  loader.setup
827
1074
  ```
828
1075
 
1076
+ Now, that file has to be loaded manually with `require` or `require_relative`:
1077
+
1078
+ ```ruby
1079
+ require_relative "my_gem/core_ext/kernel"
1080
+ ```
1081
+
1082
+ and you can do that anytime, before configuring the loader, or after configuring the loader, does not matter.
1083
+
829
1084
  <a id="markdown-use-case-the-adapter-pattern" name="use-case-the-adapter-pattern"></a>
830
1085
  #### Use case: The adapter pattern
831
1086
 
@@ -867,10 +1122,39 @@ loader.ignore(tests)
867
1122
  loader.setup
868
1123
  ```
869
1124
 
1125
+ <a id="markdown-shadowed-files" name="shadowed-files"></a>
1126
+ ### Shadowed files
1127
+
1128
+ In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
1129
+
1130
+ ```ruby
1131
+ require "foo"
1132
+ ```
1133
+
1134
+ the first one found gets loaded, and the rest are ignored.
1135
+
1136
+ 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
1137
+
1138
+ ```
1139
+ file #{file} is ignored because #{previous_occurrence} has precedence
1140
+ ```
1141
+
1142
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1143
+
1144
+ 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
1145
+
1146
+ ```
1147
+ file #{file} is ignored because #{constant_path} is already defined
1148
+ ```
1149
+
1150
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1151
+
1152
+ 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).
1153
+
870
1154
  <a id="markdown-edge-cases" name="edge-cases"></a>
871
1155
  ### Edge cases
872
1156
 
873
- A class or module that acts as a namespace:
1157
+ [Explicit namespaces](#explicit-namespaces) like `Trip` here:
874
1158
 
875
1159
  ```ruby
876
1160
  # trip.rb
@@ -884,14 +1168,15 @@ module Trip::Geolocation
884
1168
  end
885
1169
  ```
886
1170
 
887
- has to be defined with the `class` or `module` keywords, as in the example above.
1171
+ have to be defined with the `class`/`module` keywords, as in the example above.
888
1172
 
889
1173
  For technical reasons, raw constant assignment is not supported:
890
1174
 
891
1175
  ```ruby
892
1176
  # trip.rb
893
- Trip = Class.new { ... } # NOT SUPPORTED
894
- Trip = Struct.new { ... } # NOT SUPPORTED
1177
+ Trip = Class { ...} # NOT SUPPORTED
1178
+ Trip = Struct.new { ... } # NOT SUPPORTED
1179
+ Trip = Data.define { ... } # NOT SUPPORTED
895
1180
  ```
896
1181
 
897
1182
  This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
@@ -946,12 +1231,91 @@ require "active_job"
946
1231
  require "active_job/queue_adapters"
947
1232
 
948
1233
  require "zeitwerk"
949
- loader = Zeitwerk::Loader.for_gem
1234
+ # By passing the flag, we acknowledge the extra directory lib/active_job
1235
+ # has to be managed by the loader and no warning has to be issued for it.
1236
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
950
1237
  loader.setup
951
1238
  ```
952
1239
 
953
1240
  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.
954
1241
 
1242
+ <a id="markdown-introspection" name="introspection"></a>
1243
+ ### Introspection
1244
+
1245
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1246
+ #### `Zeitwerk::Loader#dirs`
1247
+
1248
+ The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1249
+
1250
+ ```ruby
1251
+ loader = Zeitwerk::Loader.new
1252
+ loader.push_dir(Pathname.new("/foo"))
1253
+ loader.dirs # => ["/foo"]
1254
+ ```
1255
+
1256
+ 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:
1257
+
1258
+ ```ruby
1259
+ loader = Zeitwerk::Loader.new
1260
+ loader.push_dir(Pathname.new("/foo"))
1261
+ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
1262
+ loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
1263
+ ```
1264
+
1265
+ By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
1266
+
1267
+ These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1268
+
1269
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1270
+ #### `Zeitwerk::Loader#cpath_expected_at`
1271
+
1272
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1273
+
1274
+ Some examples, assuming that `app/models` is a root directory:
1275
+
1276
+ ```ruby
1277
+ loader.cpath_expected_at("app/models") # => "Object"
1278
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1279
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1280
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1281
+ ```
1282
+
1283
+ If `collapsed` is a collapsed directory:
1284
+
1285
+ ```ruby
1286
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1287
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1288
+ loader.cpath_expected_at("a/b") # => "A::B"
1289
+ ```
1290
+
1291
+ 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.
1292
+
1293
+ `Zeitwerk::Error` is raised if the given path does not exist:
1294
+
1295
+ ```ruby
1296
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1297
+ ```
1298
+
1299
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1300
+
1301
+ ```ruby
1302
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1303
+ ```
1304
+
1305
+ 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.
1306
+
1307
+ <a id="markdown-encodings" name="encodings"></a>
1308
+ ### Encodings
1309
+
1310
+ Zeitwerk supports projects whose files and file system are in UTF-8. The encoding of the file system can be checked this way:
1311
+
1312
+ ```
1313
+ % ruby -e "puts Encoding.find('filesystem')"
1314
+ UTF-8
1315
+ ```
1316
+
1317
+ 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.
1318
+
955
1319
  <a id="markdown-rules-of-thumb" name="rules-of-thumb"></a>
956
1320
  ### Rules of thumb
957
1321
 
@@ -970,20 +1334,11 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
970
1334
  <a id="markdown-debuggers" name="debuggers"></a>
971
1335
  ### Debuggers
972
1336
 
973
- <a id="markdown-debugrb" name="debugrb"></a>
974
- #### debug.rb
975
-
976
- The new [debug.rb](https://github.com/ruby/debug) gem and Zeitwerk seem to be compatible, as far as I can tell. This is the new debugger that is going to ship with Ruby 3.1.
977
-
978
- <a id="markdown-break" name="break"></a>
979
- #### Break
1337
+ 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)).
980
1338
 
981
- Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger.
1339
+ [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.
982
1340
 
983
- <a id="markdown-byebug" name="byebug"></a>
984
- #### Byebug
985
-
986
- Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) are incompatible, classes or modules that belong to [explicit namespaces](#explicit-namespaces) are not autoloaded inside a Byebug session. See [this issue](https://github.com/deivid-rodriguez/byebug/issues/564#issuecomment-499413606) for further details.
1341
+ [Break](https://github.com/gsamokovarov/break) is fully compatible.
987
1342
 
988
1343
  <a id="markdown-pronunciation" name="pronunciation"></a>
989
1344
  ## Pronunciation
@@ -1042,6 +1397,11 @@ With Zeitwerk, you just name things following conventions and done. Things are a
1042
1397
 
1043
1398
  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.
1044
1399
 
1400
+ <a id="markdown-awards" name="awards"></a>
1401
+ ## Awards
1402
+
1403
+ Zeitwerk has been awarded an "Outstanding Performance Award" Fukuoka Ruby Award 2022.
1404
+
1045
1405
  <a id="markdown-thanks" name="thanks"></a>
1046
1406
  ## Thanks
1047
1407