fix(loader): remove all cached loaders on disable #41613

Problem:
When disabling, the loader list is traversed forwards while entries are
removed. As a result, some entries are skipped and remain active. Thus,
disabling `vim.loader` restores `_G.loadfile`, but does not fully
restore the original package loader chain.

Solution:
Traverse `package.loaders` in reverse when removing cached loaders.
Capture the original `package.loaders` list in the test, and assert that
the list is restored properly.
This commit is contained in:
Volodymyr Chernetskyi
2026-09-02 16:08:37 +02:00
committed by GitHub
parent 350fa5ad7c
commit 5bc7dbb13e
2 changed files with 4 additions and 1 deletions

View File

@@ -14,10 +14,12 @@ describe('vim.loader', function()
it('can be disabled', function()
exec_lua(function()
local orig_loader = _G.loadfile
local orig_loaders = { unpack(package.loaders) }
vim.loader.enable()
assert(orig_loader ~= _G.loadfile)
vim.loader.enable(false)
assert(orig_loader == _G.loadfile)
assert(vim.deep_equal(orig_loaders, package.loaders))
end)
end)