Skip to content

Commit

Permalink
feat(health): added spec parsing errors to :checkhealth
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 30, 2022
1 parent def5cc5 commit 32511a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
23 changes: 18 additions & 5 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion lua/lazy/health.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Util = require("lazy.util")
local Config = require("lazy.core.config")
local Plugin = require("lazy.core.plugin")

local M = {}

Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 32511a1

Please sign in to comment.