From 32511a121407aab44a839c68592860856c691f9f Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 30 Dec 2022 11:52:09 +0100 Subject: [PATCH] feat(health): added spec parsing errors to `:checkhealth` --- lua/lazy/core/plugin.lua | 23 ++++++++++++++++++----- lua/lazy/core/util.lua | 15 +++++++++++---- lua/lazy/health.lua | 13 ++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index d81070f..e6e1fec 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -9,12 +9,17 @@ local M = {} ---@class LazySpecLoader ---@field plugins table ---@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 diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 973d005..c207e5d 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -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 diff --git a/lua/lazy/health.lua b/lua/lazy/health.lua index 894d57e..57c24f6 100644 --- a/lua/lazy/health.lua +++ b/lua/lazy/health.lua @@ -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