perf: new file-based cache that ensures correct rtp order (#532)

* perf: new file-based cache that ensures rtp is alweays correct and will cache all files, including those after startup

* refactor: new cache

* test: fix tests

* fix(cache): cache file names on Windows

* feat(cache): allow to disable the cache

* docs: updated cache settings
This commit is contained in:
Folke Lemaitre 2023-02-13 12:01:56 +01:00 committed by GitHub
parent 06f835d0b4
commit 462633bae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 354 additions and 549 deletions

View file

@ -51,10 +51,8 @@ describe("util", function()
local files = Helpers.fs_create(test.files)
-- test with empty cache
Cache.cache = {}
Cache.indexed = {}
Cache.indexed_rtp = false
local root = Cache.find_root(test.mod)
package.loaded["lazy.core.cache"] = nil
local root = Util.find_root(test.mod)
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
assert.same(Helpers.path(test.root), root)
local mods = {}
@ -65,13 +63,8 @@ describe("util", function()
assert.same(expected, mods)
-- fill the cache
Cache.cache = {}
for i, file in ipairs(files) do
Cache.cache[test.mods[i]] = { modpath = file }
end
Cache.indexed = {}
Cache.indexed_rtp = false
root = Cache.find_root(test.mod)
package.loaded["lazy.core.cache"] = nil
root = Util.find_root(test.mod)
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
assert.same(Helpers.path(test.root), root)
mods = {}
@ -85,12 +78,12 @@ describe("util", function()
it("find the correct root with dels", function()
Cache.cache = {}
Cache.indexed = {}
Cache.indexed_rtp = false
Cache._topmods = {}
Cache.topmods_rtp = false
vim.opt.rtp:append(Helpers.path("old"))
Helpers.fs_create({ "old/lua/foobar/init.lua" })
Cache.cache["foobar"] = { modpath = Helpers.path("old/lua/foobar/init.lua") }
local root = Cache.find_root("foobar")
local root = Util.find_root("foobar")
assert(root, "foobar root not found")
assert.same(Helpers.path("old/lua/foobar"), root)
@ -98,24 +91,22 @@ describe("util", function()
assert(not vim.loop.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
-- vim.opt.rtp = rtp
Cache.indexed = {}
Cache.indexed_rtp = false
Cache._topmods = {}
vim.opt.rtp:append(Helpers.path("new"))
Helpers.fs_create({ "new/lua/foobar/init.lua" })
root = Cache.find_root("foobar")
root = Util.find_root("foobar")
assert(root, "foobar root not found")
assert.same(Helpers.path("new/lua/foobar"), root)
end)
it("find the correct root with mod dels", function()
Cache.cache = {}
Cache.indexed = {}
Cache.indexed_rtp = false
Cache._topmods = {}
Cache.enabled = true
vim.opt.rtp:append(Helpers.path("old"))
Helpers.fs_create({ "old/lua/foobar/test.lua" })
Cache.cache["foobar.test"] = { modpath = Helpers.path("old/lua/foobar/test.lua") }
local root = Cache.find_root("foobar")
local root = Util.find_root("foobar")
assert(root, "foobar root not found")
assert.same(Helpers.path("old/lua/foobar"), root)
assert(not Cache.cache["foobar"], "foobar should not be in cache")
@ -124,11 +115,10 @@ describe("util", function()
Helpers.fs_rm("old")
-- vim.opt.rtp = rtp
Cache.indexed = {}
Cache.indexed_rtp = false
Cache._topmods = {}
vim.opt.rtp:append(Helpers.path("new"))
Helpers.fs_create({ "new/lua/foobar/test.lua" })
root = Cache.find_root("foobar")
root = Util.find_root("foobar")
assert(root, "foobar root not found")
assert.same(Helpers.path("new/lua/foobar"), root)
end)