Skip to content

Commit

Permalink
feat(plugin): improve error handling and show better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 23, 2024
1 parent d5686ef commit c02268a
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,52 +146,61 @@ function Spec:import(spec)

local imported = 0

---@type (string|(fun():LazyPluginSpec))[]
---@type {modname: string, load: fun():(LazyPluginSpec?, string?)}[]
local modspecs = {}

if type(import) == "string" then
Util.lsmod(import, function(modname, modpath)
modspecs[#modspecs + 1] = modname
package.preload[modname] = function()
return loadfile(modpath)()
end
modspecs[#modspecs + 1] = {
modname = modname,
load = function()
local mod, err = loadfile(modpath)
if mod then
return mod()
else
return nil, err
end
end,
}
end)
table.sort(modspecs, function(a, b)
return a.modname < b.modname
end)
table.sort(modspecs)
else
modspecs = { spec.import }
modspecs = { modname = import_name, load = spec.import }
end

for _, modspec in ipairs(modspecs) do
imported = imported + 1
local modname = type(modspec) == "string" and modspec or import_name
local modname = modspec.modname
Util.track({ import = modname })
self.importing = modname
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
local mod = type(modspec) == "function" and modspec() or require(modspec)
if type(mod) ~= "table" then
self.importing = nil
local mod, err = modspec.load()
if err then
self:error("Failed to load `" .. modname .. "`:\n" .. err)
elseif type(mod) ~= "table" then
return self:error(
"Invalid spec module: `"
.. modname
.. "`\nExpected a `table` of specs, but a `"
.. type(mod)
.. "` was returned instead"
)
else
self:normalize(mod)
end
self:normalize(mod)
self.importing = nil
Util.track()
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
self:error(msg)
self.importing = nil
Util.track()
end,
})
self.importing = nil
Util.track()
end
if imported == 0 then
self:error("No specs found for module " .. spec.import)
Expand Down

0 comments on commit c02268a

Please sign in to comment.