Skip to content

Commit

Permalink
fix(loader): show proper error message when trying to load a plugin t…
Browse files Browse the repository at this point in the history
…hat is not installed. Fixes #201. Fixes #202
  • Loading branch information
folke committed Dec 28, 2022
1 parent 5f423b2 commit 956164d
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 956164d

Please sign in to comment.