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

This commit is contained in:
Folke Lemaitre 2023-10-15 08:51:54 +02:00
parent 3dc413d6fd
commit c8e2091e6d
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 13 additions and 2 deletions

View File

@ -495,6 +495,7 @@ end
function M.auto_load(modname, modpath) function M.auto_load(modname, modpath)
local plugin = Plugin.find(modpath) local plugin = Plugin.find(modpath)
if plugin and modpath:find(plugin.dir, 1, true) == 1 then 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 -- don't load if we're loading specs or if the plugin is already loaded
if not (Plugin.loading or plugin._.loaded) then if not (Plugin.loading or plugin._.loaded) then
if plugin.module == false then if plugin.module == false then

View File

@ -459,10 +459,19 @@ function Spec:merge(old, new)
end end
local new_dir = new._.dir or old._.dir or (new.name and (Config.options.root .. "/" .. new.name)) or nil 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 if new_dir ~= old.dir then
self:warn("Plugin `" .. new.name .. "` changed `dir`:\n- from: `" .. new.dir .. "`\n- to: `" .. new_dir .. "`") 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 end
new.dir = new_dir new.dir = new_dir
new._.rtp_loaded = new._.rtp_loaded or old._.rtp_loaded
new._.super = old new._.super = old
setmetatable(new, { __index = old }) setmetatable(new, { __index = old })

View File

@ -19,6 +19,7 @@
---@field super? LazyPlugin ---@field super? LazyPlugin
---@field module? string ---@field module? string
---@field dir? string Explicit dir or dev set for this plugin ---@field dir? string Explicit dir or dev set for this plugin
---@field rtp_loaded? boolean
---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table? ---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?