zeitwerk 2.6.7 → 2.6.12

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: e6c7bf12c3a33195c57541b840ca6d7dbc21b19e0d7b91d8933ad37aa88b325d
4
- data.tar.gz: 4b8dd86417dd633b78c02eada9ed6956e8e708397c988237aaddecb9644ba469
3
+ metadata.gz: 5a9048a5ba05f448e5406080995aab9709a3dd7c8dd32aad3aa0ba4f544b69c4
4
+ data.tar.gz: 581cf27f70c82b754a1baadf7a41f6c87c069ae2e78bd26b4ec6e02cfa2795b9
5
5
  SHA512:
6
- metadata.gz: 4f0bc60d9914a9e815aed501a030f7e9bde7927f750f3836f96ac86a52c0699f4ca69456bd381845428f6b09adccaa085443f39bb0fdaa651437742e21d10af2
7
- data.tar.gz: 3ba55f7bb24725d156cc4043697bfcd6092e2893f1aa71296965c82ae9ed66b79c30f853b535693b6b884d7d3d26cf1b122545e395ba11c92f65552e14116248
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%3main)
6
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3Amain)
7
+
7
8
 
8
9
  <!-- TOC -->
9
10
 
@@ -24,6 +25,7 @@
24
25
  - [Setup](#setup)
25
26
  - [Generic](#generic)
26
27
  - [for_gem](#for_gem)
28
+ - [for_gem_extension](#for_gem_extension)
27
29
  - [Autoloading](#autoloading)
28
30
  - [Eager loading](#eager-loading)
29
31
  - [Eager load exclusions](#eager-load-exclusions)
@@ -55,6 +57,8 @@
55
57
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
56
58
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
57
59
  - [Introspection](#introspection)
60
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
61
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
58
62
  - [Encodings](#encodings)
59
63
  - [Rules of thumb](#rules-of-thumb)
60
64
  - [Debuggers](#debuggers)
@@ -75,15 +79,15 @@
75
79
 
76
80
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
77
81
 
78
- Given a [conventional file structure](#file-structure), Zeitwerk is able to load your project's classes and modules on demand (autoloading), or upfront (eager loading). You don't need to write `require` calls for your own files, rather, you can streamline your programming knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and matches Ruby's semantics for constants.
82
+ Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
79
83
 
80
- Zeitwerk is also able to reload code, which may be handy while developing web applications. Coordination is needed to reload in a thread-safe manner. The documentation below explains how to do this.
84
+ Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
81
85
 
82
- The gem is designed so that any project, gem dependency, application, etc. can have their own independent loader, coexisting in the same process, managing their own project trees, and independent of each other. Each loader has its own configuration, inflector, and optional logger.
86
+ The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
83
87
 
84
- Internally, Zeitwerk issues `require` calls exclusively using absolute file names, so there are no costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk do not even need to be in `$LOAD_PATH`.
88
+ Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
85
89
 
86
- Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
90
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
87
91
 
88
92
  <a id="markdown-synopsis" name="synopsis"></a>
89
93
  ## Synopsis
@@ -143,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
143
147
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
144
148
  ### The idea: File paths match constant paths
145
149
 
146
- To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
150
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
147
151
 
148
152
  ```
149
153
  lib/my_gem.rb -> MyGem
@@ -152,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
152
156
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
153
157
  ```
154
158
 
155
- You can tune that a bit by [collapsing directories](#collapsing-directories), or by [ignoring parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
159
+ You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
156
160
 
157
161
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
158
162
  ### Inner simple constants
@@ -210,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
210
214
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
211
215
  #### Custom root namespaces
212
216
 
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.
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.
214
218
 
215
219
  For example, given:
216
220
 
@@ -226,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
226
230
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
227
231
  ```
228
232
 
229
- Please, note that the given root namespace must be non-reloadable, though autoloaded constants in that namespace can be. That is, if you associate `app/api` with an existing `Api` module, that module should not be reloadable. However, if the project defines and autoloads the class `Api::Deliveries`, that one can be reloaded.
233
+ Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
230
234
 
231
235
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
232
236
  #### Nested root directories
233
237
 
234
- Root directories should not be ideally nested, but Zeitwerk supports them because in Rails, for example, both `app/models` and `app/models/concerns` belong to the autoload paths.
238
+ Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
235
239
 
236
- Zeitwerk detects nested root directories, and treats them as roots only. In the example above, `concerns` is not considered to be a namespace below `app/models`. For example, the file:
240
+ Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
237
241
 
238
242
  ```
239
243
  app/models/concerns/geolocatable.rb
@@ -244,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
244
248
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
245
249
  ### Implicit namespaces
246
250
 
247
- If a namespace is just a simple module with no code, you do not need to define it in a file: Directories without a matching Ruby file get modules created automatically on your behalf.
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.
248
252
 
249
- For example, if a project has an `admin` directory:
253
+ For instance, suppose a project includes an `admin` directory:
250
254
 
251
255
  ```
252
256
  app/controllers/admin/users_controller.rb -> Admin::UsersController
@@ -254,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
254
258
 
255
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.
256
260
 
257
- 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.
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.
258
262
 
259
263
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
260
264
  ### Explicit namespaces
@@ -410,6 +414,65 @@ Otherwise, there's a flag to say the extra stuff is OK:
410
414
  Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
411
415
  ```
412
416
 
417
+ <a id="markdown-for_gem_extension" name="for_gem_extension"></a>
418
+ #### for_gem_extension
419
+
420
+ Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/):
421
+
422
+ * The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
423
+ * The namespace should be `Net::HTTP::NicheFeature`.
424
+ * The entry point should be `lib/net/http/niche_feature.rb`.
425
+ * Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.
426
+
427
+ The top-level file mentioned in the last point is optional. In particular, from
428
+
429
+ ```ruby
430
+ gem "net-http-niche_feature"
431
+ ```
432
+
433
+ if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
434
+
435
+ Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
436
+
437
+ The structure of the gem would be like this:
438
+
439
+ ```ruby
440
+ # lib/net-http-niche_feature.rb (optional)
441
+
442
+ # For technical reasons, this cannot be require_relative.
443
+ require "net/http/niche_feature"
444
+
445
+
446
+ # lib/net/http/niche_feature.rb
447
+
448
+ require "net/http"
449
+ require "zeitwerk"
450
+
451
+ loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
452
+ loader.setup
453
+
454
+ module Net::HTTP::NicheFeature
455
+ # Since the setup has been performed, at this point we are already able
456
+ # to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
457
+ include MyMixin
458
+ end
459
+
460
+
461
+ # lib/net/http/niche_feature/version.rb
462
+
463
+ module Net::HTTP::NicheFeature
464
+ VERSION = "1.0.0"
465
+ end
466
+ ```
467
+
468
+ `Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
469
+
470
+ If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
471
+
472
+ Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file.
473
+
474
+ `Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
475
+
413
476
  <a id="markdown-autoloading" name="autoloading"></a>
414
477
  ### Autoloading
415
478
 
@@ -673,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
673
736
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
674
737
  ```
675
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
+
676
764
  Overrides need to be configured before calling `setup`.
677
765
 
678
- The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
766
+ The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
679
767
 
680
768
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
681
769
  #### Zeitwerk::GemInflector
@@ -1154,6 +1242,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
1154
1242
  <a id="markdown-introspection" name="introspection"></a>
1155
1243
  ### Introspection
1156
1244
 
1245
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1246
+ #### `Zeitwerk::Loader#dirs`
1247
+
1157
1248
  The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1158
1249
 
1159
1250
  ```ruby
@@ -1175,6 +1266,44 @@ By default, ignored root directories are filtered out. If you want them included
1175
1266
 
1176
1267
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1177
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
+
1178
1307
  <a id="markdown-encodings" name="encodings"></a>
1179
1308
  ### Encodings
1180
1309
 
@@ -1205,9 +1334,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1205
1334
  <a id="markdown-debuggers" name="debuggers"></a>
1206
1335
  ### Debuggers
1207
1336
 
1208
- Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
1337
+ Zeitwerk and [debug.rb](https://github.com/ruby/debug) are fully compatible if CRuby is ≥ 3.1 (see [ruby/debug#558](https://github.com/ruby/debug/pull/558)).
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.
1209
1340
 
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).
1341
+ [Break](https://github.com/gsamokovarov/break) is fully compatible.
1211
1342
 
1212
1343
  <a id="markdown-pronunciation" name="pronunciation"></a>
1213
1344
  ## Pronunciation
@@ -5,8 +5,8 @@ module Zeitwerk
5
5
  # @sig (String) -> void
6
6
  def initialize(root_file)
7
7
  namespace = File.basename(root_file, ".rb")
8
- lib_dir = File.dirname(root_file)
9
- @version_file = File.join(lib_dir, namespace, "version.rb")
8
+ root_dir = File.dirname(root_file)
9
+ @version_file = File.join(root_dir, namespace, "version.rb")
10
10
  end
11
11
 
12
12
  # @sig (String, String) -> String
@@ -3,27 +3,31 @@
3
3
  module Zeitwerk
4
4
  # @private
5
5
  class GemLoader < Loader
6
+ include RealModName
7
+
6
8
  # Users should not create instances directly, the public interface is
7
9
  # `Zeitwerk::Loader.for_gem`.
8
10
  private_class_method :new
9
11
 
10
12
  # @private
11
13
  # @sig (String, bool) -> Zeitwerk::GemLoader
12
- def self._new(root_file, warn_on_extra_files:)
13
- new(root_file, warn_on_extra_files: warn_on_extra_files)
14
+ def self.__new(root_file, namespace:, warn_on_extra_files:)
15
+ new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
14
16
  end
15
17
 
16
18
  # @sig (String, bool) -> void
17
- def initialize(root_file, warn_on_extra_files:)
19
+ def initialize(root_file, namespace:, warn_on_extra_files:)
18
20
  super()
19
21
 
20
- @tag = File.basename(root_file, ".rb")
22
+ @tag = File.basename(root_file, ".rb")
23
+ @tag = real_mod_name(namespace) + "-" + @tag unless namespace.equal?(Object)
24
+
21
25
  @inflector = GemInflector.new(root_file)
22
26
  @root_file = File.expand_path(root_file)
23
- @lib = File.dirname(root_file)
27
+ @root_dir = File.dirname(root_file)
24
28
  @warn_on_extra_files = warn_on_extra_files
25
29
 
26
- push_dir(@lib)
30
+ push_dir(@root_dir, namespace: namespace)
27
31
  end
28
32
 
29
33
  # @sig () -> void
@@ -38,7 +42,7 @@ module Zeitwerk
38
42
  def warn_on_extra_files
39
43
  expected_namespace_dir = @root_file.delete_suffix(".rb")
40
44
 
41
- ls(@lib) do |basename, abspath|
45
+ ls(@root_dir) do |basename, abspath|
42
46
  next if abspath == @root_file
43
47
  next if abspath == expected_namespace_dir
44
48
 
@@ -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.on_file_autoloaded(path) if required
31
+ loader.__on_file_autoloaded(path) if required
32
32
  required
33
33
  else
34
- loader.on_dir_autoloaded(path)
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.on_file_autoloaded(abspath)
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 we handle
36
- # directories ourselves, so the callback needs to account for concurrency.
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
- mutex2.synchronize do
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
- set_autoloads_in_dir(dir, namespace)
84
+ define_autoloads_for_dir(dir, namespace)
77
85
  end
78
86
  end
79
87
  end
@@ -109,8 +109,7 @@ module Zeitwerk::Loader::Config
109
109
  # @raise [Zeitwerk::Error]
110
110
  # @sig (String | Pathname, Module) -> void
111
111
  def push_dir(path, namespace: Object)
112
- # Note that Class < Module.
113
- unless namespace.is_a?(Module)
112
+ unless namespace.is_a?(Module) # Note that Class < Module.
114
113
  raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
115
114
  end
116
115
 
@@ -298,9 +297,9 @@ module Zeitwerk::Loader::Config
298
297
  # Common use case.
299
298
  return false if ignored_paths.empty?
300
299
 
301
- walk_up(abspath) do |abspath|
302
- return true if ignored_path?(abspath)
303
- return false if roots.key?(abspath)
300
+ walk_up(abspath) do |path|
301
+ return true if ignored_path?(path)
302
+ return false if roots.key?(path)
304
303
  end
305
304
 
306
305
  false
@@ -328,9 +327,9 @@ module Zeitwerk::Loader::Config
328
327
  # Optimize this common use case.
329
328
  return false if eager_load_exclusions.empty?
330
329
 
331
- walk_up(abspath) do |abspath|
332
- return true if eager_load_exclusions.member?(abspath)
333
- return false if roots.key?(abspath)
330
+ walk_up(abspath) do |path|
331
+ return true if eager_load_exclusions.member?(path)
332
+ return false if roots.key?(path)
334
333
  end
335
334
 
336
335
  false
@@ -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]) && !shadowed_file?(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
@@ -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 Mutex
95
- attr_reader :mutex2
96
- private :mutex2
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
- set_autoloads_in_dir(root_dir, root_namespace)
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
  #
@@ -264,12 +314,15 @@ module Zeitwerk
264
314
  # --- Class methods ---------------------------------------------------------------------------
265
315
 
266
316
  class << self
317
+ include RealModName
318
+
267
319
  # @sig #call | #debug | nil
268
320
  attr_accessor :default_logger
269
321
 
270
322
  # This is a shortcut for
271
323
  #
272
324
  # require "zeitwerk"
325
+ #
273
326
  # loader = Zeitwerk::Loader.new
274
327
  # loader.tag = File.basename(__FILE__, ".rb")
275
328
  # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
@@ -284,7 +337,36 @@ module Zeitwerk
284
337
  # @sig (bool) -> Zeitwerk::GemLoader
285
338
  def for_gem(warn_on_extra_files: true)
286
339
  called_from = caller_locations(1, 1).first.path
287
- Registry.loader_for_gem(called_from, warn_on_extra_files: warn_on_extra_files)
340
+ Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
341
+ end
342
+
343
+ # This is a shortcut for
344
+ #
345
+ # require "zeitwerk"
346
+ #
347
+ # loader = Zeitwerk::Loader.new
348
+ # loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb")
349
+ # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
350
+ # loader.push_dir(__dir__, namespace: namespace)
351
+ #
352
+ # except that this method returns the same object in subsequent calls from
353
+ # the same file, in the unlikely case the gem wants to be able to reload.
354
+ #
355
+ # This method returns a subclass of Zeitwerk::Loader, but the exact type
356
+ # is private, client code can only rely on the interface.
357
+ #
358
+ # @sig (bool) -> Zeitwerk::GemLoader
359
+ def for_gem_extension(namespace)
360
+ unless namespace.is_a?(Module) # Note that Class < Module.
361
+ raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
362
+ end
363
+
364
+ unless real_mod_name(namespace)
365
+ raise Zeitwerk::Error, "extending anonymous namespaces is unsupported"
366
+ end
367
+
368
+ called_from = caller_locations(1, 1).first.path
369
+ Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false)
288
370
  end
289
371
 
290
372
  # Broadcasts `eager_load` to all loaders. Those that have not been setup
@@ -325,36 +407,17 @@ module Zeitwerk
325
407
  end
326
408
 
327
409
  # @sig (String, Module) -> void
328
- private def set_autoloads_in_dir(dir, parent)
410
+ private def define_autoloads_for_dir(dir, parent)
329
411
  ls(dir) do |basename, abspath|
330
- begin
331
- if ruby?(basename)
332
- basename.delete_suffix!(".rb")
333
- cname = inflector.camelize(basename, abspath).to_sym
334
- autoload_file(parent, cname, abspath)
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)
335
418
  else
336
- if collapse?(abspath)
337
- set_autoloads_in_dir(abspath, parent)
338
- else
339
- cname = inflector.camelize(basename, abspath).to_sym
340
- autoload_subdir(parent, cname, abspath)
341
- end
419
+ autoload_subdir(parent, cname_for(basename, abspath), abspath)
342
420
  end
343
- rescue ::NameError => error
344
- path_type = ruby?(abspath) ? "file" : "directory"
345
-
346
- raise NameError.new(<<~MESSAGE, error.name)
347
- #{error.message} inferred by #{inflector.class} from #{path_type}
348
-
349
- #{abspath}
350
-
351
- Possible ways to address this:
352
-
353
- * Tell Zeitwerk to ignore this particular #{path_type}.
354
- * Tell Zeitwerk to ignore one of its parent directories.
355
- * Rename the #{path_type} to comply with the naming conventions.
356
- * Modify the inflector to handle this case.
357
- MESSAGE
358
421
  end
359
422
  end
360
423
  end
@@ -380,12 +443,12 @@ module Zeitwerk
380
443
  elsif !cdef?(parent, cname)
381
444
  # First time we find this namespace, set an autoload for it.
382
445
  namespace_dirs[cpath(parent, cname)] << subdir
383
- set_autoload(parent, cname, subdir)
446
+ define_autoload(parent, cname, subdir)
384
447
  else
385
448
  # For whatever reason the constant that corresponds to this namespace has
386
449
  # already been defined, we have to recurse.
387
450
  log("the namespace #{cpath(parent, cname)} already exists, descending into #{subdir}") if logger
388
- set_autoloads_in_dir(subdir, cget(parent, cname))
451
+ define_autoloads_for_dir(subdir, cget(parent, cname))
389
452
  end
390
453
  end
391
454
 
@@ -408,7 +471,7 @@ module Zeitwerk
408
471
  shadowed_files << file
409
472
  log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger
410
473
  else
411
- set_autoload(parent, cname, file)
474
+ define_autoload(parent, cname, file)
412
475
  end
413
476
  end
414
477
 
@@ -422,12 +485,12 @@ module Zeitwerk
422
485
 
423
486
  log("earlier autoload for #{cpath(parent, cname)} discarded, it is actually an explicit namespace defined in #{file}") if logger
424
487
 
425
- set_autoload(parent, cname, file)
488
+ define_autoload(parent, cname, file)
426
489
  register_explicit_namespace(cpath(parent, cname))
427
490
  end
428
491
 
429
492
  # @sig (Module, Symbol, String) -> void
430
- private def set_autoload(parent, cname, abspath)
493
+ private def define_autoload(parent, cname, abspath)
431
494
  parent.autoload(cname, abspath)
432
495
 
433
496
  if logger
@@ -479,7 +542,6 @@ module Zeitwerk
479
542
  raise Error,
480
543
  "loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
481
544
  " which is already managed by\n\n#{loader.pretty_inspect}\n"
482
- EOS
483
545
  end
484
546
  end
485
547
  end
@@ -86,8 +86,8 @@ module Zeitwerk
86
86
  #
87
87
  # @private
88
88
  # @sig (String) -> Zeitwerk::Loader
89
- def loader_for_gem(root_file, warn_on_extra_files:)
90
- gem_loaders_by_root_file[root_file] ||= GemLoader._new(root_file, warn_on_extra_files: warn_on_extra_files)
89
+ def loader_for_gem(root_file, namespace:, warn_on_extra_files:)
90
+ gem_loaders_by_root_file[root_file] ||= GemLoader.__new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
91
91
  end
92
92
 
93
93
  # @private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.6.7"
4
+ VERSION = "2.6.12"
5
5
  end
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.7
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-02-10 00:00:00.000000000 Z
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.3
64
+ rubygems_version: 3.4.16
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: Efficient and thread-safe constant autoloader