Skip to content

Commit

Permalink
fix(cache): check package.loaded after auto-load and return existing …
Browse files Browse the repository at this point in the history
…module if present. Fixes #224
  • Loading branch information
folke committed Dec 29, 2022
1 parent b8c5ab5 commit 044e28b
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ function M.disable()
M.enabled = false
end

function M.check_loaded(modname)
---@diagnostic disable-next-line: no-unknown
local mod = package.loaded[modname]
if type(mod) == "table" then
return function()
return mod
end
end
end

---@param modname string
---@return any
function M.loader(modname)
Expand All @@ -111,12 +121,6 @@ function M.loader(modname)
local chunk, err
if entry and M.check_path(modname, entry.modpath) then
M.stats.find.total = M.stats.find.total + 1
local mod = package.loaded[modname]
if type(mod) == "table" then
return function()
return mod
end
end
chunk, err = M.load(modname, entry.modpath)
end
if not chunk then
Expand All @@ -127,7 +131,10 @@ function M.loader(modname)
if M.enabled then
chunk, err = M.load(modname, modpath)
else
chunk, err = M._loadfile(modpath)
chunk = M.check_loaded(modname)
if not chunk then
chunk, err = M._loadfile(modpath)
end
end
end
end
Expand All @@ -145,6 +152,11 @@ end
---@param modpath string
---@return function?, string? error_message
function M.load(modkey, modpath)
local chunk, err
chunk = M.check_loaded(modkey)
if chunk then
return chunk
end
modpath = modpath:gsub("\\", "/")
local hash = M.hash(modpath)
if not hash then
Expand All @@ -158,7 +170,7 @@ function M.load(modkey, modpath)
entry.used = os.time()
if M.eq(entry.hash, hash) then
-- found in cache and up to date
local chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
return chunk, err
end
Expand All @@ -175,7 +187,7 @@ function M.load(modkey, modpath)
end)
end

local chunk, err = M._loadfile(entry.modpath)
chunk, err = M._loadfile(entry.modpath)
if chunk then
M.dirty = true
entry.chunk = string.dump(chunk)
Expand All @@ -184,7 +196,9 @@ function M.load(modkey, modpath)
end

function M.require(modname)
---@diagnostic disable-next-line: no-unknown
local mod = M.loader(modname)()
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = mod
return mod
end
Expand Down

0 comments on commit 044e28b

Please sign in to comment.