From c8e2091e6d2836b587b9892e0fb64afaec36926a Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 08:51:54 +0200 Subject: [PATCH] fix(plugin): dont allow `dir` changes when we already loaded files from the plugin's old dir. Show an error in this case. Fixes #993 --- lua/lazy/core/loader.lua | 1 + lua/lazy/core/plugin.lua | 13 +++++++++++-- lua/lazy/types.lua | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 9e62371..bc41864 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -495,6 +495,7 @@ end function M.auto_load(modname, modpath) local plugin = Plugin.find(modpath) if plugin and modpath:find(plugin.dir, 1, true) == 1 then + plugin._.rtp_loaded = true -- don't load if we're loading specs or if the plugin is already loaded if not (Plugin.loading or plugin._.loaded) then if plugin.module == false then diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index a915d8f..a51806e 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -459,10 +459,19 @@ function Spec:merge(old, new) end local new_dir = new._.dir or old._.dir or (new.name and (Config.options.root .. "/" .. new.name)) or nil - if new_dir ~= new.dir then - self:warn("Plugin `" .. new.name .. "` changed `dir`:\n- from: `" .. new.dir .. "`\n- to: `" .. new_dir .. "`") + if new_dir ~= old.dir then + local msg = "Plugin `" .. new.name .. "` changed `dir`:\n- from: `" .. old.dir .. "`\n- to: `" .. new_dir .. "`" + if new._.rtp_loaded or old._.rtp_loaded then + msg = msg + .. "\n\nThis plugin was already partially loaded, so we did not change it's `dir`.\nPlease fix your config." + self:error(msg) + new_dir = old.dir + else + self:warn(msg) + end end new.dir = new_dir + new._.rtp_loaded = new._.rtp_loaded or old._.rtp_loaded new._.super = old setmetatable(new, { __index = old }) diff --git a/lua/lazy/types.lua b/lua/lazy/types.lua index 0434359..dcbcf18 100644 --- a/lua/lazy/types.lua +++ b/lua/lazy/types.lua @@ -19,6 +19,7 @@ ---@field super? LazyPlugin ---@field module? string ---@field dir? string Explicit dir or dev set for this plugin +---@field rtp_loaded? boolean ---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?