Skip to content

Commit e897524

Browse files
committed
refactor: split autoload in autoload and check_load
1 parent f23a6ee commit e897524

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

Diff for: lua/lazy/core/cache.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function M.check_load(modname, modpath)
3636
if modname:sub(1, 4) == "lazy" then
3737
return
3838
end
39-
require("lazy.core.loader").autoload(modname, modpath)
39+
require("lazy.core.loader").check_load(modname, modpath)
4040
end
4141

4242
function M.disable()

Diff for: lua/lazy/core/loader.lua

+25-16
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ function M.startup()
5252
Util.track({ start = "rtp plugins" })
5353
for _, path in ipairs(rtp) do
5454
if not path:find("after/?$") then
55-
M.source_runtime(path, "plugin")
56-
M.ftdetect(path)
55+
M.packadd(path)
5756
end
5857
end
5958
Util.track()
@@ -83,6 +82,8 @@ function M.startup()
8382
end
8483
Util.track()
8584

85+
M.init_done = true
86+
8687
-- run plugin init
8788
Util.track({ start = "init" })
8889
for _, plugin in pairs(Config.plugins) do
@@ -94,7 +95,6 @@ function M.startup()
9495
end
9596
Util.track()
9697

97-
M.init_done = true
9898
Util.track()
9999
end
100100

@@ -180,33 +180,42 @@ end
180180
-- This loader is added as the very last one.
181181
-- This only hits when the modname is not cached and
182182
-- even then only once per plugin. So pretty much never.
183-
--
184-
-- lazy.module will call this when loading a cached file with modpath set.
185183
---@param modname string
186-
---@param modpath string?
187-
function M.autoload(modname, modpath)
188-
-- fast return when we know the modpath
189-
if modpath then
190-
local plugin = require("lazy.core.plugin").find(modpath)
191-
if plugin and not plugin._.loaded then
192-
M.load(plugin, { require = modname })
193-
end
194-
return
195-
end
184+
function M.autoload(modname)
196185
-- check if a lazy plugin should be loaded
197186
for _, plugin in pairs(Config.plugins) do
198187
if not plugin._.loaded then
199188
for _, pattern in ipairs({ ".lua", "/init.lua" }) do
200189
local path = plugin.dir .. "/lua/" .. modname:gsub("%.", "/") .. pattern
201190
if vim.loop.fs_stat(path) then
202191
M.load(plugin, { require = modname })
192+
-- check if the module has been loaded in the meantime
193+
if type(package.loaded[modname]) == "table" then
194+
local mod = package.loaded[modname]
195+
return function()
196+
return mod
197+
end
198+
end
203199
local chunk, err = loadfile(path)
204200
return chunk or error(err)
205201
end
206202
end
207203
end
208204
end
209-
return modname .. " not found in unloaded opt plugins"
205+
return modname .. " not found in lazy plugins"
206+
end
207+
208+
-- lazy.cache will call this when loading a cached file with modpath set.
209+
---@param modname string
210+
---@param modpath string
211+
function M.check_load(modname, modpath)
212+
-- no need to check anything before init
213+
if M.init_done then
214+
local plugin = require("lazy.core.plugin").find(modpath)
215+
if plugin and not plugin._.loaded then
216+
M.load(plugin, { require = modname })
217+
end
218+
end
210219
end
211220

212221
return M

0 commit comments

Comments
 (0)