Skip to content

Commit

Permalink
fix(fragments): prevent adding the same spec instance more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 24, 2024
1 parent fd04bc6 commit dbffad6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
40 changes: 24 additions & 16 deletions lua/lazy/core/fragments.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
local Config = require("lazy.core.config")

local M = {}

M._fid = 0

local function next_id()
M._fid = M._fid + 1
return M._fid
end
local Util = require("lazy.core.util")

--- This class is used to manage the fragments of a plugin spec.
--- It keeps track of the fragments and their relations to other fragments.
Expand All @@ -17,30 +9,39 @@ end
---@field frag_stack number[]
---@field dep_stack number[]
---@field dirty table<number, boolean>
---@field plugins table<LazyPlugin, number>
---@field spec LazySpecLoader
local F = {}
local M = {}

M._fid = 0

local function next_id()
M._fid = M._fid + 1
return M._fid
end

---@param spec LazySpecLoader
---@return LazyFragments
function M.new(spec)
local self = setmetatable({}, { __index = F })
local self = setmetatable({}, { __index = M })
self.fragments = {}
self.frag_stack = {}
self.dep_stack = {}
self.spec = spec
self.dirty = {}
self.plugins = {}
return self
end

---@param id number
function F:get(id)
function M:get(id)
return self.fragments[id]
end

--- Remove a fragment and all its children.
--- This will also remove the fragment from its parent's children list.
---@param id number
function F:del(id)
function M:del(id)
-- del fragment
local fragment = self.fragments[id]
if not fragment then
Expand All @@ -55,13 +56,13 @@ function F:del(id)
local parent = self.fragments[pid]
if parent.frags then
---@param fid number
parent.frags = vim.tbl_filter(function(fid)
parent.frags = Util.filter(function(fid)
return fid ~= id
end, parent.frags)
end
if parent.deps then
---@param fid number
parent.deps = vim.tbl_filter(function(fid)
parent.deps = Util.filter(function(fid)
return fid ~= id
end, parent.deps)
end
Expand All @@ -81,8 +82,15 @@ end
--- Add a fragment to the fragments list.
--- This also resolves its name, url, dir, dependencies and child specs.
---@param plugin LazyPluginSpec
function F:add(plugin)
function M:add(plugin)
if self.plugins[plugin] then
return self.fragments[self.plugins[plugin]]
end

local id = next_id()
setmetatable(plugin, nil)

self.plugins[plugin] = id

local pid = self.frag_stack[#self.frag_stack]

Expand Down
13 changes: 6 additions & 7 deletions lua/lazy/core/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function M:load_pkgs()
if not Config.options.pkg.enabled then
return
end
local specs = Pkg.get()
for dir, pkg in pairs(specs) do
for _, pkg in ipairs(Pkg.get()) do
local meta, fragment = self:add(pkg.spec)
if meta and fragment then
meta._.pkg = pkg
Expand All @@ -44,7 +43,7 @@ function M:load_pkgs()
frag.spec.optional = true
end
-- keep track of the top-level package fragment
self.pkgs[dir] = fragment.id
self.pkgs[pkg.dir] = fragment.id
end
end
end
Expand Down Expand Up @@ -128,7 +127,7 @@ function M:rebuild()
-- fragment was deleted, so remove it from plugin
self.frag_to_meta[fid] = nil
---@param f number
meta._.frags = vim.tbl_filter(function(f)
meta._.frags = Util.filter(function(f)
return f ~= fid
end, meta._.frags)
-- if no fragments left, delete plugin
Expand Down Expand Up @@ -167,10 +166,10 @@ function M:_rebuild(name)
assert(#plugin._.frags > 0, "no fragments found for plugin " .. name)

---@type table<number, boolean>
local done = {}
local added = {}
for _, fid in ipairs(plugin._.frags) do
if not done[fid] then
done[fid] = true
if not added[fid] then
added[fid] = true
local fragment = self.fragments:get(fid)
assert(fragment, "fragment " .. fid .. " not found, for plugin " .. name)
---@diagnostic disable-next-line: no-unknown
Expand Down
19 changes: 9 additions & 10 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ function Spec:import(spec)
self.importing = nil
return self:error(
"Invalid spec module: `"
.. modname
.. "`\nExpected a `table` of specs, but a `"
.. type(mod)
.. "` was returned instead"
.. modname
.. "`\nExpected a `table` of specs, but a `"
.. type(mod)
.. "` was returned instead"
)
end
self:normalize(mod)
Expand Down Expand Up @@ -216,11 +216,11 @@ function M.update_state()
plugin._ = plugin._ or {}
if plugin.lazy == nil then
local lazy = plugin._.dep
or Config.options.defaults.lazy
or plugin.event
or plugin.keys
or plugin.ft
or plugin.cmd
or Config.options.defaults.lazy
or plugin.event
or plugin.keys
or plugin.ft
or plugin.cmd
plugin.lazy = lazy and true or false
end
if plugin.dir:find(Config.options.root, 1, true) == 1 then
Expand Down Expand Up @@ -342,7 +342,6 @@ function M.load()
Config.plugins[name]._ = plugin._
Config.plugins[name]._.dep = new_state.dep
Config.plugins[name]._.frags = new_state.frags
-- Config.plugins[name]._.tasks = new_state.tasks
end
end
Util.track()
Expand Down
14 changes: 14 additions & 0 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ function M.track(data, time)
end
end

---@generic T
---@param list T[]
---@param fn fun(v: T):boolean?
---@return T[]
function M.filter(fn, list)
local ret = {}
for _, v in ipairs(list) do
if fn(v) then
table.insert(ret, v)
end
end
return ret
end

---@generic F: fun()
---@param data (string|{[string]:string})?
---@param fn F
Expand Down
16 changes: 12 additions & 4 deletions lua/lazy/pkg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ M.dirty = false
---@field pkgs LazyPkg[]
---@field version number

---@type table<string, LazyPkg>?
---@type LazyPkg[]?
M.cache = nil

function M.update()
Expand Down Expand Up @@ -65,6 +65,9 @@ function M.update()
end
end
end
table.sort(ret.pkgs, function(a, b)
return a.name < b.name
end)
local code = "return " .. Util.dump(ret)
vim.fn.mkdir(vim.fn.fnamemodify(Config.options.pkg.cache, ":h"), "p")
Util.write_file(Config.options.pkg.cache, code)
Expand All @@ -91,8 +94,8 @@ local function _load()
end
-- wrap in the scope of the plugin
pkg.spec = { pkg.name, specs = pkg.spec }
M.cache[pkg.dir] = pkg
end
M.cache = ret.pkgs
end
end, "Error loading pkg:")
end
Expand All @@ -107,10 +110,15 @@ end

---@param dir string
---@return LazyPkg?
---@overload fun():table<string, LazyPkg>
---@overload fun():LazyPkg[]
function M.get(dir)
if dir then
return M.cache[dir]
for _, pkg in ipairs(M.cache) do
if pkg.dir == dir then
return pkg
end
end
return
end
return M.cache
end
Expand Down

0 comments on commit dbffad6

Please sign in to comment.