onload 1.0.3 → 1.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c041fc54820d21453d5f755e87e35c91881e976d0b51b16d757ecfa20b55f4c
4
- data.tar.gz: 8d8520e2f427c22df3562c5aa6b0c5ae0b890333a3149a21a1f181a9ec365b0e
3
+ metadata.gz: 66e3e322ccf2b92d128fb3bf904426cff77ba86e88ec47eb8ba9c5101dd0ae28
4
+ data.tar.gz: 5495ff350b972c47e6c992f744245d12baa57c0929d7878241f6c9a91922f17c
5
5
  SHA512:
6
- metadata.gz: fc15ad2aa9f2c17e84b2fdd1110d1ba930bb179dc13a1df827a4ca29ca7a19d4a2b4158fe66ee285ce92b2af8522f787c6f3526e9d068fb62dd4439137bcc0c7
7
- data.tar.gz: 6475cd2fae9260661881cf3eb42769c8a25dd3a776a651293ba38094affa21a9520033efc8488f08b6706ef8ab7c92fcf6d0d1641769d695697869009ab70a0f
6
+ metadata.gz: 1fd824da5c6ee8cd78342b5e0cb945353147970ebe26a4c6e16005bf8245cd5e743223441c76b7ab04216dd1757a8b3925a71b32825402d1f4becf7cbbf488c0
7
+ data.tar.gz: 8f296f7561c97af7ff6863a87fb73c3ef1c2bccb6379ca4aceb5300c13ccde937d17189de93e877c6016ab76a62f12225ce04e2494e77e229f59172c5dfd0de7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.0.5
2
+ * Fix issues with Zeitwerk v2.7.3 and later.
3
+ * Don't attempt to autoload directories.
4
+
5
+ ## 1.0.4
6
+ * Fix issues with Zeitwerk v2.6.13 and later.
7
+ - Zeitwerk introduced the `Cref` class, which encapsulates a `mod` and `cname`. A number of internal methods used to return both of these things individually; now they are wrapped in `Cref`s.
8
+
1
9
  ## 1.0.3
2
10
  * Fix Bootsnap issue causing `NoMethodError`.
3
11
  - Onload started out using `alias_method` to override certain Zeitwerk and Bootsnap methods. When it was extracted into a gem, I chose to use `Module#prepend` instead. I forgot to convert one of the method calls to `super`, hence the error.
@@ -15,9 +15,19 @@ module Kernel
15
15
  # in order to load the resulting file. Otherwise you get an error about
16
16
  # an uninitialized constant, and it's like... yeah, I _know_ it's
17
17
  # uninitialized, that's why I'm loading this file. Whatevs.
18
- loader = Zeitwerk::Registry.loader_for(file)
18
+ loader = if Zeitwerk::Registry.respond_to?(:loader_for)
19
+ Zeitwerk::Registry.loader_for(file)
20
+ else
21
+ Zeitwerk::Registry.autoloads.registered?(file)
22
+ end
23
+
19
24
  parent, cname = loader.send(:autoloads)[file]
20
- parent.send(:remove_const, cname)
25
+
26
+ if defined?(Zeitwerk::Cref) && parent.is_a?(Zeitwerk::Cref)
27
+ parent.remove
28
+ else
29
+ parent.send(:remove_const, cname)
30
+ end
21
31
 
22
32
  return onload_orig_load(f.outfile, *args)
23
33
  end
@@ -3,6 +3,9 @@
3
3
  module Onload
4
4
  module BootsnapAutoloadPatch
5
5
  def autoload(const, path)
6
+ # only autoload files, not directories
7
+ return super if ::File.directory?(path)
8
+
6
9
  # Bootsnap monkeypatches Module.autoload in order to leverage its load
7
10
  # path cache, which effectively converts a relative path into an absolute
8
11
  # one without incurring the cost of searching the load path.
@@ -11,26 +11,38 @@ module Onload
11
11
  super || Onload.process?(path)
12
12
  end
13
13
 
