feat(health): added spec parsing errors to `:checkhealth`

This commit is contained in:
Folke Lemaitre 2022-12-30 11:52:09 +01:00
parent def5cc5816
commit 32511a1214
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 41 additions and 10 deletions

View File

@ -9,12 +9,17 @@ local M = {}
---@class LazySpecLoader
---@field plugins table<string, LazyPlugin>
---@field errors string[]
---@field opts LazySpecOptions
local Spec = {}
M.Spec = Spec
---@alias LazySpecOptions {show_errors: boolean}
---@param spec? LazySpec
function Spec.new(spec)
---@param opts? LazySpecOptions
function Spec.new(spec, opts)
local self = setmetatable({}, { __index = Spec })
self.opts = opts or {}
self.plugins = {}
self.errors = {}
if spec then
@ -78,7 +83,9 @@ end
function Spec:error(error)
self.errors[#self.errors + 1] = error
Util.error(error)
if self.opts.show_errors ~= false then
Util.error(error)
end
end
---@param spec LazySpec
@ -185,8 +192,9 @@ function M.update_state()
end
end
function M.spec()
local spec = Spec.new()
---@param opts? LazySpecOptions
function M.spec(opts)
local spec = Spec.new(nil, opts)
if type(Config.spec) == "string" then
-- spec is a module
@ -196,7 +204,12 @@ function M.spec()
package.loaded[modname] = nil
Util.try(function()
spec:normalize(Cache.require(modname))
end, "Failed to load **" .. modname .. "**")
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
spec:error(msg)
end,
})
end
Util.lsmod(Config.spec --[[@as string]], _load)
else

View File

@ -48,7 +48,10 @@ function M.norm(path)
return path:sub(-1) == "/" and path:sub(1, -2) or path
end
function M.try(fn, msg)
---@param opts? string|{msg:string, on_error:fun(msg)}
function M.try(fn, opts)
opts = type(opts) == "string" and { msg = opts } or opts or {}
local msg = opts.msg
-- error handler
local error_handler = function(err)
local Config = require("lazy.core.config")
@ -77,9 +80,13 @@ function M.try(fn, msg)
if #trace > 0 then
msg = msg .. "\n\n# stacktrace:\n" .. table.concat(trace, "\n")
end
vim.schedule(function()
M.error(msg)
end)
if opts.on_error then
opts.on_error(msg)
else
vim.schedule(function()
M.error(msg)
end)
end
return err
end

View File

@ -1,5 +1,6 @@
local Util = require("lazy.util")
local Config = require("lazy.core.config")
local Plugin = require("lazy.core.plugin")
local M = {}
@ -48,7 +49,8 @@ function M.check()
"cond",
"_",
}
for _, plugin in pairs(Config.plugins) do
local spec = Plugin.spec({ show_errors = false })
for _, plugin in pairs(spec.plugins) do
for key in pairs(plugin) do
if not vim.tbl_contains(valid, key) then
if key ~= "module" or type(plugin.module) ~= "boolean" then
@ -57,6 +59,15 @@ function M.check()
end
end
end
if #spec.errors > 0 then
vim.health.report_error("Errors were reported when loading your specs:")
for _, error in ipairs(spec.errors) do
local lines = vim.split(error, "\n")
for _, line in ipairs(lines) do
vim.health.report_error(line)
end
end
end
end
return M