zeitwerk 2.6.0 → 2.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7c15f85768f681d78dd0a96b23e5bb92fe8955fe74d9ae0cefeb1b0a55b76f1
4
- data.tar.gz: 212a4b236b7d67c9413bd583bef540c0c9c090f5d0457af38b0681405bba3244
3
+ metadata.gz: 220865ab50f0336d05c8b907a08a94a8bf92843ba688c1a9686163a190b0754e
4
+ data.tar.gz: a9d62662351c60a3b264a68726a84c41fc4b6f92eaae855fb4887e3eb4cf14a1
5
5
  SHA512:
6
- metadata.gz: 4feeb02e4ec695895035a39d1a46d45bd8eb77a1d72e732c28387ff16f4c3e2ac10735195fa246179cfce2fe072ada05c0f661b599fd4bc44763d300e485b12d
7
- data.tar.gz: 0bd284cdfd10b1f27c7ad31ecd79c7bce9d76be46d3644941259ac7ee22ed425baac0cbcc340653547c873aecca6d5833c908e3009f58c4f0376be5402c9645b
6
+ metadata.gz: b802eaabb27e6268eafa8d35d4402819551203c5fd2a1692d9a9bae65075bf668f5aecfaf3fa6c763987796b60fd5bde4704aebda4a8bfa1c74a526c27264ab5
7
+ data.tar.gz: c4a53a60b49e5cb83aee148271b26fee8de15fd072327e52363438b0531d9953961305375ec727cc3a0ff2c021254eb081056350165922bd9f6f8b1e1b3e96f3
data/README.md CHANGED
@@ -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,9 +50,11 @@
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)
57
+ - [Introspection](#introspection)
50
58
  - [Encodings](#encodings)
51
59
  - [Rules of thumb](#rules-of-thumb)
52
60
  - [Debuggers](#debuggers)
@@ -205,7 +213,7 @@ serializers/user_serializer.rb -> UserSerializer
205
213
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
206
214
  #### Custom root namespaces
207
215
 
208
- 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.
216
+ 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.
209
217
 
210
218
  For example, given:
211
219
 
@@ -249,7 +257,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
249
257
 
250
258
  and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
251
259
 
252
- For this to happen, the directory has to contain non-ignored Ruby files, directly or recursively, otherwise it is ignored. This condition is evaluated again on reloads.
260
+ For this to happen, the directory has to contain non-ignored Ruby files with extension `.rb`, directly or recursively, otherwise it is ignored. This condition is evaluated again on reloads.
253
261
 
254
262
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
255
263
  ### Explicit namespaces
@@ -441,6 +449,8 @@ In gems, the method needs to be invoked after the main namespace has been define
441
449
 
442
450
  Eager loading is synchronized and idempotent.
443
451
 
452
+ Attempting to eager load without previously calling `setup` raises `Zeitwerk::SetupRequired`.
453
+
444
454
  <a id="markdown-eager-load-exclusions" name="eager-load-exclusions"></a>
445
455
  #### Eager load exclusions
446
456
 
@@ -463,6 +473,81 @@ Which may be handy if the project eager loads in the test suite to [ensure proje
463
473
 
464
474
  The `force` flag does not affect ignored files and directories, those are still ignored.
465
475
 
476
+ <a id="markdown-eager-load-directories" name="eager-load-directories"></a>
477
+ #### Eager load directories
478
+
479
+ The method `Zeitwerk::Loader#eager_load_dir` eager loads a given directory, recursively:
480
+
481
+ ```ruby
482
+ loader.eager_load_dir("#{__dir__}/custom_web_app/routes")
483
+ ```
484
+
485
+ 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.
486
+
487
+ 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`.
488
+
489
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
490
+
491
+ `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.
492
+
493
+ The method checks if a regular eager load was already executed, in which case it returns fast.
494
+
495
+ Nested root directories which are descendants of the argument are skipped. Those subtrees are considered to be conceptually apart.
496
+
497
+ Attempting to eager load a directory without previously calling `setup` raises `Zeitwerk::SetupRequired`.
498
+
499
+ <a id="markdown-eager-load-namespaces" name="eager-load-namespaces"></a>
500
+ #### Eager load namespaces
501
+
502
+ The method `Zeitwerk::Loader#eager_load_namespace` eager loads a given namespace, recursively:
503
+
504
+ ```ruby
505
+ loader.eager_load_namespace(MyApp::Routes)
506
+ ```
507
+
508
+ 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.
509
+
510
+ The argument has to be a class or module object and the method raises `Zeitwerk::Error` otherwise.
511
+
512
+ 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
513
+
514
+ ```
515
+ root_dir1/my_app/routes
516
+ root_dir2/my_app/routes
517
+ root_dir3/my_app/routes
518
+ ```
519
+
520
+ where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
521
+
522
+ 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.
523
+
524
+ 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.
525
+
526
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
527
+
528
+ `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.
529
+
530
+ The method checks if a regular eager load was already executed, in which case it returns fast.
531
+
532
+ 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.
533
+
534
+ Attempting to eager load a namespace without previously calling `setup` raises `Zeitwerk::SetupRequired`.
535
+
536
+ <a id="markdown-eager-load-namespaces-shared-by-several-loaders" name="eager-load-namespaces-shared-by-several-loaders"></a>
537
+ #### Eager load namespaces shared by several loaders
538
+
539
+ The method `Zeitwerk::Loader.eager_load_namespace` broadcasts `eager_load_namespace` to all loaders.
540
+
541
+ ```ruby
542
+ Zeitwerk::Loader.eager_load_namespace(MyFramework::Routes)
543
+ ```
544
+
545
+ 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.
546
+
547
+ 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.
548
+
549
+ 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.
550
+
466
551
  <a id="markdown-global-eager-load" name="global-eager-load"></a>
467
552
  #### Global eager load
468
553
 
@@ -478,9 +563,31 @@ Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager lo
478
563
 
479
564
  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.
480
565
 
566
+ 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.
567
+
568
+ <a id="markdown-loading-individual-files" name="loading-individual-files"></a>
569
+ ### Loading individual files
570
+
571
+ The method `Zeitwerk::Loader#load_file` loads an individual Ruby file:
572
+
573
+ ```ruby
574
+ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
575
+ ```
576
+
577
+ 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.
578
+
579
+ 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](https://github.com/fxn/zeitwerk#shadowed-files), or is not managed by the receiver.
580
+
581
+ `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.
582
+
583
+ 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.
584
+
481
585
  <a id="markdown-reloading" name="reloading"></a>
482
586
  ### Reloading
483
587
 
588
+ <a id="markdown-configuration-and-usage" name="configuration-and-usage"></a>
589
+ #### Configuration and usage
590
+
484
591
  Zeitwerk is able to reload code, but you need to enable this feature:
485
592
 
486
593
  ```ruby
@@ -494,7 +601,7 @@ loader.reload
494
601
 
495
602
  There is no way to undo this, either you want to reload or you don't.
496
603
 
497
- Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
604
+ 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`.
498
605
 
499
606
  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.
500
607
 
@@ -502,12 +609,34 @@ Reloading removes the currently loaded classes and modules and resets the loader
502
609
 
503
610
  It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
504
611
 
505
- Reloading is not thread-safe:
612
+ <a id="markdown-thread-safety" name="thread-safety"></a>
613
+ #### Thread-safety
614
+
615
+ In order to reload safely, no other thread can be autoloading or reloading concurrently. Client code is responsible for this coordination.
616
+
617
+ 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:
618
+
619
+ ```ruby
620
+ require "concurrent/atomic/read_write_lock"
621
+
622
+ MyFramework::RELOAD_RW_LOCK = Concurrent::ReadWriteLock.new
623
+ ```
624
+
625
+ You acquire the lock for reading for serving each individual request:
626
+
627
+ ```ruby
628
+ MyFramework::RELOAD_RW_LOCK.with_read_lock do
629
+ serve(request)
630
+ end
631
+ ```
506
632
 
507
- * You should not reload while another thread is reloading.
508
- * You should not autoload while another thread is reloading.
633
+ Then, when a reload is triggered, just acquire the lock for writing in order to execute the method call safely:
509
634
 
510
- In order to reload in a thread-safe manner, frameworks need to implement some coordination. For example, a web framework that serves each request with its own thread may have a globally accessible read/write lock: When a request comes in, the framework acquires the lock for reading at the beginning, and releases it at the end. On the other hand, the code in the framework responsible for the call to `Zeitwerk::Loader#reload` needs to acquire the lock for writing.
635
+ ```ruby
636
+ MyFramework::RELOAD_RW_LOCK.with_write_lock do
637
+ loader.reload
638
+ end
639
+ ```
511
640
 
512
641
  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.
513
642
 
@@ -904,10 +1033,39 @@ loader.ignore(tests)
904
1033
  loader.setup
905
1034
  ```
906
1035
 
1036
+ <a id="markdown-shadowed-files" name="shadowed-files"></a>
1037
+ ### Shadowed files
1038
+
1039
+ In Ruby, if you have several files called `foo.rb` in different directories of `$LOAD_PATH` and execute
1040
+
1041
+ ```ruby
1042
+ require "foo"
1043
+ ```
1044
+
1045
+ the first one found gets loaded, and the rest are ignored.
1046
+
1047
+ 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
1048
+
1049
+ ```
1050
+ file #{file} is ignored because #{previous_occurrence} has precedence
1051
+ ```
1052
+
1053
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1054
+
1055
+ 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
1056
+
1057
+ ```
1058
+ file #{file} is ignored because #{constant_path} is already defined
1059
+ ```
1060
+
1061
+ (This message is not public interface and may change, you cannot rely on that exact wording.)
1062
+
1063
+ 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).
1064
+
907
1065
  <a id="markdown-edge-cases" name="edge-cases"></a>
908
1066
  ### Edge cases
909
1067
 
910
- A class or module that acts as a namespace:
1068
+ [Explicit namespaces](#explicit-namespaces) like `Trip` here:
911
1069
 
912
1070
  ```ruby
913
1071
  # trip.rb
@@ -921,7 +1079,7 @@ module Trip::Geolocation
921
1079
  end
922
1080
  ```
923
1081
 
924
- has to be defined with the `class` or `module` keywords, as in the example above.
1082
+ have to be defined with the `class`/`module` keywords, as in the example above.
925
1083
 
926
1084
  For technical reasons, raw constant assignment is not supported:
927
1085
 
@@ -983,12 +1141,36 @@ require "active_job"
983
1141
  require "active_job/queue_adapters"
984
1142
 
985
1143
  require "zeitwerk"
986
- loader = Zeitwerk::Loader.for_gem
1144
+ # By passing the flag, we acknowledge the extra directory lib/active_job
1145
+ # has to be managed by the loader and no warning has to be issued for it.
1146
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
987
1147
  loader.setup
988
1148
  ```
989
1149
 
990
1150
  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.
991
1151
 
1152
+ <a id="markdown-introspection" name="introspection"></a>
1153
+ ### Introspection
1154
+
1155
+ The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1156
+
1157
+ ```ruby
1158
+ loader = Zeitwerk::Loader.new
1159
+ loader.push_dir(Pathname.new("/foo"))
1160
+ loader.dirs # => ["/foo"]
1161
+ ```
1162
+
1163
+ 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:
1164
+
1165
+ ```ruby
1166
+ loader = Zeitwerk::Loader.new
1167
+ loader.push_dir(Pathname.new("/foo"))
1168
+ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
1169
+ loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
1170
+ ```
1171
+
1172
+ These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1173
+
992
1174
  <a id="markdown-encodings" name="encodings"></a>
993
1175
  ### Encodings
994
1176
 
@@ -12,4 +12,10 @@ module Zeitwerk
12
12
 
13
13
  class NameError < ::NameError
14
14
  end
15
+
16
+ class SetupRequired < Error
17
+ def initialize
18
+ super("please, finish your configuration and call Zeitwerk::Loader#setup once all is ready")
19
+ end
20
+ end
15
21
  end
@@ -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,21 @@ 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
- private
52
-
53
50
  # @sig () -> void
54
- def disable_tracer_if_unneeded
51
+ private def disable_tracer_if_unneeded
55
52
  mutex.synchronize do
56
53
  tracer.disable if cpaths.empty?
57
54
  end
58
55
  end
59
56
 
60
57
  # @sig (TracePoint) -> void
61
- def tracepoint_class_callback(event)
58
+ private def tracepoint_class_callback(event)
62
59
  # If the class is a singleton class, we won't do anything with it so we
63
60
  # can bail out immediately. This is several orders of magnitude faster
64
61
  # than accessing its name.
@@ -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)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is a private module.
4
+ module Zeitwerk::Internal
5
+ def internal(method_name)
6
+ private method_name
7
+
8
+ mangled = "__#{method_name}"
9
+ alias_method mangled, method_name
10
+ public mangled
11
+ end
12
+ end
@@ -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)
@@ -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 lazy_subdirs entry.
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 subdirs = lazy_subdirs.delete(real_mod_name(namespace))
75
- subdirs.each do |subdir|
76
- set_autoloads_in_dir(subdir, namespace)
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