zeitwerk 2.6.8 → 2.6.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +89 -18
- data/lib/zeitwerk/kernel.rb +3 -3
- data/lib/zeitwerk/loader/callbacks.rb +16 -8
- data/lib/zeitwerk/loader/eager_load.rb +9 -3
- data/lib/zeitwerk/loader/helpers.rb +44 -0
- data/lib/zeitwerk/loader.rb +69 -39
- data/lib/zeitwerk/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a9048a5ba05f448e5406080995aab9709a3dd7c8dd32aad3aa0ba4f544b69c4
|
4
|
+
data.tar.gz: 581cf27f70c82b754a1baadf7a41f6c87c069ae2e78bd26b4ec6e02cfa2795b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45327b148bab210d941ffeae022b70e5c2f2be4372f2192a08859faeed81d2cc5702b05d6e4efcf676a8fb7b451c46200ac372051c68ba84cfc31ed9339f3ede
|
7
|
+
data.tar.gz: 8e3266d7641f58b6a8f74abfa913c14ae4a79d4efd177578de4e7e144a21adc33d33279b7d2771cdc57937cfedfb1f5e193bb9e57c18ea90956b342b516e9bd9
|
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[![Gem Version](https://img.shields.io/gem/v/zeitwerk.svg?style=for-the-badge)](https://rubygems.org/gems/zeitwerk)
|
6
|
-
[![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%
|
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
|
|
@@ -56,6 +57,8 @@
|
|
56
57
|
- [Beware of circular dependencies](#beware-of-circular-dependencies)
|
57
58
|
- [Reopening third-party namespaces](#reopening-third-party-namespaces)
|
58
59
|
- [Introspection](#introspection)
|
60
|
+
- [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
|
61
|
+
- [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
|
59
62
|
- [Encodings](#encodings)
|
60
63
|
- [Rules of thumb](#rules-of-thumb)
|
61
64
|
- [Debuggers](#debuggers)
|
@@ -76,15 +79,15 @@
|
|
76
79
|
|
77
80
|
Zeitwerk is an efficient and thread-safe code loader for Ruby.
|
78
81
|
|
79
|
-
Given a [conventional file structure](#file-structure), Zeitwerk is
|
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.
|
80
83
|
|
81
|
-
Zeitwerk
|
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.
|
82
85
|
|
83
|
-
The gem is designed
|
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.
|
84
87
|
|
85
|
-
Internally, Zeitwerk
|
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`.
|
86
89
|
|
87
|
-
Furthermore, Zeitwerk
|
90
|
+
Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
|
88
91
|
|
89
92
|
<a id="markdown-synopsis" name="synopsis"></a>
|
90
93
|
## Synopsis
|
@@ -144,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
|
|
144
147
|
<a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
|
145
148
|
### The idea: File paths match constant paths
|
146
149
|
|
147
|
-
|
150
|
+
For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
|
148
151
|
|
149
152
|
```
|
150
153
|
lib/my_gem.rb -> MyGem
|
@@ -153,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
|
|
153
156
|
lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
|
154
157
|
```
|
155
158
|
|
156
|
-
You can tune
|
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.
|
157
160
|
|
158
161
|
<a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
|
159
162
|
### Inner simple constants
|
@@ -211,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
|
|
211
214
|
<a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
|
212
215
|
#### Custom root namespaces
|
213
216
|
|
214
|
-
|
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.
|
215
218
|
|
216
219
|
For example, given:
|
217
220
|
|
@@ -227,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
|
|
227
230
|
adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
|
228
231
|
```
|
229
232
|
|
230
|
-
Please
|
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.
|
231
234
|
|
232
235
|
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
233
236
|
#### Nested root directories
|
234
237
|
|
235
|
-
Root directories
|
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.
|
236
239
|
|
237
|
-
Zeitwerk
|
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:
|
238
241
|
|
239
242
|
```
|
240
243
|
app/models/concerns/geolocatable.rb
|
@@ -245,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
|
|
245
248
|
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
246
249
|
### Implicit namespaces
|
247
250
|
|
248
|
-
If a namespace
|
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.
|
249
252
|
|
250
|
-
For
|
253
|
+
For instance, suppose a project includes an `admin` directory:
|
251
254
|
|
252
255
|
```
|
253
256
|
app/controllers/admin/users_controller.rb -> Admin::UsersController
|
@@ -255,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
|
|
255
258
|
|
256
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.
|
257
260
|
|
258
|
-
|
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.
|
259
262
|
|
260
263
|
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
261
264
|
### Explicit namespaces
|
@@ -733,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
|
|
733
736
|
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
734
737
|
```
|
735
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
|
+
|
736
764
|
Overrides need to be configured before calling `setup`.
|
737
765
|
|
738
|
-
|
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.
|
739
767
|
|
740
768
|
<a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
|
741
769
|
#### Zeitwerk::GemInflector
|
@@ -1214,6 +1242,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
|
|
1214
1242
|
<a id="markdown-introspection" name="introspection"></a>
|
1215
1243
|
### Introspection
|
1216
1244
|
|
1245
|
+
<a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
|
1246
|
+
#### `Zeitwerk::Loader#dirs`
|
1247
|
+
|
1217
1248
|
The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
|
1218
1249
|
|
1219
1250
|
```ruby
|
@@ -1235,6 +1266,44 @@ By default, ignored root directories are filtered out. If you want them included
|
|
1235
1266
|
|
1236
1267
|
These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
|
1237
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
|
+
|
1238
1307
|
<a id="markdown-encodings" name="encodings"></a>
|
1239
1308
|
### Encodings
|
1240
1309
|
|
@@ -1265,9 +1334,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
|
|
1265
1334
|
<a id="markdown-debuggers" name="debuggers"></a>
|
1266
1335
|
### Debuggers
|
1267
1336
|
|
1268
|
-
Zeitwerk
|
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)).
|
1338
|
+
|
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.
|
1269
1340
|
|
1270
|
-
[
|
1341
|
+
[Break](https://github.com/gsamokovarov/break) is fully compatible.
|
1271
1342
|
|
1272
1343
|
<a id="markdown-pronunciation" name="pronunciation"></a>
|
1273
1344
|
## Pronunciation
|
data/lib/zeitwerk/kernel.rb
CHANGED
@@ -28,10 +28,10 @@ module Kernel
|
|
28
28
|
if loader = Zeitwerk::Registry.loader_for(path)
|
29
29
|
if path.end_with?(".rb")
|
30
30
|
required = zeitwerk_original_require(path)
|
31
|
-
loader.
|
31
|
+
loader.__on_file_autoloaded(path) if required
|
32
32
|
required
|
33
33
|
else
|
34
|
-
loader.
|
34
|
+
loader.__on_dir_autoloaded(path)
|
35
35
|
true
|
36
36
|
end
|
37
37
|
else
|
@@ -39,7 +39,7 @@ module Kernel
|
|
39
39
|
if required
|
40
40
|
abspath = $LOADED_FEATURES.last
|
41
41
|
if loader = Zeitwerk::Registry.loader_for(abspath)
|
42
|
-
loader.
|
42
|
+
loader.__on_file_autoloaded(abspath)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
required
|
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
module Zeitwerk::Loader::Callbacks
|
4
4
|
include Zeitwerk::RealModName
|
5
|
+
extend Zeitwerk::Internal
|
5
6
|
|
6
7
|
# Invoked from our decorated Kernel#require when a managed file is autoloaded.
|
7
8
|
#
|
8
|
-
# @private
|
9
9
|
# @sig (String) -> void
|
10
|
-
def on_file_autoloaded(file)
|
10
|
+
internal def on_file_autoloaded(file)
|
11
11
|
cref = autoloads.delete(file)
|
12
12
|
cpath = cpath(*cref)
|
13
13
|
|
@@ -20,8 +20,16 @@ module Zeitwerk::Loader::Callbacks
|
|
20
20
|
else
|
21
21
|
msg = "expected file #{file} to define constant #{cpath}, but didn't"
|
22
22
|
log(msg) if logger
|
23
|
+
|
24
|
+
# Ruby still keeps the autoload defined, but we remove it because the
|
25
|
+
# contract in Zeitwerk is more strict.
|
23
26
|
crem(*cref)
|
27
|
+
|
28
|
+
# Since the expected constant was not defined, there is nothing to unload.
|
29
|
+
# However, if the exception is rescued and reloading is enabled, we still
|
30
|
+
# need to deleted the file from $LOADED_FEATURES.
|
24
31
|
to_unload[cpath] = [file, cref] if reloading_enabled?
|
32
|
+
|
25
33
|
raise Zeitwerk::NameError.new(msg, cref.last)
|
26
34
|
end
|
27
35
|
end
|
@@ -29,11 +37,11 @@ module Zeitwerk::Loader::Callbacks
|
|
29
37
|
# Invoked from our decorated Kernel#require when a managed directory is
|
30
38
|
# autoloaded.
|
31
39
|
#
|
32
|
-
# @private
|
33
40
|
# @sig (String) -> void
|
34
|
-
def on_dir_autoloaded(dir)
|
35
|
-
# Module#autoload does not serialize concurrent requires, and
|
36
|
-
# directories ourselves
|
41
|
+
internal def on_dir_autoloaded(dir)
|
42
|
+
# Module#autoload does not serialize concurrent requires in CRuby < 3.2, and
|
43
|
+
# we handle directories ourselves without going through Kernel#require, so
|
44
|
+
# the callback needs to account for concurrency.
|
37
45
|
#
|
38
46
|
# Multi-threading would introduce a race condition here in which thread t1
|
39
47
|
# autovivifies the module, and while autoloads for its children are being
|
@@ -43,7 +51,7 @@ module Zeitwerk::Loader::Callbacks
|
|
43
51
|
# That not only would reassign the constant (undesirable per se) but, worse,
|
44
52
|
# the module object created by t2 wouldn't have any of the autoloads for its
|
45
53
|
# children, since t1 would have correctly deleted its namespace_dirs entry.
|
46
|
-
|
54
|
+
dirs_autoload_monitor.synchronize do
|
47
55
|
if cref = autoloads.delete(dir)
|
48
56
|
autovivified_module = cref[0].const_set(cref[1], Module.new)
|
49
57
|
cpath = autovivified_module.name
|
@@ -73,7 +81,7 @@ module Zeitwerk::Loader::Callbacks
|
|
73
81
|
def on_namespace_loaded(namespace)
|
74
82
|
if dirs = namespace_dirs.delete(real_mod_name(namespace))
|
75
83
|
dirs.each do |dir|
|
76
|
-
|
84
|
+
define_autoloads_for_dir(dir, namespace)
|
77
85
|
end
|
78
86
|
end
|
79
87
|
end
|
@@ -45,8 +45,10 @@ module Zeitwerk::Loader::EagerLoad
|
|
45
45
|
|
46
46
|
break if root_namespace = roots[dir]
|
47
47
|
|
48
|
+
basename = File.basename(dir)
|
49
|
+
return if hidden?(basename)
|
50
|
+
|
48
51
|
unless collapse?(dir)
|
49
|
-
basename = File.basename(dir)
|
50
52
|
cnames << inflector.camelize(basename, dir).to_sym
|
51
53
|
end
|
52
54
|
end
|
@@ -119,6 +121,8 @@ module Zeitwerk::Loader::EagerLoad
|
|
119
121
|
raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(abspath)
|
120
122
|
|
121
123
|
basename = File.basename(abspath, ".rb")
|
124
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
|
125
|
+
|
122
126
|
base_cname = inflector.camelize(basename, abspath).to_sym
|
123
127
|
|
124
128
|
root_namespace = nil
|
@@ -129,8 +133,10 @@ module Zeitwerk::Loader::EagerLoad
|
|
129
133
|
|
130
134
|
break if root_namespace = roots[dir]
|
131
135
|
|
136
|
+
basename = File.basename(dir)
|
137
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
|
138
|
+
|
132
139
|
unless collapse?(dir)
|
133
|
-
basename = File.basename(dir)
|
134
140
|
cnames << inflector.camelize(basename, dir).to_sym
|
135
141
|
end
|
136
142
|
end
|
@@ -165,7 +171,7 @@ module Zeitwerk::Loader::EagerLoad
|
|
165
171
|
next if honour_exclusions && eager_load_exclusions.member?(abspath)
|
166
172
|
|
167
173
|
if ruby?(abspath)
|
168
|
-
if (cref = autoloads[abspath])
|
174
|
+
if (cref = autoloads[abspath])
|
169
175
|
cget(*cref)
|
170
176
|
end
|
171
177
|
else
|
@@ -140,4 +140,48 @@ module Zeitwerk::Loader::Helpers
|
|
140
140
|
private def crem(parent, cname)
|
141
141
|
parent.__send__(:remove_const, cname)
|
142
142
|
end
|
143
|
+
|
144
|
+
CNAME_VALIDATOR = Module.new
|
145
|
+
private_constant :CNAME_VALIDATOR
|
146
|
+
|
147
|
+
# @raise [Zeitwerk::NameError]
|
148
|
+
# @sig (String, String) -> Symbol
|
149
|
+
private def cname_for(basename, abspath)
|
150
|
+
cname = inflector.camelize(basename, abspath)
|
151
|
+
|
152
|
+
unless cname.is_a?(String)
|
153
|
+
raise TypeError, "#{inflector.class}#camelize must return a String, received #{cname.inspect}"
|
154
|
+
end
|
155
|
+
|
156
|
+
if cname.include?("::")
|
157
|
+
raise Zeitwerk::NameError.new(<<~MESSAGE, cname)
|
158
|
+
wrong constant name #{cname} inferred by #{inflector.class} from
|
159
|
+
|
160
|
+
#{abspath}
|
161
|
+
|
162
|
+
#{inflector.class}#camelize should return a simple constant name without "::"
|
163
|
+
MESSAGE
|
164
|
+
end
|
165
|
+
|
166
|
+
begin
|
167
|
+
CNAME_VALIDATOR.const_defined?(cname, false)
|
168
|
+
rescue ::NameError => error
|
169
|
+
path_type = ruby?(abspath) ? "file" : "directory"
|
170
|
+
|
171
|
+
raise Zeitwerk::NameError.new(<<~MESSAGE, error.name)
|
172
|
+
#{error.message} inferred by #{inflector.class} from #{path_type}
|
173
|
+
|
174
|
+
#{abspath}
|
175
|
+
|
176
|
+
Possible ways to address this:
|
177
|
+
|
178
|
+
* Tell Zeitwerk to ignore this particular #{path_type}.
|
179
|
+
* Tell Zeitwerk to ignore one of its parent directories.
|
180
|
+
* Rename the #{path_type} to comply with the naming conventions.
|
181
|
+
* Modify the inflector to handle this case.
|
182
|
+
MESSAGE
|
183
|
+
end
|
184
|
+
|
185
|
+
cname.to_sym
|
186
|
+
end
|
143
187
|
end
|
data/lib/zeitwerk/loader.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "monitor"
|
3
4
|
require "set"
|
4
5
|
|
5
6
|
module Zeitwerk
|
@@ -91,9 +92,9 @@ module Zeitwerk
|
|
91
92
|
attr_reader :mutex
|
92
93
|
private :mutex
|
93
94
|
|
94
|
-
# @sig
|
95
|
-
attr_reader :
|
96
|
-
private :
|
95
|
+
# @sig Monitor
|
96
|
+
attr_reader :dirs_autoload_monitor
|
97
|
+
private :dirs_autoload_monitor
|
97
98
|
|
98
99
|
def initialize
|
99
100
|
super
|
@@ -103,11 +104,12 @@ module Zeitwerk
|
|
103
104
|
@to_unload = {}
|
104
105
|
@namespace_dirs = Hash.new { |h, cpath| h[cpath] = [] }
|
105
106
|
@shadowed_files = Set.new
|
106
|
-
@mutex = Mutex.new
|
107
|
-
@mutex2 = Mutex.new
|
108
107
|
@setup = false
|
109
108
|
@eager_loaded = false
|
110
109
|
|
110
|
+
@mutex = Mutex.new
|
111
|
+
@dirs_autoload_monitor = Monitor.new
|
112
|
+
|
111
113
|
Registry.register_loader(self)
|
112
114
|
end
|
113
115
|
|
@@ -119,7 +121,7 @@ module Zeitwerk
|
|
119
121
|
break if @setup
|
120
122
|
|
121
123
|
actual_roots.each do |root_dir, root_namespace|
|
122
|
-
|
124
|
+
define_autoloads_for_dir(root_dir, root_namespace)
|
123
125
|
end
|
124
126
|
|
125
127
|
on_setup_callbacks.each(&:call)
|
@@ -228,6 +230,54 @@ module Zeitwerk
|
|
228
230
|
setup
|
229
231
|
end
|
230
232
|
|
233
|
+
# @sig (String | Pathname) -> String?
|
234
|
+
def cpath_expected_at(path)
|
235
|
+
abspath = File.expand_path(path)
|
236
|
+
|
237
|
+
raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)
|
238
|
+
|
239
|
+
return unless dir?(abspath) || ruby?(abspath)
|
240
|
+
return if ignored_path?(abspath)
|
241
|
+
|
242
|
+
paths = []
|
243
|
+
|
244
|
+
if ruby?(abspath)
|
245
|
+
basename = File.basename(abspath, ".rb")
|
246
|
+
return if hidden?(basename)
|
247
|
+
|
248
|
+
paths << [basename, abspath]
|
249
|
+
walk_up_from = File.dirname(abspath)
|
250
|
+
else
|
251
|
+
walk_up_from = abspath
|
252
|
+
end
|
253
|
+
|
254
|
+
root_namespace = nil
|
255
|
+
|
256
|
+
walk_up(walk_up_from) do |dir|
|
257
|
+
break if root_namespace = roots[dir]
|
258
|
+
return if ignored_path?(dir)
|
259
|
+
|
260
|
+
basename = File.basename(dir)
|
261
|
+
return if hidden?(basename)
|
262
|
+
|
263
|
+
paths << [basename, abspath] unless collapse?(dir)
|
264
|
+
end
|
265
|
+
|
266
|
+
return unless root_namespace
|
267
|
+
|
268
|
+
if paths.empty?
|
269
|
+
real_mod_name(root_namespace)
|
270
|
+
else
|
271
|
+
cnames = paths.reverse_each.map { |b, a| cname_for(b, a) }
|
272
|
+
|
273
|
+
if root_namespace == Object
|
274
|
+
cnames.join("::")
|
275
|
+
else
|
276
|
+
"#{real_mod_name(root_namespace)}::#{cnames.join("::")}"
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
231
281
|
# Says if the given constant path would be unloaded on reload. This
|
232
282
|
# predicate returns `false` if reloading is disabled.
|
233
283
|
#
|
@@ -357,36 +407,17 @@ module Zeitwerk
|
|
357
407
|
end
|
358
408
|
|
359
409
|
# @sig (String, Module) -> void
|
360
|
-
private def
|
410
|
+
private def define_autoloads_for_dir(dir, parent)
|
361
411
|
ls(dir) do |basename, abspath|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
412
|
+
if ruby?(basename)
|
413
|
+
basename.delete_suffix!(".rb")
|
414
|
+
autoload_file(parent, cname_for(basename, abspath), abspath)
|
415
|
+
else
|
416
|
+
if collapse?(abspath)
|
417
|
+
define_autoloads_for_dir(abspath, parent)
|
367
418
|
else
|
368
|
-
|
369
|
-
set_autoloads_in_dir(abspath, parent)
|
370
|
-
else
|
371
|
-
cname = inflector.camelize(basename, abspath).to_sym
|
372
|
-
autoload_subdir(parent, cname, abspath)
|
373
|
-
end
|
419
|
+
autoload_subdir(parent, cname_for(basename, abspath), abspath)
|
374
420
|
end
|
375
|
-
rescue ::NameError => error
|
376
|
-
path_type = ruby?(abspath) ? "file" : "directory"
|
377
|
-
|
378
|
-
raise NameError.new(<<~MESSAGE, error.name)
|
379
|
-
#{error.message} inferred by #{inflector.class} from #{path_type}
|
380
|
-
|
381
|
-
#{abspath}
|
382
|
-
|
383
|
-
Possible ways to address this:
|
384
|
-
|
385
|
-
* Tell Zeitwerk to ignore this particular #{path_type}.
|
386
|
-
* Tell Zeitwerk to ignore one of its parent directories.
|
387
|
-
* Rename the #{path_type} to comply with the naming conventions.
|
388
|
-
* Modify the inflector to handle this case.
|
389
|
-
MESSAGE
|
390
421
|
end
|
391
422
|
end
|
392
423
|
end
|
@@ -412,12 +443,12 @@ module Zeitwerk
|
|
412
443
|
elsif !cdef?(parent, cname)
|
413
444
|
# First time we find this namespace, set an autoload for it.
|
414
445
|
namespace_dirs[cpath(parent, cname)] << subdir
|
415
|
-
|
446
|
+
define_autoload(parent, cname, subdir)
|
416
447
|
else
|
417
448
|
# For whatever reason the constant that corresponds to this namespace has
|
418
449
|
# already been defined, we have to recurse.
|
419
450
|
log("the namespace #{cpath(parent, cname)} already exists, descending into #{subdir}") if logger
|
420
|
-
|
451
|
+
define_autoloads_for_dir(subdir, cget(parent, cname))
|
421
452
|
end
|
422
453
|
end
|
423
454
|
|
@@ -440,7 +471,7 @@ module Zeitwerk
|
|
440
471
|
shadowed_files << file
|
441
472
|
log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger
|
442
473
|
else
|
443
|
-
|
474
|
+
define_autoload(parent, cname, file)
|
444
475
|
end
|
445
476
|
end
|
446
477
|
|
@@ -454,12 +485,12 @@ module Zeitwerk
|
|
454
485
|
|
455
486
|
log("earlier autoload for #{cpath(parent, cname)} discarded, it is actually an explicit namespace defined in #{file}") if logger
|
456
487
|
|
457
|
-
|
488
|
+
define_autoload(parent, cname, file)
|
458
489
|
register_explicit_namespace(cpath(parent, cname))
|
459
490
|
end
|
460
491
|
|
461
492
|
# @sig (Module, Symbol, String) -> void
|
462
|
-
private def
|
493
|
+
private def define_autoload(parent, cname, abspath)
|
463
494
|
parent.autoload(cname, abspath)
|
464
495
|
|
465
496
|
if logger
|
@@ -511,7 +542,6 @@ module Zeitwerk
|
|
511
542
|
raise Error,
|
512
543
|
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
|
513
544
|
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
514
|
-
EOS
|
515
545
|
end
|
516
546
|
end
|
517
547
|
end
|
data/lib/zeitwerk/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zeitwerk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.
|
4
|
+
version: 2.6.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Noria
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Zeitwerk implements constant autoloading with Ruby semantics. Each gem
|
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
|
-
rubygems_version: 3.4.
|
64
|
+
rubygems_version: 3.4.16
|
65
65
|
signing_key:
|
66
66
|
specification_version: 4
|
67
67
|
summary: Efficient and thread-safe constant autoloader
|