From 956164d27dc02b8d3c21c9ef7cc9028d854b0978 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 28 Dec 2022 17:56:20 +0100 Subject: [PATCH] fix(loader): show proper error message when trying to load a plugin that is not installed. Fixes #201. Fixes #202 --- lua/lazy/core/loader.lua | 113 +++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 2d5d493..eab29c2 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -131,67 +131,74 @@ function M.load(plugins, reason) ---@cast plugins (string|LazyPlugin)[] for _, plugin in pairs(plugins) do - local try_load = true - if type(plugin) == "string" then - if not Config.plugins[plugin] then - Util.error("Plugin " .. plugin .. " not found") - try_load = false - else + if Config.plugins[plugin] then plugin = Config.plugins[plugin] + else + Util.error("Plugin " .. plugin .. " not found") + plugin = nil end end - - if try_load and plugin.cond then - try_load = plugin.cond == true or (type(plugin.cond) == "function" and plugin.cond()) or false - plugin._.cond = try_load - end - - ---@cast plugin LazyPlugin - - if try_load and not plugin._.loaded then - ---@diagnostic disable-next-line: assign-type-mismatch - plugin._.loaded = {} - for k, v in pairs(reason) do - plugin._.loaded[k] = v - end - if #M.loading > 0 then - plugin._.loaded.plugin = M.loading[#M.loading].name - elseif reason.require then - plugin._.loaded.source = Util.get_source() - end - - table.insert(M.loading, plugin) - - Util.track({ plugin = plugin.name, start = reason.start }) - Handler.disable(plugin) - - vim.opt.runtimepath:prepend(plugin.dir) - local after = plugin.dir .. "/after" - if vim.loop.fs_stat(after) then - vim.opt.runtimepath:append(after) - end - - if plugin.dependencies then - Util.try(function() - M.load(plugin.dependencies, {}) - end, "Failed to load deps for " .. plugin.name) - end - - M.packadd(plugin.dir) - if plugin.config then - M.config(plugin) - end - - plugin._.loaded.time = Util.track().time - table.remove(M.loading) - vim.schedule(function() - vim.cmd("do User LazyRender") - end) + if plugin and not plugin._.loaded then + M._load(plugin, reason) end end end +---@param plugin LazyPlugin +---@param reason {[string]:string} +function M._load(plugin, reason) + if not plugin._.installed then + return Util.error("Plugin " .. plugin.name .. " is not installed") + end + + if plugin.cond ~= nil then + if plugin.cond == false or (type(plugin.cond) == "function" and not plugin.cond()) then + plugin._.cond = false + return + end + end + + ---@diagnostic disable-next-line: assign-type-mismatch + plugin._.loaded = {} + for k, v in pairs(reason) do + plugin._.loaded[k] = v + end + if #M.loading > 0 then + plugin._.loaded.plugin = M.loading[#M.loading].name + elseif reason.require then + plugin._.loaded.source = Util.get_source() + end + + table.insert(M.loading, plugin) + + Util.track({ plugin = plugin.name, start = reason.start }) + Handler.disable(plugin) + + vim.opt.runtimepath:prepend(plugin.dir) + local after = plugin.dir .. "/after" + if vim.loop.fs_stat(after) then + vim.opt.runtimepath:append(after) + end + + if plugin.dependencies then + Util.try(function() + M.load(plugin.dependencies, {}) + end, "Failed to load deps for " .. plugin.name) + end + + M.packadd(plugin.dir) + if plugin.config then + M.config(plugin) + end + + plugin._.loaded.time = Util.track().time + table.remove(M.loading) + vim.schedule(function() + vim.cmd("do User LazyRender") + end) +end + --- runs plugin config ---@param plugin LazyPlugin function M.config(plugin)