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

View File

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

View File

@ -1,5 +1,6 @@
local Util = require("lazy.util") local Util = require("lazy.util")
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
local Plugin = require("lazy.core.plugin")
local M = {} local M = {}
@ -48,7 +49,8 @@ function M.check()
"cond", "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 for key in pairs(plugin) do
if not vim.tbl_contains(valid, key) then if not vim.tbl_contains(valid, key) then
if key ~= "module" or type(plugin.module) ~= "boolean" then if key ~= "module" or type(plugin.module) ~= "boolean" then
@ -57,6 +59,15 @@ function M.check()
end end
end 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 end
return M return M