diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index edba74a..709e321 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -262,7 +262,7 @@ function M.find_root(modname) if M.cache[modname] then -- check if modname is in cache local modpath = M.cache[modname].modpath - if M.check_path(modname, modpath) then + if M.check_path(modname, modpath) and vim.loop.fs_stat(modpath) then local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "") return root end @@ -271,7 +271,7 @@ function M.find_root(modname) -- check for any children in the cache for child, entry in pairs(M.cache) do if child:find(modname, 1, true) == 1 then - if M.check_path(child, entry.modpath) then + if M.check_path(child, entry.modpath) and vim.loop.fs_stat(entry.modpath) then local basename = modname:gsub("%.", "/") local childbase = child:gsub("%.", "/") local ret = entry.modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "") diff --git a/tests/core/util_spec.lua b/tests/core/util_spec.lua index 655b71f..53566a3 100644 --- a/tests/core/util_spec.lua +++ b/tests/core/util_spec.lua @@ -3,11 +3,19 @@ local Cache = require("lazy.core.cache") local Helpers = require("tests.helpers") describe("util", function() + local rtp = vim.opt.rtp:get() before_each(function() + vim.opt.rtp = rtp + for k, v in pairs(package.loaded) do + if k:find("^foobar") then + package.loaded[k] = nil + end + end Helpers.fs_rm("") end) it("lsmod lists all mods in dir", function() + vim.opt.rtp:append(Helpers.path("")) local tests = { { root = "lua/foo", @@ -35,7 +43,6 @@ describe("util", function() }, } - vim.opt.rtp:append(Helpers.path("")) for t, test in ipairs(tests) do local expected = vim.deepcopy(test.mods) table.sort(expected) @@ -74,4 +81,54 @@ describe("util", function() assert.same(expected, mods) end end) + + it("find the correct root with dels", function() + Cache.cache = {} + Cache.indexed = {} + Cache.indexed_rtp = false + vim.opt.rtp:append(Helpers.path("old")) + Helpers.fs_create({ "old/lua/foobar/init.lua" }) + require("foobar") + local root = Cache.find_root("foobar") + assert(root, "foobar root not found") + assert.same(Helpers.path("old/lua/foobar"), root) + + Helpers.fs_rm("old/") + + -- vim.opt.rtp = rtp + Cache.indexed = {} + Cache.indexed_rtp = false + vim.opt.rtp:append(Helpers.path("new")) + Helpers.fs_create({ "new/lua/foobar/init.lua" }) + root = Cache.find_root("foobar") + assert(root, "foobar root not found") + assert.same(Helpers.path("new/lua/foobar"), root) + end) + + it("find the correct root with mod dels", function() + Cache.cache = {} + Cache.indexed = {} + Cache.indexed_rtp = false + Cache.topmods = {} + Cache.enabled = true + vim.opt.rtp:append(Helpers.path("old")) + Helpers.fs_create({ "old/lua/foobar/test.lua" }) + Cache.cache["foobar.test"] = { modpath = Helpers.path("old/lua/foobar/test.lua") } + local root = Cache.find_root("foobar") + assert(root, "foobar root not found") + assert.same(Helpers.path("old/lua/foobar"), root) + assert(not Cache.cache["foobar"], "foobar should not be in cache") + assert(Cache.cache["foobar.test"], "foobar.test not found in cache") + + Helpers.fs_rm("old/") + + -- vim.opt.rtp = rtp + Cache.indexed = {} + Cache.indexed_rtp = false + vim.opt.rtp:append(Helpers.path("new")) + Helpers.fs_create({ "new/lua/foobar/test.lua" }) + root = Cache.find_root("foobar") + assert(root, "foobar root not found") + assert.same(Helpers.path("new/lua/foobar"), root) + end) end)