14
- def autoload_file(parent, cname, file)
15
- if Onload.process?(file)
16
- # Some older versions of Zeitwerk very naïvely try to remove only the
17
- # last 3 characters in an attempt to strip off the .rb file extension,
18
- # while newer ones only remove it if it's actually there. This line is
19
- # necessary to remove the trailing leftover period for older versions,
20
- # and remove the entire extension for newer versions. Although cname
21
- # means "constant name," we use Onload.basename to remove all residual
22
- # file extensions that were left over from the conversion from a file
23
- # name to a cname.
24
- cname = Onload.basename(cname.to_s).to_sym
25
- else
26
- # if there is a corresponding unprocessed file, autoload it instead of
27
- # the .rb file
28
- if (unprocessed_file = Onload.unprocessed_file_for(file))
29
- file = unprocessed_file
14
+ if Zeitwerk::Loader.instance_method(:autoload_file).arity == 2
15
+ def autoload_file(cref, file)
16
+ if !Onload.process?(file)
17
+ if (unprocessed_file = Onload.unprocessed_file_for(file))
18
+ file = unprocessed_file
19
+ end
30
20
  end
21
+
22
+ super
31
23
  end
24
+ else
25
+ def autoload_file(parent, cname, file)
26
+ if Onload.process?(file)
27
+ # Some older versions of Zeitwerk very naïvely try to remove only the
28
+ # last 3 characters in an attempt to strip off the .rb file extension,
29
+ # while newer ones only remove it if it's actually there. This line is
30
+ # necessary to remove the trailing leftover period for older versions,
31
+ # and remove the entire extension for newer versions. Although cname
32
+ # means "constant name," we use Onload.basename to remove all residual
33
+ # file extensions that were left over from the conversion from a file
34
+ # name to a cname.
35
+ cname = Onload.basename(cname.to_s).to_sym
36
+ else
37
+ # if there is a corresponding unprocessed file, autoload it instead of
38
+ # the .rb file
39
+ if (unprocessed_file = Onload.unprocessed_file_for(file))
40
+ file = unprocessed_file
41
+ end
42
+ end
32
43
 
33
- super
44
+ super
45
+ end
34
46
  end
35
47
 
36
48
  # introduced in Zeitwerk v2.6.10
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Onload
4
- VERSION = "1.0.3"
4
+ VERSION = "1.0.5"
5
5
  end
@@ -38,5 +38,14 @@ describe HomeController, type: :request do
38
38
  )
39
39
  end
40
40
  end
41
+
42
+ it "supports file shadowing" do
43
+ get "/action_list"
44
+
45
+ expect(response).to have_http_status(:ok)
46
+ expect(response.body).to(
47
+ have_selector("div", text: "LIST ITEM")
48
+ )
49
+ end
41
50
  end
42
51
  end
@@ -0,0 +1 @@
1
+ <div><%= Primer::Alpha::ActionList.new.list %></div>
@@ -11,5 +11,7 @@ module Onload
11
11
  config.autoload_paths << ::File.expand_path(
12
12
  ::File.join(*%w[.. .. .. fixtures]), __dir__
13
13
  )
14
+
15
+ config.autoload_paths << Rails.root.join("lib").to_s
14
16
  end
15
17
  end
@@ -1,3 +1,4 @@
1
1
  Rails.application.routes.draw do
2
- root to: 'home#index'
2
+ root to: "home#index"
3
+ get "/action_list", to: "home#action_list"
3
4
  end
@@ -0,0 +1,11 @@
1
+ module Primer
2
+ module Alpha
3
+ class ActionList
4
+ class Item
5
+ def item
6
+ "item".upcase
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Primer
2
+ module Alpha
3
+ class ActionList
4
+ class Item
5
+ def item
6
+ "item"
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Primer
2
+ module Alpha
3
+ class ActionList
4
+ def list
5
+ "list ".upcase + Item.new.item
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Primer
2
+ module Alpha
3
+ class ActionList
4
+ def list
5
+ "list " + Item.new.item
6
+ end
7
+ end
8
+ end
9
+ end