fix(cache): check that modpaths still exist when finding mod root

This commit is contained in:
Folke Lemaitre 2023-01-07 22:24:46 +01:00
parent 8798ccc950
commit d34c85d580
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 60 additions and 3 deletions

View File

@ -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$", "")

View File

@ -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)