Skip to content

Commit 3a1a10c

Browse files
committed
fix(loader): implemented correct adding to rtp. fix #230, fix #226
1 parent c7122d6 commit 3a1a10c

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

lua/lazy/core/config.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ function M.setup(spec, opts)
182182
M.me = Util.norm(vim.fn.fnamemodify(M.me, ":p:h:h:h:h"))
183183
if M.options.performance.rtp.reset then
184184
vim.opt.rtp = {
185-
M.me,
186185
vim.fn.stdpath("config"),
186+
M.me,
187187
vim.env.VIMRUNTIME,
188188
vim.fn.fnamemodify(vim.v.progpath, ":p:h:h") .. "/lib/nvim",
189189
vim.fn.stdpath("config") .. "/after",

lua/lazy/core/loader.lua

+32-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function M.startup()
8080
end
8181
Util.track()
8282

83-
-- 3. load plugins from rtp, excluding after
83+
-- 3. load plugins from the original rtp, excluding after
8484
Util.track({ start = "rtp plugins" })
8585
for _, path in ipairs(rtp) do
8686
if not path:find("after/?$") then
@@ -172,11 +172,7 @@ function M._load(plugin, reason)
172172
Util.track({ plugin = plugin.name, start = reason.start })
173173
Handler.disable(plugin)
174174

175-
vim.opt.runtimepath:prepend(plugin.dir)
176-
local after = plugin.dir .. "/after"
177-
if vim.loop.fs_stat(after) then
178-
vim.opt.runtimepath:append(after)
179-
end
175+
M.add_to_rtp(plugin)
180176

181177
if plugin.dependencies then
182178
Util.try(function()
@@ -271,6 +267,36 @@ function M.source_runtime(...)
271267
end
272268
end
273269

270+
-- This does the same as runtime.c:add_pack_dir_to_rtp
271+
-- * find first after
272+
-- * find lazy pack path
273+
-- * insert right after lazy pack path or right before first after or at the end
274+
-- * insert after dir right before first after or append to the end
275+
---@param plugin LazyPlugin
276+
function M.add_to_rtp(plugin)
277+
local rtp = vim.api.nvim_get_runtime_file("", true)
278+
local idx_dir, idx_after
279+
280+
for i, path in ipairs(rtp) do
281+
if path == Config.me then
282+
idx_dir = i + 1
283+
elseif not idx_after and path:sub(-6, -1) == "/after" then
284+
idx_after = i + 1 -- +1 to offset the insert of the plugin dir
285+
idx_dir = idx_dir or i
286+
break
287+
end
288+
end
289+
290+
table.insert(rtp, idx_dir or (#rtp + 1), plugin.dir)
291+
292+
local after = plugin.dir .. "/after"
293+
if vim.loop.fs_stat(after) then
294+
table.insert(rtp, idx_after or (#rtp + 1), after)
295+
end
296+
297+
vim.opt.rtp = rtp
298+
end
299+
274300
function M.source(path)
275301
Util.track({ runtime = path })
276302
Util.try(function()

lua/lazy/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function M.bootstrap()
7777
lazypath,
7878
})
7979
end
80-
vim.opt.runtimepath:prepend(lazypath)
80+
vim.opt.rtp:prepend(lazypath)
8181
end
8282

8383
---@return LazyPlugin[]

0 commit comments

Comments
 (0)