zeitwerk 2.7.3 → 2.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +145 -72
- data/lib/zeitwerk/core_ext/kernel.rb +1 -1
- data/lib/zeitwerk/cref/map.rb +3 -3
- data/lib/zeitwerk/cref.rb +8 -1
- data/lib/zeitwerk/error.rb +12 -1
- data/lib/zeitwerk/gem_inflector.rb +3 -3
- data/lib/zeitwerk/gem_loader.rb +6 -6
- data/lib/zeitwerk/inflector.rb +8 -8
- data/lib/zeitwerk/loader/callbacks.rb +4 -4
- data/lib/zeitwerk/loader/config.rb +96 -33
- data/lib/zeitwerk/loader/constant_path_validator.rb +17 -0
- data/lib/zeitwerk/loader/eager_load.rb +44 -48
- data/lib/zeitwerk/loader/file_system.rb +212 -0
- data/lib/zeitwerk/loader/helpers.rb +3 -105
- data/lib/zeitwerk/loader.rb +200 -163
- data/lib/zeitwerk/real_mod_name.rb +1 -1
- data/lib/zeitwerk/registry.rb +30 -4
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +13 -13
- metadata +5 -3
data/lib/zeitwerk/loader.rb
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'monitor'
|
|
4
|
+
require 'set'
|
|
5
5
|
|
|
6
6
|
module Zeitwerk
|
|
7
7
|
class Loader
|
|
8
|
-
require_relative
|
|
9
|
-
require_relative
|
|
10
|
-
require_relative
|
|
11
|
-
require_relative
|
|
8
|
+
require_relative 'loader/helpers'
|
|
9
|
+
require_relative 'loader/callbacks'
|
|
10
|
+
require_relative 'loader/config'
|
|
11
|
+
require_relative 'loader/eager_load'
|
|
12
|
+
require_relative 'loader/file_system'
|
|
13
|
+
require_relative 'loader/constant_path_validator'
|
|
12
14
|
|
|
13
15
|
extend Internal
|
|
14
16
|
|
|
@@ -18,14 +20,11 @@ module Zeitwerk
|
|
|
18
20
|
include Config
|
|
19
21
|
include EagerLoad
|
|
20
22
|
|
|
21
|
-
MUTEX = Mutex.new #: Mutex
|
|
22
|
-
private_constant :MUTEX
|
|
23
|
-
|
|
24
23
|
# Maps absolute paths for which an autoload has been set ---and not
|
|
25
24
|
# executed--- to their corresponding Zeitwerk::Cref object.
|
|
26
25
|
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
26
|
+
# '/Users/fxn/blog/app/models/user.rb' => #<Zeitwerk::Cref:... @mod=Object, @cname=:User, ...>,
|
|
27
|
+
# '/Users/fxn/blog/app/models/hotel/pricing.rb' => #<Zeitwerk::Cref:... @mod=Hotel, @cname=:Pricing, ...>,
|
|
29
28
|
# ...
|
|
30
29
|
#
|
|
31
30
|
#: Hash[String, Zeitwerk::Cref]
|
|
@@ -44,9 +43,9 @@ module Zeitwerk
|
|
|
44
43
|
# file is normally required and, while doing so, the loader sets an autoload
|
|
45
44
|
# on the gem namespace. That autoload hits this edge case.
|
|
46
45
|
#
|
|
47
|
-
# There is some logic that
|
|
48
|
-
#
|
|
49
|
-
#
|
|
46
|
+
# There is some logic that needs to know if an autoload for a given constant
|
|
47
|
+
# already exists. We check Module#autoload? first, and fallback to the
|
|
48
|
+
# inceptions just in case.
|
|
50
49
|
#
|
|
51
50
|
# This map keeps track of pairs (cref, autoload_path) found by the loader.
|
|
52
51
|
# The object Zeitwerk::Registry.inceptions, on the other hand, acts as a
|
|
@@ -104,6 +103,7 @@ module Zeitwerk
|
|
|
104
103
|
attr_reader :dirs_autoload_monitor
|
|
105
104
|
private :dirs_autoload_monitor
|
|
106
105
|
|
|
106
|
+
#: () -> void
|
|
107
107
|
def initialize
|
|
108
108
|
super
|
|
109
109
|
|
|
@@ -115,6 +115,8 @@ module Zeitwerk
|
|
|
115
115
|
@shadowed_files = Set.new
|
|
116
116
|
@setup = false
|
|
117
117
|
@eager_loaded = false
|
|
118
|
+
@fs = FileSystem.new(self)
|
|
119
|
+
@cpv = ConstantPathValidator.new
|
|
118
120
|
|
|
119
121
|
@mutex = Mutex.new
|
|
120
122
|
@dirs_autoload_monitor = Monitor.new
|
|
@@ -130,7 +132,7 @@ module Zeitwerk
|
|
|
130
132
|
break if @setup
|
|
131
133
|
|
|
132
134
|
actual_roots.each do |root_dir, root_namespace|
|
|
133
|
-
define_autoloads_for_dir(root_dir, root_namespace)
|
|
135
|
+
define_autoloads_for_dir(root_dir, root_namespace, external: true)
|
|
134
136
|
end
|
|
135
137
|
|
|
136
138
|
on_setup_callbacks.each(&:call)
|
|
@@ -154,73 +156,79 @@ module Zeitwerk
|
|
|
154
156
|
def unload
|
|
155
157
|
mutex.synchronize do
|
|
156
158
|
raise SetupRequired unless @setup
|
|
159
|
+
__unload
|
|
160
|
+
end
|
|
161
|
+
end
|
|
157
162
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
# This is an internal method.
|
|
164
|
+
#
|
|
165
|
+
#: () -> void
|
|
166
|
+
def __unload
|
|
167
|
+
# We are going to keep track of the files that were required by our
|
|
168
|
+
# autoloads to later remove them from $LOADED_FEATURES, thus making them
|
|
169
|
+
# loadable by Kernel#require again.
|
|
170
|
+
#
|
|
171
|
+
# Directories are not stored in $LOADED_FEATURES, keeping track of files
|
|
172
|
+
# is enough.
|
|
173
|
+
unloaded_files = Set.new
|
|
165
174
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
end
|
|
175
|
+
autoloads.each do |abspath, cref|
|
|
176
|
+
if cref.autoload?
|
|
177
|
+
unload_autoload(cref)
|
|
178
|
+
else
|
|
179
|
+
# Could happen if loaded with require_relative. That is unsupported,
|
|
180
|
+
# and the constant path would escape unloadable_cpath? This is just
|
|
181
|
+
# defensive code to clean things up as much as we are able to.
|
|
182
|
+
unload_cref(cref)
|
|
183
|
+
unloaded_files.add(abspath) if @fs.rb_extension?(abspath)
|
|
176
184
|
end
|
|
185
|
+
end
|
|
177
186
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
end
|
|
187
|
+
to_unload.each do |abspath, cref|
|
|
188
|
+
unless on_unload_callbacks.empty?
|
|
189
|
+
begin
|
|
190
|
+
value = cref.get
|
|
191
|
+
rescue ::NameError
|
|
192
|
+
# Perhaps the user deleted the constant by hand, or perhaps an
|
|
193
|
+
# autoload failed to define the expected constant but the user
|
|
194
|
+
# rescued the exception.
|
|
195
|
+
else
|
|
196
|
+
run_on_unload_callbacks(cref, value, abspath)
|
|
189
197
|
end
|
|
190
|
-
|
|
191
|
-
unload_cref(cref)
|
|
192
|
-
unloaded_files.add(abspath) if ruby?(abspath)
|
|
193
198
|
end
|
|
194
199
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
#
|
|
199
|
-
# To make it aware of changes, the gem defines singleton methods in
|
|
200
|
-
# $LOADED_FEATURES:
|
|
201
|
-
#
|
|
202
|
-
# https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb
|
|
203
|
-
#
|
|
204
|
-
# Rails applications may depend on bootsnap, so for unloading to work
|
|
205
|
-
# in that setting it is preferable that we restrict our API choice to
|
|
206
|
-
# one of those methods.
|
|
207
|
-
$LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) }
|
|
208
|
-
end
|
|
200
|
+
unload_cref(cref)
|
|
201
|
+
unloaded_files.add(abspath) if @fs.rb_extension?(abspath)
|
|
202
|
+
end
|
|
209
203
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
204
|
+
unless unloaded_files.empty?
|
|
205
|
+
# Bootsnap decorates Kernel#require to speed it up using a cache and
|
|
206
|
+
# this optimization does not check if $LOADED_FEATURES has the file.
|
|
207
|
+
#
|
|
208
|
+
# To make it aware of changes, the gem defines singleton methods in
|
|
209
|
+
# $LOADED_FEATURES:
|
|
210
|
+
#
|
|
211
|
+
# https://github.com/rails/bootsnap/blob/main/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb
|
|
212
|
+
#
|
|
213
|
+
# Rails applications may depend on bootsnap, so for unloading to work
|
|
214
|
+
# in that setting it is preferable that we restrict our API choice to
|
|
215
|
+
# one of those methods.
|
|
216
|
+
$LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) }
|
|
217
|
+
end
|
|
215
218
|
|
|
216
|
-
|
|
217
|
-
|
|
219
|
+
autoloads.clear
|
|
220
|
+
autoloaded_dirs.clear
|
|
221
|
+
to_unload.clear
|
|
222
|
+
namespace_dirs.clear
|
|
223
|
+
shadowed_files.clear
|
|
218
224
|
|
|
219
|
-
|
|
225
|
+
unregister_inceptions
|
|
226
|
+
unregister_explicit_namespaces
|
|
220
227
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
228
|
+
Registry.autoloads.unregister_loader(self)
|
|
229
|
+
|
|
230
|
+
@setup = false
|
|
231
|
+
@eager_loaded = false
|
|
224
232
|
end
|
|
225
233
|
|
|
226
234
|
# Unloads all loaded code, and calls setup again so that the loader is able
|
|
@@ -235,8 +243,11 @@ module Zeitwerk
|
|
|
235
243
|
raise SetupRequired unless @setup
|
|
236
244
|
|
|
237
245
|
unload
|
|
246
|
+
|
|
238
247
|
recompute_ignored_paths
|
|
239
248
|
recompute_collapse_dirs
|
|
249
|
+
recompute_collapse_parents
|
|
250
|
+
|
|
240
251
|
setup
|
|
241
252
|
end
|
|
242
253
|
|
|
@@ -253,18 +264,20 @@ module Zeitwerk
|
|
|
253
264
|
while (dir, cpath = queue.shift)
|
|
254
265
|
result[dir] = cpath
|
|
255
266
|
|
|
256
|
-
prefix = cpath ==
|
|
267
|
+
prefix = cpath == 'Object' ? '' : cpath + '::'
|
|
257
268
|
|
|
258
|
-
ls(dir) do |basename, abspath, ftype|
|
|
269
|
+
@fs.ls(dir, collapse: false) do |basename, abspath, ftype|
|
|
259
270
|
if ftype == :file
|
|
260
|
-
basename
|
|
261
|
-
|
|
262
|
-
else
|
|
263
|
-
if collapse?(abspath)
|
|
264
|
-
queue << [abspath, cpath]
|
|
271
|
+
if basename == @nsfile
|
|
272
|
+
result[abspath] = cpath
|
|
265
273
|
else
|
|
266
|
-
|
|
274
|
+
basename.delete_suffix!('.rb')
|
|
275
|
+
result[abspath] = "#{prefix}#{cname_for(basename, abspath)}"
|
|
267
276
|
end
|
|
277
|
+
elsif collapse?(abspath)
|
|
278
|
+
queue.unshift([abspath, cpath])
|
|
279
|
+
else
|
|
280
|
+
queue.push([abspath, "#{prefix}#{cname_for(basename, abspath)}"])
|
|
268
281
|
end
|
|
269
282
|
end
|
|
270
283
|
end
|
|
@@ -279,16 +292,18 @@ module Zeitwerk
|
|
|
279
292
|
|
|
280
293
|
raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)
|
|
281
294
|
|
|
282
|
-
|
|
295
|
+
ftype = @fs.supported_ftype?(abspath)
|
|
296
|
+
return unless ftype
|
|
297
|
+
|
|
283
298
|
return if ignored_path?(abspath)
|
|
284
299
|
|
|
285
300
|
paths = []
|
|
286
301
|
|
|
287
|
-
if
|
|
288
|
-
basename = File.basename(abspath
|
|
289
|
-
return if hidden?(basename)
|
|
302
|
+
if ftype == :file
|
|
303
|
+
basename = File.basename(abspath)
|
|
304
|
+
return if @fs.hidden?(basename)
|
|
290
305
|
|
|
291
|
-
paths << [basename, abspath]
|
|
306
|
+
paths << [basename.delete_suffix('.rb'), abspath] unless basename == @nsfile
|
|
292
307
|
walk_up_from = File.dirname(abspath)
|
|
293
308
|
else
|
|
294
309
|
walk_up_from = abspath
|
|
@@ -296,12 +311,12 @@ module Zeitwerk
|
|
|
296
311
|
|
|
297
312
|
root_namespace = nil
|
|
298
313
|
|
|
299
|
-
walk_up(walk_up_from) do |dir|
|
|
314
|
+
@fs.walk_up(walk_up_from) do |dir|
|
|
300
315
|
break if root_namespace = roots[dir]
|
|
301
316
|
return if ignored_path?(dir)
|
|
302
317
|
|
|
303
318
|
basename = File.basename(dir)
|
|
304
|
-
return if hidden?(basename)
|
|
319
|
+
return if @fs.hidden?(basename)
|
|
305
320
|
|
|
306
321
|
paths << [basename, dir] unless collapse?(dir)
|
|
307
322
|
end
|
|
@@ -314,9 +329,9 @@ module Zeitwerk
|
|
|
314
329
|
cnames = paths.reverse_each.map { cname_for(_1, _2) }
|
|
315
330
|
|
|
316
331
|
if root_namespace == Object
|
|
317
|
-
cnames.join(
|
|
332
|
+
cnames.join('::')
|
|
318
333
|
else
|
|
319
|
-
"#{real_mod_name(root_namespace)}::#{cnames.join(
|
|
334
|
+
"#{real_mod_name(root_namespace)}::#{cnames.join('::')}"
|
|
320
335
|
end
|
|
321
336
|
end
|
|
322
337
|
end
|
|
@@ -363,6 +378,16 @@ module Zeitwerk
|
|
|
363
378
|
shadowed_files.member?(file)
|
|
364
379
|
end
|
|
365
380
|
|
|
381
|
+
#: { () -> String } -> void
|
|
382
|
+
internal def log
|
|
383
|
+
return unless logger
|
|
384
|
+
|
|
385
|
+
message = yield
|
|
386
|
+
method_name = logger.respond_to?(:debug) ? :debug : :call
|
|
387
|
+
logger.send(method_name, "Zeitwerk@#{tag}: #{message}")
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
|
|
366
391
|
# --- Class methods ---------------------------------------------------------------------------
|
|
367
392
|
|
|
368
393
|
class << self
|
|
@@ -373,10 +398,10 @@ module Zeitwerk
|
|
|
373
398
|
|
|
374
399
|
# This is a shortcut for
|
|
375
400
|
#
|
|
376
|
-
# require
|
|
401
|
+
# require 'zeitwerk'
|
|
377
402
|
#
|
|
378
403
|
# loader = Zeitwerk::Loader.new
|
|
379
|
-
# loader.tag = File.basename(__FILE__,
|
|
404
|
+
# loader.tag = File.basename(__FILE__, '.rb')
|
|
380
405
|
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
|
381
406
|
# loader.push_dir(__dir__)
|
|
382
407
|
#
|
|
@@ -394,10 +419,10 @@ module Zeitwerk
|
|
|
394
419
|
|
|
395
420
|
# This is a shortcut for
|
|
396
421
|
#
|
|
397
|
-
# require
|
|
422
|
+
# require 'zeitwerk'
|
|
398
423
|
#
|
|
399
424
|
# loader = Zeitwerk::Loader.new
|
|
400
|
-
# loader.tag = namespace.name +
|
|
425
|
+
# loader.tag = namespace.name + '-' + File.basename(__FILE__, '.rb')
|
|
401
426
|
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
|
402
427
|
# loader.push_dir(__dir__, namespace: namespace)
|
|
403
428
|
#
|
|
@@ -414,7 +439,7 @@ module Zeitwerk
|
|
|
414
439
|
end
|
|
415
440
|
|
|
416
441
|
unless real_mod_name(namespace)
|
|
417
|
-
raise Zeitwerk::Error,
|
|
442
|
+
raise Zeitwerk::Error, 'extending anonymous namespaces is unsupported'
|
|
418
443
|
end
|
|
419
444
|
|
|
420
445
|
called_from = caller_locations(1, 1).first.path
|
|
@@ -462,68 +487,94 @@ module Zeitwerk
|
|
|
462
487
|
end
|
|
463
488
|
end
|
|
464
489
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
490
|
+
# Scans `dir` and sets autoloads in `mod` for the constants its contents are
|
|
491
|
+
# expected to define.
|
|
492
|
+
#
|
|
493
|
+
# The `external` flag indicates whether `mod` has been externally defined,
|
|
494
|
+
# as is the case with root namespaces or reopened third-party namespaces.
|
|
495
|
+
#
|
|
496
|
+
#: (String, Module, external: boolish) -> void
|
|
497
|
+
private def define_autoloads_for_dir(dir, mod, external:)
|
|
498
|
+
@fs.ls(dir) do |basename, abspath, ftype|
|
|
468
499
|
if ftype == :file
|
|
469
|
-
basename
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
autoload_subdir(cref, abspath)
|
|
500
|
+
if basename == @nsfile
|
|
501
|
+
if external
|
|
502
|
+
cpath = real_mod_name(mod)
|
|
503
|
+
location = Object.const_source_location(cpath)&.join(':')
|
|
504
|
+
location = nil if location&.empty?
|
|
505
|
+
raise Zeitwerk::ConflictingNamespaceDefinitionError.new(cpath, location: location, conflicting_file: abspath)
|
|
506
|
+
end
|
|
507
|
+
next # Pass if this is a managed namespace, the nsfile was already probed when visiting the parent directory.
|
|
478
508
|
end
|
|
509
|
+
|
|
510
|
+
basename.delete_suffix!('.rb')
|
|
511
|
+
cref = Cref.new(mod, cname_for(basename, abspath))
|
|
512
|
+
visit_file(cref, abspath)
|
|
513
|
+
else
|
|
514
|
+
cref = Cref.new(mod, cname_for(basename, abspath))
|
|
515
|
+
visit_subdir(cref, abspath, external:)
|
|
479
516
|
end
|
|
480
517
|
end
|
|
481
518
|
end
|
|
482
519
|
|
|
483
520
|
#: (Zeitwerk::Cref, String) -> void
|
|
484
|
-
private def
|
|
521
|
+
private def visit_file(cref, file)
|
|
522
|
+
if autoload_path = cref.autoload? || Registry.inceptions.registered?(cref)
|
|
523
|
+
if @fs.rb_extension?(autoload_path)
|
|
524
|
+
if File.basename(autoload_path) == @nsfile && autoload_path_set_by_me_for?(cref)
|
|
525
|
+
raise Zeitwerk::ConflictingNamespaceDefinitionError.new(cref.path, location: autoload_path, conflicting_file: file)
|
|
526
|
+
end
|
|
527
|
+
shadowed_files << file
|
|
528
|
+
log { "file #{file} is ignored because #{autoload_path} has precedence" }
|
|
529
|
+
else
|
|
530
|
+
promote_namespace_from_implicit_to_explicit(dir: autoload_path, file: file, cref: cref)
|
|
531
|
+
end
|
|
532
|
+
elsif cref.defined?
|
|
533
|
+
shadowed_files << file
|
|
534
|
+
if location = cref.location
|
|
535
|
+
log { "file #{file} is ignored because #{cref} is already defined in #{location}" }
|
|
536
|
+
else
|
|
537
|
+
log { "file #{file} is ignored because #{cref} is already defined (unknown location)" }
|
|
538
|
+
end
|
|
539
|
+
else
|
|
540
|
+
define_autoload(cref, file)
|
|
541
|
+
end
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
#: (Zeitwerk::Cref, String, external: boolish) -> void
|
|
545
|
+
private def visit_subdir(cref, subdir, external:)
|
|
485
546
|
if autoload_path = autoload_path_set_by_me_for?(cref)
|
|
486
|
-
if
|
|
547
|
+
if @fs.rb_extension?(autoload_path)
|
|
548
|
+
# The namespace that corresponds to this subdirectory is defined in a
|
|
549
|
+
# file, either regular or nsfile. Therefore, a nsfile would be a
|
|
550
|
+
# duplication.
|
|
551
|
+
if nsfile_abspath = @fs.has_exactly_one_nsfile?(cref, subdir)
|
|
552
|
+
raise Zeitwerk::ConflictingNamespaceDefinitionError.new(cref.path, location: autoload_path, conflicting_file: nsfile_abspath)
|
|
553
|
+
end
|
|
487
554
|
# Scanning visited a Ruby file first, and now a directory for the same
|
|
488
|
-
# constant has been found. This
|
|
489
|
-
# namespace whose definition was seen first.
|
|
555
|
+
# constant has been found. This is an explicit namespace.
|
|
490
556
|
#
|
|
491
|
-
#
|
|
492
|
-
#
|
|
493
|
-
# on, no big deal.
|
|
557
|
+
# The namespace may be spread over multiple directories and perhaps it
|
|
558
|
+
# was already registered, but registering is idempotent, just do it.
|
|
494
559
|
register_explicit_namespace(cref)
|
|
560
|
+
elsif nsfile_abspath = @fs.has_exactly_one_nsfile?(cref, subdir)
|
|
561
|
+
# Scanning found a matching directory first, and now we saw a nsfile.
|
|
562
|
+
promote_namespace_from_implicit_to_explicit(dir: subdir, file: nsfile_abspath, cref: cref)
|
|
495
563
|
end
|
|
496
|
-
# If the existing autoload points to a file, it has to be preserved, if
|
|
497
|
-
# not, it is fine as it is. In either case, we do not need to override.
|
|
498
|
-
# Just remember the subdirectory conforms this namespace.
|
|
499
564
|
namespace_dirs.get_or_set(cref) { [] } << subdir
|
|
500
565
|
elsif !cref.defined?
|
|
501
|
-
|
|
566
|
+
if nsfile_abspath = @fs.has_exactly_one_nsfile?(cref, subdir)
|
|
567
|
+
define_autoload(cref, nsfile_abspath)
|
|
568
|
+
register_explicit_namespace(cref)
|
|
569
|
+
else
|
|
570
|
+
define_autoload(cref, subdir)
|
|
571
|
+
end
|
|
502
572
|
namespace_dirs.get_or_set(cref) { [] } << subdir
|
|
503
|
-
define_autoload(cref, subdir)
|
|
504
573
|
else
|
|
505
574
|
# For whatever reason the constant that corresponds to this namespace has
|
|
506
575
|
# already been defined, we have to recurse.
|
|
507
|
-
log
|
|
508
|
-
define_autoloads_for_dir(subdir, cref.get)
|
|
509
|
-
end
|
|
510
|
-
end
|
|
511
|
-
|
|
512
|
-
#: (Zeitwerk::Cref, String) -> void
|
|
513
|
-
private def autoload_file(cref, file)
|
|
514
|
-
if autoload_path = cref.autoload? || Registry.inceptions.registered?(cref)
|
|
515
|
-
# First autoload for a Ruby file wins, just ignore subsequent ones.
|
|
516
|
-
if ruby?(autoload_path)
|
|
517
|
-
shadowed_files << file
|
|
518
|
-
log("file #{file} is ignored because #{autoload_path} has precedence") if logger
|
|
519
|
-
else
|
|
520
|
-
promote_namespace_from_implicit_to_explicit(dir: autoload_path, file: file, cref: cref)
|
|
521
|
-
end
|
|
522
|
-
elsif cref.defined?
|
|
523
|
-
shadowed_files << file
|
|
524
|
-
log("file #{file} is ignored because #{cref} is already defined") if logger
|
|
525
|
-
else
|
|
526
|
-
define_autoload(cref, file)
|
|
576
|
+
log { "the namespace #{cref} already exists, descending into #{subdir}" }
|
|
577
|
+
define_autoloads_for_dir(subdir, cref.get, external:)
|
|
527
578
|
end
|
|
528
579
|
end
|
|
529
580
|
|
|
@@ -535,7 +586,7 @@ module Zeitwerk
|
|
|
535
586
|
autoloads.delete(dir)
|
|
536
587
|
Registry.autoloads.unregister(dir)
|
|
537
588
|
|
|
538
|
-
log
|
|
589
|
+
log { "earlier autoload for #{cref} discarded, it is actually an explicit namespace defined in #{file}" }
|
|
539
590
|
|
|
540
591
|
# Order matters: When Module#const_added is triggered by the autoload, we
|
|
541
592
|
# don't want the namespace to be registered yet.
|
|
@@ -548,10 +599,10 @@ module Zeitwerk
|
|
|
548
599
|
cref.autoload(abspath)
|
|
549
600
|
|
|
550
601
|
if logger
|
|
551
|
-
if
|
|
552
|
-
log
|
|
602
|
+
if @fs.rb_extension?(abspath)
|
|
603
|
+
log { "autoload set for #{cref}, to be loaded from #{abspath}" }
|
|
553
604
|
else
|
|
554
|
-
log
|
|
605
|
+
log { "autoload set for #{cref}, to be autovivified from #{abspath}" }
|
|
555
606
|
end
|
|
556
607
|
end
|
|
557
608
|
|
|
@@ -595,26 +646,12 @@ module Zeitwerk
|
|
|
595
646
|
end
|
|
596
647
|
|
|
597
648
|
#: (String) -> void
|
|
598
|
-
private def
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
next if loader.__ignores?(dir)
|
|
605
|
-
|
|
606
|
-
loader.__roots.each_key do |root_dir|
|
|
607
|
-
next if ignores?(root_dir)
|
|
608
|
-
|
|
609
|
-
root_dir_slash = root_dir + "/"
|
|
610
|
-
if dir_slash.start_with?(root_dir_slash) || root_dir_slash.start_with?(dir_slash)
|
|
611
|
-
require "pp" # Needed for pretty_inspect, even in Ruby 2.5.
|
|
612
|
-
raise Error,
|
|
613
|
-
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
|
|
614
|
-
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
|
615
|
-
end
|
|
616
|
-
end
|
|
617
|
-
end
|
|
649
|
+
private def raise_if_conflicting_root_dir(root_dir)
|
|
650
|
+
if loader = Registry.conflicting_root_dir?(self, root_dir)
|
|
651
|
+
require 'pp' # Needed to have pretty_inspect available.
|
|
652
|
+
raise Error,
|
|
653
|
+
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{root_dir}," \
|
|
654
|
+
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
|
618
655
|
end
|
|
619
656
|
end
|
|
620
657
|
|
|
@@ -628,7 +665,7 @@ module Zeitwerk
|
|
|
628
665
|
#: (Zeitwerk::Cref) -> void
|
|
629
666
|
private def unload_autoload(cref)
|
|
630
667
|
cref.remove
|
|
631
|
-
log
|
|
668
|
+
log { "autoload for #{cref} removed" }
|
|
632
669
|
end
|
|
633
670
|
|
|
634
671
|
#: (Zeitwerk::Cref) -> void
|
|
@@ -640,7 +677,7 @@ module Zeitwerk
|
|
|
640
677
|
# There are a few edge scenarios in which this may happen. If the constant
|
|
641
678
|
# is gone, that is OK, anyway.
|
|
642
679
|
else
|
|
643
|
-
log
|
|
680
|
+
log { "#{cref} unloaded" }
|
|
644
681
|
end
|
|
645
682
|
end
|
|
646
683
|
end
|
|
@@ -7,7 +7,7 @@ module Zeitwerk::RealModName
|
|
|
7
7
|
|
|
8
8
|
# Returns the real name of the class or module.
|
|
9
9
|
#
|
|
10
|
-
# We need this indirection
|
|
10
|
+
# We need this indirection because the `name` method can be overridden, and
|
|
11
11
|
# because in practice what we really need is the constant paths of modules
|
|
12
12
|
# with a permanent name, not so much what the user considers to be the name of
|
|
13
13
|
# a certain class or module of theirs.
|
data/lib/zeitwerk/registry.rb
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module Zeitwerk
|
|
4
4
|
module Registry # :nodoc: all
|
|
5
|
-
require_relative
|
|
6
|
-
require_relative
|
|
7
|
-
require_relative
|
|
8
|
-
require_relative
|
|
5
|
+
require_relative 'registry/autoloads'
|
|
6
|
+
require_relative 'registry/explicit_namespaces'
|
|
7
|
+
require_relative 'registry/inceptions'
|
|
8
|
+
require_relative 'registry/loaders'
|
|
9
9
|
|
|
10
10
|
class << self
|
|
11
11
|
# Keeps track of all loaders. Useful to broadcast messages and to prevent
|
|
@@ -44,6 +44,31 @@ module Zeitwerk
|
|
|
44
44
|
gem_loaders_by_root_file.delete_if { |_, l| l == loader }
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
+
#: (Zeitwerk::Loader, String) -> Zeitwerk::Loader?
|
|
48
|
+
def conflicting_root_dir?(loader, new_root_dir)
|
|
49
|
+
@mutex.synchronize do
|
|
50
|
+
loaders.each do |existing_loader|
|
|
51
|
+
next if existing_loader == loader
|
|
52
|
+
|
|
53
|
+
existing_loader.__roots.each_key do |existing_root_dir|
|
|
54
|
+
# Conflicting directories are rare, optimize for the common case.
|
|
55
|
+
next if !new_root_dir.start_with?(existing_root_dir) && !existing_root_dir.start_with?(new_root_dir)
|
|
56
|
+
|
|
57
|
+
new_root_dir_slash = new_root_dir + '/'
|
|
58
|
+
existing_root_dir_slash = existing_root_dir + '/'
|
|
59
|
+
next if !new_root_dir_slash.start_with?(existing_root_dir_slash) && !existing_root_dir_slash.start_with?(new_root_dir_slash)
|
|
60
|
+
|
|
61
|
+
next if loader.__ignores?(existing_root_dir)
|
|
62
|
+
break if existing_loader.__ignores?(new_root_dir)
|
|
63
|
+
|
|
64
|
+
return existing_loader
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
nil
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
47
72
|
# This method returns always a loader, the same instance for the same root
|
|
48
73
|
# file. That is how Zeitwerk::Loader.for_gem is idempotent.
|
|
49
74
|
#
|
|
@@ -59,5 +84,6 @@ module Zeitwerk
|
|
|
59
84
|
@autoloads = Autoloads.new
|
|
60
85
|
@explicit_namespaces = ExplicitNamespaces.new
|
|
61
86
|
@inceptions = Inceptions.new
|
|
87
|
+
@mutex = Mutex.new
|
|
62
88
|
end
|
|
63
89
|
end
|
data/lib/zeitwerk/version.rb
CHANGED
data/lib/zeitwerk.rb
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Zeitwerk
|
|
4
|
-
require_relative
|
|
5
|
-
require_relative
|
|
6
|
-
require_relative
|
|
7
|
-
require_relative
|
|
8
|
-
require_relative
|
|
9
|
-
require_relative
|
|
10
|
-
require_relative
|
|
11
|
-
require_relative
|
|
12
|
-
require_relative
|
|
13
|
-
require_relative
|
|
14
|
-
require_relative
|
|
4
|
+
require_relative 'zeitwerk/real_mod_name'
|
|
5
|
+
require_relative 'zeitwerk/internal'
|
|
6
|
+
require_relative 'zeitwerk/cref'
|
|
7
|
+
require_relative 'zeitwerk/loader'
|
|
8
|
+
require_relative 'zeitwerk/gem_loader'
|
|
9
|
+
require_relative 'zeitwerk/registry'
|
|
10
|
+
require_relative 'zeitwerk/inflector'
|
|
11
|
+
require_relative 'zeitwerk/gem_inflector'
|
|
12
|
+
require_relative 'zeitwerk/null_inflector'
|
|
13
|
+
require_relative 'zeitwerk/error'
|
|
14
|
+
require_relative 'zeitwerk/version'
|
|
15
15
|
|
|
16
|
-
require_relative
|
|
17
|
-
require_relative
|
|
16
|
+
require_relative 'zeitwerk/core_ext/kernel'
|
|
17
|
+
require_relative 'zeitwerk/core_ext/module'
|
|
18
18
|
|
|
19
19
|
# This is a dangerous method.
|
|
20
20
|
#
|