summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkylo252 <[email protected]>2021-11-02 16:58:55 +0100
committerGitHub <[email protected]>2021-11-02 16:58:55 +0100
commit32ca5afa4ad21f1a616cc30323c272191e7548c1 (patch)
tree1d4d0a40330f426b51b8d62409d23869887bf7dd
parente8693406babee724dd51293dc6dad10931dbef45 (diff)
feat: better error handling for packer (#1883)
-rw-r--r--.github/workflows/install.yaml6
-rw-r--r--init.lua2
-rw-r--r--lua/lvim/bootstrap.lua2
-rw-r--r--lua/lvim/config/init.lua4
-rw-r--r--lua/lvim/core/commands.lua1
-rw-r--r--lua/lvim/core/which-key.lua2
-rw-r--r--lua/lvim/plugin-loader.lua58
-rw-r--r--lua/lvim/utils/hooks.lua3
-rw-r--r--tests/bootstrap_spec.lua8
9 files changed, 51 insertions, 35 deletions
diff --git a/.github/workflows/install.yaml b/.github/workflows/install.yaml
index fa1b9be8..02400ce4 100644
--- a/.github/workflows/install.yaml
+++ b/.github/workflows/install.yaml
@@ -33,12 +33,6 @@ jobs:
run: |
./utils/installer/install.sh --local --no-install-dependencies
- - name: Test LunarVim PackerCompile
- run: if "$HOME"/.local/bin/lvim --headless +PackerCompile -c ':qall' 2>&1|grep -q 'Error'; then false; fi
-
- - name: Test LunarVim Health
- run: if "$HOME"/.local/bin/lvim --headless +checkhealth -c ':qall' 2>&1|grep -q 'Error'; then false; fi
-
- name: Run unit-tests
# NOTE: make sure to adjust the timeout if you start adding a lot of tests
timeout-minutes: 4
diff --git a/init.lua b/init.lua
index 9bc5c9ee..80fb68a6 100644
--- a/init.lua
+++ b/init.lua
@@ -10,7 +10,7 @@ require("lvim.bootstrap"):init(base_dir)
require("lvim.config"):load()
local plugins = require "lvim.plugins"
-require("lvim.plugin-loader"):load { plugins, lvim.plugins }
+require("lvim.plugin-loader").load { plugins, lvim.plugins }
local Log = require "lvim.core.log"
Log:debug "Starting LunarVim"
diff --git a/lua/lvim/bootstrap.lua b/lua/lvim/bootstrap.lua
index e17c79db..74a9bf45 100644
--- a/lua/lvim/bootstrap.lua
+++ b/lua/lvim/bootstrap.lua
@@ -87,7 +87,7 @@ function M:init(base_dir)
require("lvim.config"):init()
- require("lvim.plugin-loader"):init {
+ require("lvim.plugin-loader").init {
package_root = self.pack_dir,
install_path = self.packer_install_dir,
}
diff --git a/lua/lvim/config/init.lua b/lua/lvim/config/init.lua
index e89cb260..1256e236 100644
--- a/lua/lvim/config/init.lua
+++ b/lua/lvim/config/init.lua
@@ -102,8 +102,8 @@ function M:reload()
local plugins = require "lvim.plugins"
utils.toggle_autoformat()
local plugin_loader = require "lvim.plugin-loader"
- plugin_loader:cache_reset()
- plugin_loader:load { plugins, lvim.plugins }
+ plugin_loader.cache_clear()
+ plugin_loader.load { plugins, lvim.plugins }
vim.cmd ":PackerInstall"
vim.cmd ":PackerCompile"
-- vim.cmd ":PackerClean"
diff --git a/lua/lvim/core/commands.lua b/lua/lvim/core/commands.lua
index 1b408eb0..61148889 100644
--- a/lua/lvim/core/commands.lua
+++ b/lua/lvim/core/commands.lua
@@ -14,6 +14,7 @@ M.defaults = {
[[ command! LvimInfo lua require('lvim.core.info').toggle_popup(vim.bo.filetype) ]],
[[ command! LvimCacheReset lua require('lvim.utils.hooks').reset_cache() ]],
[[ command! LvimUpdate lua require('lvim.bootstrap').update() ]],
+ [[ command! LvimSyncCorePlugins lua require('lvim.plugin-loader'):sync_core_plugins() ]],
[[ command! LvimReload lua require('lvim.config'):reload() ]],
}
diff --git a/lua/lvim/core/which-key.lua b/lua/lvim/core/which-key.lua
index 316abe3f..b036e376 100644
--- a/lua/lvim/core/which-key.lua
+++ b/lua/lvim/core/which-key.lua
@@ -98,7 +98,7 @@ M.config = function()
name = "Packer",
c = { "<cmd>PackerCompile<cr>", "Compile" },
i = { "<cmd>PackerInstall<cr>", "Install" },
- r = { "<cmd>lua require('lvim.plugin-loader').cache_reset()<cr>", "Re-compile" },
+ r = { "<cmd>lua require('lvim.plugin-loader').recompile()<cr>", "Re-compile" },
s = { "<cmd>PackerSync<cr>", "Sync" },
S = { "<cmd>PackerStatus<cr>", "Status" },
u = { "<cmd>PackerUpdate<cr>", "Update" },
diff --git a/lua/lvim/plugin-loader.lua b/lua/lvim/plugin-loader.lua
index f145a958..c4bd7373 100644
--- a/lua/lvim/plugin-loader.lua
+++ b/lua/lvim/plugin-loader.lua
@@ -5,7 +5,9 @@ local Log = require "lvim.core.log"
-- we need to reuse this outside of init()
local compile_path = get_config_dir() .. "/plugin/packer_compiled.lua"
-function plugin_loader:init(opts)
+local _, packer = pcall(require, "packer")
+
+function plugin_loader.init(opts)
opts = opts or {}
local install_path = opts.install_path or vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
@@ -16,14 +18,10 @@ function plugin_loader:init(opts)
vim.cmd "packadd packer.nvim"
end
- local packer_ok, packer = pcall(require, "packer")
- if not packer_ok then
- return
- end
-
packer.init {
package_root = package_root,
compile_path = compile_path,
+ log = { level = "warn" },
git = { clone_timeout = 300 },
display = {
open_fn = function()
@@ -31,36 +29,51 @@ function plugin_loader:init(opts)
end,
},
}
+end
- self.packer = packer
- return self
+-- packer expects a space separated list
+local function pcall_packer_command(cmd, kwargs)
+ local status_ok, msg = pcall(function()
+ require("packer")[cmd](unpack(kwargs or {}))
+ end)
+ if not status_ok then
+ Log:warn(cmd .. " failed with: " .. vim.inspect(msg))
+ Log:trace(vim.inspect(vim.fn.eval "v:errmsg"))
+ end
end
-function plugin_loader:cache_clear()
+function plugin_loader.cache_clear()
if vim.fn.delete(compile_path) == 0 then
Log:debug "deleted packer_compiled.lua"
end
end
-function plugin_loader:cache_reset()
- plugin_loader:cache_clear()
- require("packer").compile()
+function plugin_loader.recompile()
+ plugin_loader.cache_clear()
+ pcall_packer_command "compile"
if utils.is_file(compile_path) then
Log:debug "generated packer_compiled.lua"
end
end
-function plugin_loader:load(configurations)
- return self.packer.startup(function(use)
- for _, plugins in ipairs(configurations) do
- for _, plugin in ipairs(plugins) do
- use(plugin)
+function plugin_loader.load(configurations)
+ Log:debug "loading plugins configuration"
+ local status_ok, _ = xpcall(function()
+ packer.startup(function(use)
+ for _, plugins in ipairs(configurations) do
+ for _, plugin in ipairs(plugins) do
+ use(plugin)
+ end
end
- end
- end)
+ end)
+ end, debug.traceback)
+ if not status_ok then
+ Log:warn "problems detected while loading plugins' configurations"
+ Log:trace(debug.traceback())
+ end
end
-function plugin_loader:get_core_plugins()
+function plugin_loader.get_core_plugins()
local list = {}
local plugins = require "lvim.plugins"
for _, item in pairs(plugins) do
@@ -69,9 +82,10 @@ function plugin_loader:get_core_plugins()
return list
end
-function plugin_loader:sync_core_plugins()
+function plugin_loader.sync_core_plugins()
local core_plugins = plugin_loader.get_core_plugins()
- vim.cmd("PackerSync " .. unpack(core_plugins))
+ Log:trace(string.format("Syncing core plugins: [%q]", table.concat(core_plugins, ", ")))
+ pcall_packer_command("sync", core_plugins)
end
return plugin_loader
diff --git a/lua/lvim/utils/hooks.lua b/lua/lvim/utils/hooks.lua
index cc884523..0fe4a7fd 100644
--- a/lua/lvim/utils/hooks.lua
+++ b/lua/lvim/utils/hooks.lua
@@ -15,8 +15,7 @@ end
---Tip: Useful for clearing any outdated settings
function M.reset_cache()
_G.__luacache.clear_cache()
-
- plugin_loader:cache_reset()
+ require("lvim.plugin-loader").recompile()
package.loaded["lvim.lsp.templates"] = nil
Log:debug "Re-generatring ftplugin template files"
diff --git a/tests/bootstrap_spec.lua b/tests/bootstrap_spec.lua
index d92e213d..c86d22d4 100644
--- a/tests/bootstrap_spec.lua
+++ b/tests/bootstrap_spec.lua
@@ -25,4 +25,12 @@ a.describe("initial start", function()
a.it("should be able to run treesitter without errors", function()
assert.truthy(vim.treesitter.highlighter.active)
end)
+
+ a.it("should be able to pass basic checkhealth without errors", function()
+ vim.cmd "checkhealth nvim"
+ local errmsg = vim.fn.eval "v:errmsg"
+ local exception = vim.fn.eval "v:exception"
+ assert.equal("", errmsg) -- v:errmsg was not updated.
+ assert.equal("", exception)
+ end)
end)