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 | 97 +++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 2d5d4935..eab29c29 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -131,65 +131,72 @@ 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 + if plugin and not plugin._.loaded then + M._load(plugin, reason) end + end +end - ---@cast plugin LazyPlugin +---@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 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 + 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 - table.insert(M.loading, plugin) + ---@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 - Util.track({ plugin = plugin.name, start = reason.start }) - Handler.disable(plugin) + table.insert(M.loading, 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 + Util.track({ plugin = plugin.name, start = reason.start }) + Handler.disable(plugin) - if plugin.dependencies then - Util.try(function() - M.load(plugin.dependencies, {}) - end, "Failed to load deps for " .. plugin.name) - end + vim.opt.runtimepath:prepend(plugin.dir) + local after = plugin.dir .. "/after" + if vim.loop.fs_stat(after) then + vim.opt.runtimepath:append(after) + end - M.packadd(plugin.dir) - if plugin.config then - M.config(plugin) - end + if plugin.dependencies then + Util.try(function() + M.load(plugin.dependencies, {}) + end, "Failed to load deps for " .. plugin.name) + end - plugin._.loaded.time = Util.track().time - table.remove(M.loading) - vim.schedule(function() - vim.cmd("do User LazyRender") - end) - 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