zeitwerk 2.3.0 → 2.3.1

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: b8fd1828e8e92937af8b1e46033442f517c65c07a443a9a265b67c03f22b8b86
4
- data.tar.gz: 4e04076d6ac82d1bff9bf3db8604ca7e826afad753fb1268116cd115f15d301b
3
+ metadata.gz: 7b0087cb3e996cc4e5ccfabf179c0e793a811d24e4ee36debf9e07fcb06e1630
4
+ data.tar.gz: 63c94cc509932eb9dfceb9e951f05092c30e9d3aff8bacda5a66d7da93bf4a64
5
5
  SHA512:
6
- metadata.gz: cacd0d2c43a566a5e3be7dfb98585e9598a9fda8e930bb0f97a96180db332d6303fb1cfbf850f85dd822071968fc624262b94c27db85ad3e1236f1e243df64c2
7
- data.tar.gz: 0f9c90d543eae10edade4777f63c02a0f5ef30ecfdfe752fc9f11e5f6e3c835d2b893b0e8074e4cd068859e41dcf6e5374527c73499876deab3a9974cd0d2bf4
6
+ metadata.gz: 6b978a994c59c4da2e50b32a825626a3e48c4e9e8f3455320f22799437c5fc81de4f3635ab8ea07626f42f03cbea71dc735c70b446942d8e914d5dd1104d15dc
7
+ data.tar.gz: 2a0d8301b268d419be3dd36902ba44236e9de4a502c203bcbc2d32448f1c1d4e85fbae294b57d200a253e8e61e8ef07c119c9bf5f50e905d50064be6fe62babc
data/README.md CHANGED
@@ -16,6 +16,8 @@
16
16
  - [Nested root directories](#nested-root-directories)
17
17
  - [Usage](#usage)
18
18
  - [Setup](#setup)
19
+ - [Generic](#generic)
20
+ - [for_gem](#for_gem)
19
21
  - [Autoloading](#autoloading)
20
22
  - [Eager loading](#eager-loading)
21
23
  - [Reloading](#reloading)
@@ -30,8 +32,11 @@
30
32
  - [Use case: The adapter pattern](#use-case-the-adapter-pattern)
31
33
  - [Use case: Test files mixed with implementation files](#use-case-test-files-mixed-with-implementation-files)
32
34
  - [Edge cases](#edge-cases)
35
+ - [Reopening third-party namespaces](#reopening-third-party-namespaces)
33
36
  - [Rules of thumb](#rules-of-thumb)
34
- - [Autoloading, explicit namespaces, and debuggers](#autoloading-explicit-namespaces-and-debuggers)
37
+ - [Debuggers](#debuggers)
38
+ - [Break](#break)
39
+ - [Byebug](#byebug)
35
40
  - [Pronunciation](#pronunciation)
36
41
  - [Supported Ruby versions](#supported-ruby-versions)
37
42
  - [Testing](#testing)
@@ -52,7 +57,9 @@ Zeitwerk is also able to reload code, which may be handy while developing web ap
52
57
 
53
58
  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.
54
59
 
55
- 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`. Furthermore, Zeitwerk does only one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
60
+ 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`.
61
+
62
+ Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
56
63
 
57
64
  <a id="markdown-synopsis" name="synopsis"></a>
58
65
  ## Synopsis
@@ -211,6 +218,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
211
218
  <a id="markdown-setup" name="setup"></a>
212
219
  ### Setup
213
220
 
221
+ <a id="markdown-generic" name="generic"></a>
222
+ #### Generic
223
+
214
224
  Loaders are ready to load code right after calling `setup` on them:
215
225
 
216
226
  ```ruby
@@ -227,9 +237,36 @@ loader.push_dir(...)
227
237
  loader.setup
228
238
  ```
229
239
 
230
- The loader returned by `Zeitwerk::Loader.for_gem` has the directory of the caller pushed, normally that is the absolute path of `lib`. In that sense, `for_gem` can be used also by projects with a gem structure, even if they are not technically gems. That is, you don't need a gemspec or anything.
240
+ <a id="markdown-for_gem" name="for_gem"></a>
241
+ #### for_gem
242
+
243
+ `Zeitwerk::Loader.for_gem` is a convenience shortcut for the common case in which a gem has its entry point directly under the `lib` directory:
244
+
245
+ ```
246
+ lib/my_gem.rb # MyGem
247
+ lib/my_gem/version.rb # MyGem::VERSION
248
+ lib/my_gem/foo.rb # MyGem::Foo
249
+ ```
250
+
251
+ Neither a gemspec nor a version file are technically required, this helper works as long as the code is organized using that standard structure.
252
+
253
+ If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](https://github.com/fxn/zeitwerk#reopening-third-party-namespaces) down below.
254
+
255
+ Conceptually, `for_gem` translates to:
256
+
257
+ ```ruby
258
+ # lib/my_gem.rb
259
+
260
+ require "zeitwerk"
261
+ loader = Zeitwerk::Loader.new
262
+ loader.tag = File.basename(__FILE__, ".rb")
263
+ loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
264
+ loader.push_dir(__dir__)
265
+ ```
266
+
267
+ except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload.
231
268
 
232
- If the main module of a library references project constants at the top-level, Zeitwerk has to be ready to load them. Their definitions, in turn, may reference other project constants. And this is recursive. Therefore, it is important that the `setup` call happens above the main module definition:
269
+ If the main module references project constants at the top-level, Zeitwerk has to be ready to load them. Their definitions, in turn, may reference other project constants. And this is recursive. Therefore, it is important that the `setup` call happens above the main module definition:
233
270
 
234
271
  ```ruby
235
272
  # lib/my_gem.rb (main file)
@@ -245,8 +282,6 @@ module MyGem
245
282
  end
246
283
  ```
247
284
 
248
- Zeitwerk works internally only with absolute paths to avoid costly file searches in `$LOAD_PATH`. Indeed, the root directories do not even need to belong to `$LOAD_PATH`, everything just works by design if they don't.
249
-
250
285
  <a id="markdown-autoloading" name="autoloading"></a>
251
286
  ### Autoloading
252
287
 
@@ -619,6 +654,42 @@ Trip = Struct.new { ... } # NOT SUPPORTED
619
654
 
620
655
  This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
621
656
 
657
+ <a id="markdown-reopening-third-party-namespaces" name="reopening-third-party-namespaces"></a>
658
+ ### Reopening third-party namespaces
659
+
660
+ Projects managed by Zeitwerk can work with namespaces defined by third-party libraries. However, they have to be loaded in memory before calling `setup`.
661
+
662
+ For example, let's imagine you're writing a gem that implements an adapter for [Active Job](https://guides.rubyonrails.org/active_job_basics.html) that uses AwesomeQueue as backend. By convention, your gem has to define a class called `ActiveJob::QueueAdapters::AwesomeQueue`, and it has to do so in a file with a matching path:
663
+
664
+ ```ruby
665
+ # lib/active_job/queue_adapters/awesome_queue.rb
666
+ module ActiveJob
667
+ module QueueAdapters
668
+ class AwesomeQueue
669
+ # ...
670
+ end
671
+ end
672
+ end
673
+ ```
674
+
675
+ It is very important that your gem _reopens_ the modules `ActiveJob` and `ActiveJob::QueueAdapters` instead of _defining_ them. Because their proper definition lives in Active Job. Furthermore, if the project reloads, you do not want any of `ActiveJob` or `ActiveJob::QueueAdapters` to be reloaded.
676
+
677
+ Bottom line, Zeitwerk should not be managing those namespaces. Active Job owns them and defines them. Your gem needs to _reopen_ them.
678
+
679
+ In order to do so, you need to make sure those modules are loaded before calling `setup`. For instance, in the entry file for the gem:
680
+
681
+ ```ruby
682
+ # Ensure these namespaces are reopened, not defined.
683
+ require "active_job"
684
+ require "active_job/queue_adapters"
685
+
686
+ require "zeitwerk"
687
+ loader = Zeitwerk::Loader.for_gem
688
+ loader.setup
689
+ ```
690
+
691
+ 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.
692
+
622
693
  <a id="markdown-rules-of-thumb" name="rules-of-thumb"></a>
623
694
  ### Rules of thumb
624
695
 
@@ -634,14 +705,18 @@ This only affects explicit namespaces, those idioms work well for any other ordi
634
705
 
635
706
  6. In a given process, ideally, there should be at most one loader with reloading enabled. Technically, you can have more, but it may get tricky if one refers to constants managed by the other one. Do that only if you know what you are doing.
636
707
 
637
- <a id="markdown-autoloading-explicit-namespaces-and-debuggers" name="autoloading-explicit-namespaces-and-debuggers"></a>
638
- ### Autoloading, explicit namespaces, and debuggers
708
+ <a id="markdown-debuggers" name="debuggers"></a>
709
+ ### Debuggers
710
+
711
+ <a id="markdown-break" name="break"></a>
712
+ #### Break
639
713
 
640
- As of this writing, Zeitwerk is unable to autoload classes or modules that belong to [explicit namespaces](#explicit-namespaces) inside debugger sessions. You'll get a `NameError`.
714
+ Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger.
641
715
 
642
- The root cause is that debuggers set trace points, and Zeitwerk does too to support explicit namespaces. A debugger session happens inside a trace point handler, and Ruby does not invoke other handlers from within a running handler. Therefore, the code that manages explicit namespaces in Zeitwerk does not get called by the interpreter. See [this issue](https://github.com/deivid-rodriguez/byebug/issues/564#issuecomment-499413606) for further details.
716
+ <a id="markdown-byebug" name="byebug"></a>
717
+ #### Byebug
643
718
 
644
- As a workaround, you can eager load. Zeitwerk tries hard to succeed or fail consistently both autoloading and eager loading, so switching to eager loading should not introduce any interference in your debugging logic, generally speaking.
719
+ Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) are incompatible, classes or modules that belong to [explicit namespaces](#explicit-namespaces) are not autoloaded inside a Byebug session. See [this issue](https://github.com/deivid-rodriguez/byebug/issues/564#issuecomment-499413606) for further details.
645
720
 
646
721
  <a id="markdown-pronunciation" name="pronunciation"></a>
647
722
  ## Pronunciation
@@ -3,6 +3,17 @@
3
3
  module Kernel
4
4
  module_function
5
5
 
6
+ # We are going to decorate Kerner#require with two goals.
7
+ #
8
+ # First, by intercepting Kernel#require calls, we are able to autovivify
9
+ # modules on required directories, and also do internal housekeeping when
10
+ # managed files are loaded.
11
+ #
12
+ # On the other hand, if you publish a new version of a gem that is now managed
13
+ # by Zeitwerk, client code can reference directly your classes and modules and
14
+ # should not require anything. But if someone has legacy require calls around,
15
+ # they will work as expected, and in a compatible way.
16
+ #
6
17
  # We cannot decorate with prepend + super because Kernel has already been
7
18
  # included in Object, and changes in ancestors don't get propagated into
8
19
  # already existing ancestor chains.
@@ -30,4 +41,24 @@ module Kernel
30
41
  end
31
42
  end
32
43
  end
44
+
45
+ # By now, I have seen no way so far to decorate require_relative.
46
+ #
47
+ # For starters, at least in CRuby, require_relative does not delegate to
48
+ # require. Both require and require_relative delegate the bulk of their work
49
+ # to an internal C function called rb_require_safe. So, our require wrapper is
50
+ # not executed.
51
+ #
52
+ # On the other hand, we cannot use the aliasing technique above because
53
+ # require_relative receives a path relative to the directory of the file in
54
+ # which the call is performed. If a wrapper here invoked the original method,
55
+ # Ruby would resolve the relative path taking lib/zeitwerk as base directory.
56
+ #
57
+ # A workaround could be to extract the base directory from caller_locations,
58
+ # but what if someone else decorated require_relative before us? You can't
59
+ # really know with certainty where's the original call site in the stack.
60
+ #
61
+ # However, the main use case for require_relative is to load files from your
62
+ # own project. Projects managed by Zeitwerk don't do this for files managed by
63
+ # Zeitwerk, precisely.
33
64
  end
@@ -464,7 +464,7 @@ module Zeitwerk
464
464
  # require "zeitwerk"
465
465
  # loader = Zeitwerk::Loader.new
466
466
  # loader.tag = File.basename(__FILE__, ".rb")
467
- # loader.inflector = Zeitwerk::GemInflector.new
467
+ # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
468
468
  # loader.push_dir(__dir__)
469
469
  #
470
470
  # except that this method returns the same object in subsequent calls from
@@ -616,7 +616,10 @@ module Zeitwerk
616
616
  # $LOADED_FEATURES stores real paths since Ruby 2.4.4. We set and save the
617
617
  # real path to be able to delete it from $LOADED_FEATURES on unload, and to
618
618
  # be able to do a lookup later in Kernel#require for manual require calls.
619
- realpath = File.realpath(abspath)
619
+ #
620
+ # We freeze realpath because that saves allocations in Module#autoload.
621
+ # See #125.
622
+ realpath = File.realpath(abspath).freeze
620
623
  parent.autoload(cname, realpath)
621
624
  if logger
622
625
  if ruby?(realpath)
@@ -719,8 +722,13 @@ module Zeitwerk
719
722
  def ls(dir)
720
723
  Dir.foreach(dir) do |basename|
721
724
  next if basename.start_with?(".")
725
+
722
726
  abspath = File.join(dir, basename)
723
- yield basename, abspath unless ignored_paths.member?(abspath)
727
+ next if ignored_paths.member?(abspath)
728
+
729
+ # We freeze abspath because that saves allocations when passed later to
730
+ # File methods. See #125.
731
+ yield basename, abspath.freeze
724
732
  end
725
733
  end
726
734
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.3.0"
4
+ VERSION = "2.3.1"
5
5
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeitwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-03 00:00:00.000000000 Z
11
+ date: 2020-06-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem
15
15
  and application may have their own independent autoloader, with its own
16
- configuration, inflector, and logger. Supports autoloading, preloading,
16
+ configuration, inflector, and logger. Supports autoloading,
17
17
  reloading, and eager loading.
18
18
  email: fxn@hashref.com
19
19
  executables: []
@@ -36,7 +36,11 @@ files:
36
36
  homepage: https://github.com/fxn/zeitwerk
37
37
  licenses:
38
38
  - MIT
39
- metadata: {}
39
+ metadata:
40
+ homepage_uri: https://github.com/fxn/zeitwerk
41
+ changelog_uri: https://github.com/fxn/zeitwerk/blob/master/CHANGELOG.md
42
+ source_code_uri: https://github.com/fxn/zeitwerk
43
+ bug_tracker_uri: https://github.com/fxn/zeitwerk/issues
40
44
  post_install_message:
41
45
  rdoc_options: []
42
46
  require_paths:
@@ -52,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
56
  - !ruby/object:Gem::Version
53
57
  version: '0'
54
58
  requirements: []
55
- rubygems_version: 3.0.3
59
+ rubygems_version: 3.1.2
56
60
  signing_key:
57
61
  specification_version: 4
58
62
  summary: Efficient and thread-safe constant autoloader