Skip to content

Commit

Permalink
Fix autoload_path_set_by_me_for? with inceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Sep 2, 2024
1 parent 8100bd1 commit f9b21aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def all_dirs
if autoload_path = cref.autoload?
autoload_path if autoloads.key?(autoload_path)
else
Registry.inception?(cref.path)
Registry.inception?(cref.path, self)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/zeitwerk/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ def register_inception(cpath, abspath, loader)

# @private
# @sig (String) -> String?
def inception?(cpath)
def inception?(cpath, registered_by_loader=nil)
if pair = inceptions[cpath]
pair.first
abspath, loader = pair
if registered_by_loader.nil? || registered_by_loader.equal?(loader)
abspath
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/lib/zeitwerk/test_reloading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ def silence_exceptions_in_threads
end
end

test 'reloading namespaces that are inceptions in other projects' do
on_teardown do
remove_const :MyGem
delete_loaded_feature "lib/my_gem.rb"
end

gem_files = [["lib/my_gem.rb", <<~EOS]]
Zeitwerk::Loader.for_gem.setup
module MyGem; end
EOS

app_files = [["app/my_gem/foo.rb", 'MyGem::Foo = true']]

with_files(gem_files + app_files) do
with_load_path("lib") do
require 'my_gem'

loader.push_dir("app")
loader.enable_reloading
loader.setup

assert MyGem::Foo
loader.reload
assert MyGem::Foo
end
end
end

test "reloading raises if the flag is not set" do
e = assert_raises(Zeitwerk::ReloadingDisabledError) do
loader = Zeitwerk::Loader.new
Expand Down

0 comments on commit f9b21aa

Please sign in to comment.