zeitwerk 2.6.1 → 2.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +184 -36
- data/lib/zeitwerk/error.rb +6 -0
- data/lib/zeitwerk/explicit_namespace.rb +14 -10
- data/lib/zeitwerk/gem_loader.rb +1 -1
- data/lib/zeitwerk/internal.rb +12 -0
- data/lib/zeitwerk/kernel.rb +3 -0
- data/lib/zeitwerk/loader/callbacks.rb +10 -10
- data/lib/zeitwerk/loader/config.rb +82 -46
- data/lib/zeitwerk/loader/eager_load.rb +228 -0
- data/lib/zeitwerk/loader/helpers.rb +30 -16
- data/lib/zeitwerk/loader.rb +112 -113
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6c7bf12c3a33195c57541b840ca6d7dbc21b19e0d7b91d8933ad37aa88b325d
|
4
|
+
data.tar.gz: 4b8dd86417dd633b78c02eada9ed6956e8e708397c988237aaddecb9644ba469
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f0bc60d9914a9e815aed501a030f7e9bde7927f750f3836f96ac86a52c0699f4ca69456bd381845428f6b09adccaa085443f39bb0fdaa651437742e21d10af2
|
7
|
+
data.tar.gz: 3ba55f7bb24725d156cc4043697bfcd6092e2893f1aa71296965c82ae9ed66b79c30f853b535693b6b884d7d3d26cf1b122545e395ba11c92f65552e14116248
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
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/
|
6
|
+
[![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3main)
|
7
7
|
|
8
8
|
<!-- TOC -->
|
9
9
|
|
@@ -27,8 +27,14 @@
|
|
27
27
|
- [Autoloading](#autoloading)
|
28
28
|
- [Eager loading](#eager-loading)
|
29
29
|
- [Eager load exclusions](#eager-load-exclusions)
|
30
|
+
- [Eager load directories](#eager-load-directories)
|
31
|
+
- [Eager load namespaces](#eager-load-namespaces)
|
32
|
+
- [Eager load namespaces shared by several loaders](#eager-load-namespaces-shared-by-several-loaders)
|
30
33
|
- [Global eager load](#global-eager-load)
|
34
|
+
- [Loading individual files](#loading-individual-files)
|
31
35
|
- [Reloading](#reloading)
|
36
|
+
- [Configuration and usage](#configuration-and-usage)
|
37
|
+
- [Thread-safety](#thread-safety)
|
32
38
|
- [Inflection](#inflection)
|
33
39
|
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
34
40
|
- [Zeitwerk::GemInflector](#zeitwerkgeminflector)
|
@@ -44,6 +50,7 @@
|
|
44
50
|
- [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
|
45
51
|
- [Use case: The adapter pattern](#use-case-the-adapter-pattern)
|
46
52
|
- [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
|
53
|
+
- [Shadowed files](#shadowed-files)
|
47
54
|
- [Edge cases](#edge-cases)
|
48
55
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
49
56
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
@@ -51,9 +58,6 @@
|
|
51
58
|
- [Encodings](#encodings)
|
52
59
|
- [Rules of thumb](#rules-of-thumb)
|
53
60
|
- [Debuggers](#debuggers)
|
54
|
-
- [debug.rb](#debugrb)
|
55
|
-
- [Byebug](#byebug)
|
56
|
-
- [Break](#break)
|
57
61
|
- [Pronunciation](#pronunciation)
|
58
62
|
- [Supported Ruby versions](#supported-ruby-versions)
|
59
63
|
- [Testing](#testing)
|
@@ -169,7 +173,7 @@ class HttpCrawler
|
|
169
173
|
end
|
170
174
|
```
|
171
175
|
|
172
|
-
The first example needs a custom [inflection](
|
176
|
+
The first example needs a custom [inflection](#inflection) rule:
|
173
177
|
|
174
178
|
```ruby
|
175
179
|
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
@@ -206,7 +210,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
206
210
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
207
211
|
#### Custom root namespaces
|
208
212
|
|
209
|
-
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.
|
213
|
+
While `Object` is by far the most common root namespace, you can associate a different one to a particular root directory. The method `push_dir` accepts a non-anonymous class or module object in the optional `namespace` keyword argument.
|
210
214
|
|
211
215
|
For example, given:
|
212
216
|
|
@@ -384,6 +388,12 @@ module MyGem
|
|
384
388
|
end
|
385
389
|
```
|
386
390
|
|
391
|
+
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.
|
392
|
+
|
393
|
+
`Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
394
|
+
|
395
|
+
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.
|
396
|
+
|
387
397
|
Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
|
388
398
|
|
389
399
|
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.
|
@@ -400,10 +410,6 @@ Otherwise, there's a flag to say the extra stuff is OK:
|
|
400
410
|
Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
401
411
|
```
|
402
412
|
|
403
|
-
This method is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
|
404
|
-
|
405
|
-
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.
|
406
|
-
|
407
413
|
<a id="markdown-autoloading" name="autoloading"></a>
|
408
414
|
### Autoloading
|
409
415
|
|
@@ -438,10 +444,12 @@ loader.eager_load
|
|
438
444
|
|
439
445
|
That skips [ignored files and directories](#ignoring-parts-of-the-project).
|
440
446
|
|
441
|
-
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](
|
447
|
+
In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
|
442
448
|
|
443
449
|
Eager loading is synchronized and idempotent.
|
444
450
|
|
451
|
+
Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
452
|
+
|
445
453
|
<a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
|
446
454
|
#### Eager load exclusions
|
447
455
|
|
@@ -464,6 +472,81 @@ Which may be handy if the project eager loads in the test suite to [ensure proje
|
|
464
472
|
|
465
473
|
The `force` flag does not affect ignored files and directories, those are still ignored.
|
466
474
|
|
475
|
+
<a id="markdown-eager-load-directories" name="eager-load-directories"></a>
|
476
|
+
#### Eager load directories
|
477
|
+
|
478
|
+
The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
|
479
|
+
|
480
|
+
```ruby
|
481
|
+
loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
|
482
|
+
```
|
483
|
+
|
484
|
+
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.
|
485
|
+
|
486
|
+
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`.
|
487
|
+
|
488
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
489
|
+
|
490
|
+
`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.
|
491
|
+
|
492
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
493
|
+
|
494
|
+
Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
|
495
|
+
|
496
|
+
Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
497
|
+
|
498
|
+
<a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
|
499
|
+
#### Eager load namespaces
|
500
|
+
|
501
|
+
The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
|
502
|
+
|
503
|
+
```ruby
|
504
|
+
loader.eager_load_namespace(MyApp::Routes)
|
505
|
+
```
|
506
|
+
|
507
|
+
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.
|
508
|
+
|
509
|
+
The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
|
510
|
+
|
511
|
+
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
|
512
|
+
|
513
|
+
```
|
514
|
+
root_dir1/my_app/routes
|
515
|
+
root_dir2/my_app/routes
|
516
|
+
root_dir3/my_app/routes
|
517
|
+
```
|
518
|
+
|
519
|
+
where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
|
520
|
+
|
521
|
+
There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
|
522
|
+
|
523
|
+
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.
|
524
|
+
|
525
|
+
[Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
|
526
|
+
|
527
|
+
`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.
|
528
|
+
|
529
|
+
The method checks if a regular eager load was already executed, in which case it returns fast.
|
530
|
+
|
531
|
+
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.
|
532
|
+
|
533
|
+
Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
|
534
|
+
|
535
|
+
<a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
|
536
|
+
#### Eager load namespaces shared by several loaders
|
537
|
+
|
538
|
+
The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
|
539
|
+
|
540
|
+
```ruby
|
541
|
+
Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
|
542
|
+
```
|
543
|
+
|
544
|
+
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.
|
545
|
+
|
546
|
+
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.
|
547
|
+
|
548
|
+
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.
|
549
|
+
|
467
550
|
<a id="markdown-global-eager-load" name="global-eager-load"></a>
|
468
551
|
#### Global eager load
|
469
552
|
|
@@ -479,9 +562,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
|
|
479
562
|
|
480
563
|
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.
|
481
564
|
|
565
|
+
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.
|
566
|
+
|
567
|
+
<a id="markdown-loading-individual-files" name="loading-individual-files"></a>
|
568
|
+
### Loading individual files
|
569
|
+
|
570
|
+
The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
|
571
|
+
|
572
|
+
```ruby
|
573
|
+
loader.load_file("#{__dir__}/custom_web_app/routes.rb")
|
574
|
+
```
|
575
|
+
|
576
|
+
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.
|
577
|
+
|
578
|
+
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.
|
579
|
+
|
580
|
+
`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.
|
581
|
+
|
582
|
+
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.
|
583
|
+
|
482
584
|
<a id="markdown-reloading" name="reloading"></a>
|
483
585
|
### Reloading
|
484
586
|
|
587
|
+
<a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
|
588
|
+
#### Configuration and usage
|
589
|
+
|
485
590
|
Zeitwerk is able to reload code, but you need to enable this feature:
|
486
591
|
|
487
592
|
```ruby
|
@@ -495,7 +600,7 @@ loader.reload
|
|
495
600
|
|
496
601
|
There is no way to undo this, either you want to reload or you don't.
|
497
602
|
|
498
|
-
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
|
603
|
+
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`.
|
499
604
|
|
500
605
|
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.
|
501
606
|
|
@@ -503,12 +608,34 @@ Reloading removes the currently loaded classes and modules and resets the loader
|
|
503
608
|
|
504
609
|
It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
|
505
610
|
|
506
|
-
|
611
|
+
<a id="markdown-thread-safety" name="thread-safety"></a>
|
612
|
+
#### Thread-safety
|
613
|
+
|
614
|
+
In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
|
615
|
+
|
616
|
+
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:
|
617
|
+
|
618
|
+
```ruby
|
619
|
+
require "concurrent/atomic/read_write_lock"
|
620
|
+
|
621
|
+
MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
|
622
|
+
```
|
623
|
+
|
624
|
+
You acquire the lock for reading for serving each individual request:
|
625
|
+
|
626
|
+
```ruby
|
627
|
+
MyFramework::RELOAD_RW_LOCK.with_read_lock do
|
628
|
+
serve(request)
|
629
|
+
end
|
630
|
+
```
|
507
631
|
|
508
|
-
|
509
|
-
* You should not autoload while another thread is reloading.
|
632
|
+
Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
|
510
633
|
|
511
|
-
|
634
|
+
```ruby
|
635
|
+
MyFramework::RELOAD_RW_LOCK.with_write_lock do
|
636
|
+
loader.reload
|
637
|
+
end
|
638
|
+
```
|
512
639
|
|
513
640
|
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.
|
514
641
|
|
@@ -823,6 +950,8 @@ However, sometimes it might still be convenient to tell Zeitwerk to completely i
|
|
823
950
|
|
824
951
|
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
825
952
|
|
953
|
+
There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explictly if you want it ignored too.
|
954
|
+
|
826
955
|
Let's see some use cases.
|
827
956
|
|
828
957
|
<a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
|
@@ -905,10 +1034,39 @@ loader.ignore(tests)
|
|
905
1034
|
loader.setup
|
906
1035
|
```
|
907
1036
|
|
1037
|
+
<a id="markdown-shadowed-files" name="shadowed-files"></a>
|
1038
|
+
### Shadowed files
|
1039
|
+
|
1040
|
+
In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
|
1041
|
+
|
1042
|
+
```ruby
|
1043
|
+
require "foo"
|
1044
|
+
```
|
1045
|
+
|
1046
|
+
the first one found gets loaded, and the rest are ignored.
|
1047
|
+
|
1048
|
+
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
|
1049
|
+
|
1050
|
+
```
|
1051
|
+
file #{file} is ignored because #{previous_occurrence} has precedence
|
1052
|
+
```
|
1053
|
+
|
1054
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1055
|
+
|
1056
|
+
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
|
1057
|
+
|
1058
|
+
```
|
1059
|
+
file #{file} is ignored because #{constant_path} is already defined
|
1060
|
+
```
|
1061
|
+
|
1062
|
+
(This message is not public interface and may change, you cannot rely on that exact wording.)
|
1063
|
+
|
1064
|
+
Shadowing only applies to Ruby files, namespace definition can be spread over multiple directories. And you can also reopen third-party namespaces if done [orderly](#reopening-third-party-namespaces).
|
1065
|
+
|
908
1066
|
<a id="markdown-edge-cases" name="edge-cases"></a>
|
909
1067
|
### Edge cases
|
910
1068
|
|
911
|
-
|
1069
|
+
[Explicit namespaces](#explicit-namespaces) like `Trip` here:
|
912
1070
|
|
913
1071
|
```ruby
|
914
1072
|
# trip.rb
|
@@ -922,14 +1080,15 @@ module Trip::Geolocation
|
|
922
1080
|
end
|
923
1081
|
```
|
924
1082
|
|
925
|
-
|
1083
|
+
have to be defined with the `class`/`module` keywords, as in the example above.
|
926
1084
|
|
927
1085
|
For technical reasons, raw constant assignment is not supported:
|
928
1086
|
|
929
1087
|
```ruby
|
930
1088
|
# trip.rb
|
931
|
-
Trip = Class
|
932
|
-
Trip = Struct.new { ... }
|
1089
|
+
Trip = Class { ...} # NOT SUPPORTED
|
1090
|
+
Trip = Struct.new { ... } # NOT SUPPORTED
|
1091
|
+
Trip = Data.define { ... } # NOT SUPPORTED
|
933
1092
|
```
|
934
1093
|
|
935
1094
|
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
@@ -984,7 +1143,7 @@ require "active_job"
|
|
984
1143
|
require "active_job/queue_adapters"
|
985
1144
|
|
986
1145
|
require "zeitwerk"
|
987
|
-
# By
|
1146
|
+
# By passing the flag, we acknowledge the extra directory lib/active_job
|
988
1147
|
# has to be managed by the loader and no warning has to be issued for it.
|
989
1148
|
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
990
1149
|
loader.setup
|
@@ -1012,6 +1171,8 @@ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
|
|
1012
1171
|
loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
|
1013
1172
|
```
|
1014
1173
|
|
1174
|
+
By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
|
1175
|
+
|
1015
1176
|
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1016
1177
|
|
1017
1178
|
<a id="markdown-encodings" name="encodings"></a>
|
@@ -1044,22 +1205,9 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1044
1205
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1045
1206
|
### Debuggers
|
1046
1207
|
|
1047
|
-
|
1048
|
-
#### debug.rb
|
1049
|
-
|
1050
|
-
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.
|
1051
|
-
|
1052
|
-
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).
|
1053
|
-
|
1054
|
-
<a id="markdown-byebug" name="byebug"></a>
|
1055
|
-
#### Byebug
|
1056
|
-
|
1057
|
-
Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) have a similar edge incompatibility.
|
1058
|
-
|
1059
|
-
<a id="markdown-break" name="break"></a>
|
1060
|
-
#### Break
|
1208
|
+
Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
|
1061
1209
|
|
1062
|
-
|
1210
|
+
[Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564).
|
1063
1211
|
|
1064
1212
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1065
1213
|
## Pronunciation
|
data/lib/zeitwerk/error.rb
CHANGED
@@ -11,28 +11,28 @@ module Zeitwerk
|
|
11
11
|
module ExplicitNamespace # :nodoc: all
|
12
12
|
class << self
|
13
13
|
include RealModName
|
14
|
+
extend Internal
|
14
15
|
|
15
16
|
# Maps constant paths that correspond to explicit namespaces according to
|
16
17
|
# the file system, to the loader responsible for them.
|
17
18
|
#
|
18
|
-
# @private
|
19
19
|
# @sig Hash[String, Zeitwerk::Loader]
|
20
20
|
attr_reader :cpaths
|
21
|
+
private :cpaths
|
21
22
|
|
22
|
-
# @private
|
23
23
|
# @sig Mutex
|
24
24
|
attr_reader :mutex
|
25
|
+
private :mutex
|
25
26
|
|
26
|
-
# @private
|
27
27
|
# @sig TracePoint
|
28
28
|
attr_reader :tracer
|
29
|
+
private :tracer
|
29
30
|
|
30
31
|
# Asserts `cpath` corresponds to an explicit namespace for which `loader`
|
31
32
|
# is responsible.
|
32
33
|
#
|
33
|
-
# @private
|
34
34
|
# @sig (String, Zeitwerk::Loader) -> void
|
35
|
-
def register(cpath, loader)
|
35
|
+
internal def register(cpath, loader)
|
36
36
|
mutex.synchronize do
|
37
37
|
cpaths[cpath] = loader
|
38
38
|
# We check enabled? because, looking at the C source code, enabling an
|
@@ -41,24 +41,28 @@ module Zeitwerk
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
# @private
|
45
44
|
# @sig (Zeitwerk::Loader) -> void
|
46
|
-
def unregister_loader(loader)
|
45
|
+
internal def unregister_loader(loader)
|
47
46
|
cpaths.delete_if { |_cpath, l| l == loader }
|
48
47
|
disable_tracer_if_unneeded
|
49
48
|
end
|
50
49
|
|
51
|
-
|
50
|
+
# This is an internal method only used by the test suite.
|
51
|
+
#
|
52
|
+
# @sig (String) -> bool
|
53
|
+
internal def registered?(cpath)
|
54
|
+
cpaths.key?(cpath)
|
55
|
+
end
|
52
56
|
|
53
57
|
# @sig () -> void
|
54
|
-
def disable_tracer_if_unneeded
|
58
|
+
private def disable_tracer_if_unneeded
|
55
59
|
mutex.synchronize do
|
56
60
|
tracer.disable if cpaths.empty?
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
64
|
# @sig (TracePoint) -> void
|
61
|
-
def tracepoint_class_callback(event)
|
65
|
+
private def tracepoint_class_callback(event)
|
62
66
|
# If the class is a singleton class, we won't do anything with it so we
|
63
67
|
# can bail out immediately. This is several orders of magnitude faster
|
64
68
|
# than accessing its name.
|
data/lib/zeitwerk/gem_loader.rb
CHANGED
@@ -43,7 +43,7 @@ module Zeitwerk
|
|
43
43
|
next if abspath == expected_namespace_dir
|
44
44
|
|
45
45
|
basename_without_ext = basename.delete_suffix(".rb")
|
46
|
-
cname = inflector.camelize(basename_without_ext, abspath)
|
46
|
+
cname = inflector.camelize(basename_without_ext, abspath).to_sym
|
47
47
|
ftype = dir?(abspath) ? "directory" : "file"
|
48
48
|
|
49
49
|
warn(<<~EOS)
|
data/lib/zeitwerk/kernel.rb
CHANGED
@@ -19,6 +19,9 @@ module Kernel
|
|
19
19
|
# included in Object, and changes in ancestors don't get propagated into
|
20
20
|
# already existing ancestor chains on Ruby < 3.0.
|
21
21
|
alias_method :zeitwerk_original_require, :require
|
22
|
+
class << self
|
23
|
+
alias_method :zeitwerk_original_require, :require
|
24
|
+
end
|
22
25
|
|
23
26
|
# @sig (String) -> true | false
|
24
27
|
def require(path)
|
@@ -11,18 +11,18 @@ module Zeitwerk::Loader::Callbacks
|
|
11
11
|
cref = autoloads.delete(file)
|
12
12
|
cpath = cpath(*cref)
|
13
13
|
|
14
|
-
# If reloading is enabled, we need to put this constant for unloading
|
15
|
-
# regardless of what cdef? says. In Ruby < 3.1 the internal state is not
|
16
|
-
# fully cleared. Module#constants still includes it, and you need to
|
17
|
-
# remove_const. See https://github.com/ruby/ruby/pull/4715.
|
18
|
-
to_unload[cpath] = [file, cref] if reloading_enabled?
|
19
14
|
Zeitwerk::Registry.unregister_autoload(file)
|
20
15
|
|
21
16
|
if cdef?(*cref)
|
22
17
|
log("constant #{cpath} loaded from file #{file}") if logger
|
18
|
+
to_unload[cpath] = [file, cref] if reloading_enabled?
|
23
19
|
run_on_load_callbacks(cpath, cget(*cref), file) unless on_load_callbacks.empty?
|
24
20
|
else
|
25
|
-
|
21
|
+
msg = "expected file #{file} to define constant #{cpath}, but didn't"
|
22
|
+
log(msg) if logger
|
23
|
+
crem(*cref)
|
24
|
+
to_unload[cpath] = [file, cref] if reloading_enabled?
|
25
|
+
raise Zeitwerk::NameError.new(msg, cref.last)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -42,7 +42,7 @@ module Zeitwerk::Loader::Callbacks
|
|
42
42
|
# Without the mutex and subsequent delete call, t2 would reset the module.
|
43
43
|
# That not only would reassign the constant (undesirable per se) but, worse,
|
44
44
|
# the module object created by t2 wouldn't have any of the autoloads for its
|
45
|
-
# children, since t1 would have correctly deleted its
|
45
|
+
# children, since t1 would have correctly deleted its namespace_dirs entry.
|
46
46
|
mutex2.synchronize do
|
47
47
|
if cref = autoloads.delete(dir)
|
48
48
|
autovivified_module = cref[0].const_set(cref[1], Module.new)
|
@@ -71,9 +71,9 @@ module Zeitwerk::Loader::Callbacks
|
|
71
71
|
# @private
|
72
72
|
# @sig (Module) -> void
|
73
73
|
def on_namespace_loaded(namespace)
|
74
|
-
if
|
75
|
-
|
76
|
-
set_autoloads_in_dir(
|
74
|
+
if dirs = namespace_dirs.delete(real_mod_name(namespace))
|
75
|
+
dirs.each do |dir|
|
76
|
+
set_autoloads_in_dir(dir, namespace)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|