Quellcode durchsuchen

feat: better error handling for packer (#1883)

kylo252 vor 3 Jahren
Ursprung
Commit
32ca5afa4a

+ 0 - 6
.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

+ 1 - 1
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"

+ 1 - 1
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,
   }

+ 2 - 2
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"

+ 1 - 0
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() ]],
 }
 

+ 1 - 1
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" },

+ 36 - 22
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

+ 1 - 2
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"

+ 8 - 0
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)