Skip to content

Commit

Permalink
fix(cache): check that modpaths still exist when finding mod root
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 7, 2023
1 parent 8798ccc commit d34c85d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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$", "")
Expand Down
59 changes: 58 additions & 1 deletion tests/core/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit d34c85d

Please sign in to comment.