Skip to content

Commit c64e282

Browse files
committed
ManifestRegistrar uses load instead of require
This is a bug that is caused by the interaction between dry-system and dry-rails. Dry::Rails rebuilds the Container from scratch on every reload, which resets all the keys. Since ManifestRegistrar was using `require`, this reload would not register the keys as expected.
1 parent dd4ad2f commit c64e282

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

lib/dry/system/manifest_registrar.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def finalize!
3333

3434
# @api private
3535
def call(component)
36-
require(root.join(config.registrations_dir, component.root_key.to_s))
36+
load(root.join(config.registrations_dir, "#{component.root_key}#{RB_EXT}"))
3737
end
3838

3939
# @api private
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "Lazy-loading registration manifest files" do
4-
before do
5-
module Test
6-
class Container < Dry::System::Container
7-
configure do |config|
8-
config.root = SPEC_ROOT.join("fixtures/manifest_registration").realpath
9-
end
4+
module Test; end
105

11-
add_to_load_path!("lib")
6+
def build_container
7+
Class.new(Dry::System::Container) do
8+
configure do |config|
9+
config.root = SPEC_ROOT.join("fixtures/manifest_registration").realpath
1210
end
1311
end
1412
end
1513

1614
shared_examples "manifest component" do
15+
before do
16+
Test::Container = build_container
17+
Test::Container.add_to_load_path!("lib")
18+
end
19+
1720
it "loads a registration manifest file if the component could not be found" do
1821
expect(Test::Container["foo.special"]).to be_a(Test::Foo)
1922
expect(Test::Container["foo.special"].name).to eq "special"
@@ -25,7 +28,36 @@ class Container < Dry::System::Container
2528
end
2629

2730
context "Finalized container" do
28-
before { Test::Container.finalize! }
2931
include_examples "manifest component"
32+
before { Test::Container.finalize! }
33+
end
34+
35+
context "Autoloaded container" do
36+
let :autoloader do
37+
Zeitwerk::Loader.new.tap do |loader|
38+
loader.enable_reloading
39+
40+
# This is a simulacrum of the Dry::Rails container reset
41+
# that happens on every reload
42+
loader.on_setup do
43+
if Test.const_defined?(:Container)
44+
Test.__send__(:remove_const, :Container)
45+
end
46+
47+
Test.const_set :Container, build_container
48+
Test::Container.finalize!
49+
50+
loader.push_dir(Test::Container.root)
51+
end
52+
end
53+
end
54+
55+
it "reloads manifest keys" do
56+
autoloader.setup
57+
expect(Test::Container.keys).to include("foo.special")
58+
59+
autoloader.reload
60+
expect(Test::Container.keys).to include("foo.special")
61+
end
3062
end
3163
end

0 commit comments

Comments
 (0)