diff --git a/lib/bootsnap/load_path_cache/cache.rb b/lib/bootsnap/load_path_cache/cache.rb index 71754afe..6ca755e5 100644 --- a/lib/bootsnap/load_path_cache/cache.rb +++ b/lib/bootsnap/load_path_cache/cache.rb @@ -14,10 +14,10 @@ def initialize(store, path_obj, development_mode: false) reinitialize end - # Does this directory exist as a child of one of the path items? - # e.g. given "/a/b/c/d" exists, and the path is ["/a/b"], has_dir?("c/d") - # is true. - def has_dir?(dir) + # What is the path item that contains the dir as child? + # e.g. given "/a/b/c/d" exists, and the path is ["/a/b"], load_dir("c/d") + # is "/a/b". + def load_dir(dir) reinitialize if stale? @mutex.synchronize { @dirs[dir] } end @@ -108,7 +108,7 @@ def reinitialize(path_obj = @path_obj) @path_obj = path_obj ChangeObserver.register(self, @path_obj) @index = {} - @dirs = Hash.new(false) + @dirs = {} @generated_at = now push_paths_locked(*@path_obj) end @@ -135,7 +135,7 @@ def push_paths_locked(*paths) expanded_path = p.expanded_path entries, dirs = p.entries_and_dirs(@store) # push -> low precedence -> set only if unset - dirs.each { |dir| @dirs[dir] ||= true } + dirs.each { |dir| @dirs[dir] ||= path } entries.each { |rel| @index[rel] ||= expanded_path } end end @@ -149,7 +149,7 @@ def unshift_paths_locked(*paths) expanded_path = p.expanded_path entries, dirs = p.entries_and_dirs(@store) # unshift -> high precedence -> unconditional set - dirs.each { |dir| @dirs[dir] = true } + dirs.each { |dir| @dirs[dir] = path } entries.each { |rel| @index[rel] = expanded_path } end end diff --git a/lib/bootsnap/load_path_cache/core_ext/active_support.rb b/lib/bootsnap/load_path_cache/core_ext/active_support.rb index 419cb075..d65aa254 100644 --- a/lib/bootsnap/load_path_cache/core_ext/active_support.rb +++ b/lib/bootsnap/load_path_cache/core_ext/active_support.rb @@ -34,7 +34,7 @@ def search_for_file(path) end def autoloadable_module?(path_suffix) - Bootsnap::LoadPathCache.autoload_paths_cache.has_dir?(path_suffix) + Bootsnap::LoadPathCache.autoload_paths_cache.load_dir(path_suffix) end def remove_constant(const) diff --git a/test/load_path_cache/cache_test.rb b/test/load_path_cache/cache_test.rb index b5086cd5..09150e78 100644 --- a/test/load_path_cache/cache_test.rb +++ b/test/load_path_cache/cache_test.rb @@ -69,9 +69,9 @@ def test_pushed_paths_have_lower_precedence def test_directory_caching cache = Cache.new(NullCache, [@dir1]) - assert cache.has_dir?("foo") - assert cache.has_dir?("foo/bar") - refute cache.has_dir?("bar") + assert_equal @dir1, cache.load_dir("foo") + assert_equal @dir1, cache.load_dir("foo/bar") + assert_nil cache.load_dir("bar") end def test_extension_permutations