From b214d2241ebc378dcdbe23bbfbb4e0aeae6cbdf0 Mon Sep 17 00:00:00 2001 From: zuqini Date: Mon, 2 Nov 2026 14:18:46 -0800 Subject: [PATCH 1/7] fix(module_loader)!: load only the plugin holding a required module With one pending lazy plugin whose lua/ has a module's top-level name, a require of any module under that name loaded the plugin, whether it held the module or not. An optional probe at startup turned lazy plugins eager: pcall(require, 'mini.icons'), as snacks and which-key do, loaded and configured { 'echasnovski/mini.pairs', event = 'InsertEnter' }, and pcall(require, 'nvim-treesitter.configs') loaded nvim-treesitter's main branch, which has no configs.lua. 2.x did the same, for any number of claimants. The fallback came in with the shared-namespace fix to keep 2.x's behaviour for a sole claimant; no plugin was found that needs it. It didn't serve a module a plugin/ script puts in package.preload either: the loader hands back only package.loaded or the plugin's file, so that first require failed anyway. A module brought in by an update is still found, since the lookup cache resets on PackChanged. As in lazy.nvim, whose loader loads a plugin only when Cache.find finds the module's file, a require now loads the first pending plugin holding it (.lua, /init.lua, .so or .dll) and otherwise fails with the usual "module not found", the same rule as for several claimants. A plugin that makes a module at load time needs a trigger of its own. Refs zpack_nvim-50v, zpack_nvim-8n1 --- doc/zpack.txt | 7 +++- docs/tips.md | 2 +- lua/zpack/module_loader.lua | 13 ++---- tests/startup_process_test.lua | 74 ++++++++++++++++++++++++++++++---- 4 files changed, 76 insertions(+), 20 deletions(-) diff --git a/doc/zpack.txt b/doc/zpack.txt index 10307fc..5996f42 100644 --- a/doc/zpack.txt +++ b/doc/zpack.txt @@ -1048,8 +1048,11 @@ module (boolean, optional) loaded when its modules are `require()`d. zpack automatically loads lazy plugins when their modules are - required: the first plugin holding the module, or failing that - the only pending plugin with its top-level name under `/lua`. + required: the first pending plugin holding the module's file + under `/lua` (`.lua`, `/init.lua`, or a compiled `.so` or + `.dll`), as in lazy.nvim. A plugin that has only the module's + top-level name, such as `mini.pairs` for a `require` of + `mini.icons`, stays lazy, and the `require` fails as usual. Default: true (module loading enabled) diff --git a/docs/tips.md b/docs/tips.md index f4dfec6..43ac5bb 100644 --- a/docs/tips.md +++ b/docs/tips.md @@ -275,7 +275,7 @@ A helper in an imported directory, such as `lua/plugins/util.lua` returning `{ o - A function-form `event`, `cmd`, `ft` or `keys` that throws used to leave the plugin loading at startup. The error is still reported, but the plugin now stays lazy, and only a `require` of its modules loads it. - `opts_extend` used to be ignored. Now it applies, so lists that used to replace each other are joined: `{ 'u/x', opts_extend = { 'l' }, opts = { l = { 'a' } } }` beside `{ 'u/x', opts = { l = { 'b' } } }` gave `{ 'b' }` and now gives `{ 'a', 'b' }`. - A bare string in a spec list used to be reported as an invalid spec and skipped. `{ 'u/a', { 'u/b' } }` now installs both. A string without an owner, as in a module returning `{ 'lua_ls', 'rust_analyzer' }`, is still skipped with a warning. -- A `require` used to load a lazy plugin whose name or `main` looked like the module's name, even with no such module. As in lazy.nvim, it now takes that top-level name under the plugin's `lua/`. +- A `require` used to load a lazy plugin whose name or `main` looked like the module's name, or whose `lua/` had only the module's top-level name. So an optional `pcall(require, 'mini.icons')` at startup loaded and configured a lazy `mini.pairs`, and `pcall(require, 'nvim-treesitter.configs')` loaded nvim-treesitter's main branch, which has no such module. As in lazy.nvim, a `require` now loads only the plugin holding the module's file under `lua/`, and fails as usual when none does. A plugin that makes a module at load time, from `plugin/` or `package.preload`, needs a trigger of its own. - `setup({ profiling = ... })` is ignored without a word, and `print_profile` and `get_profile` are gone from `zpack.module_loader`. ## Upgrading from 1.x to 2.0 diff --git a/lua/zpack/module_loader.lua b/lua/zpack/module_loader.lua index 148266a..57a62ca 100644 --- a/lua/zpack/module_loader.lua +++ b/lua/zpack/module_loader.lua @@ -36,28 +36,21 @@ local function claims_topmod(path, topmod) end ---Find the pending plugin to load for a module: the first claimant of the top-level ----name holding its file, or failing that a sole pending claimant +---name holding its file ---@param modname string ---@return string? src plugin source if found local function find_src_by_scanning(modname) local topmod = modname:match("^[^.]+") - local first_pending, pending_count = nil, 0 for _, pack_spec in ipairs(lazy_pack_specs) do local src = pack_spec.src local registry_entry = state.spec_registry[src] local plugin = registry_entry and registry_entry.plugin if plugin and registry_entry.load_status == "pending" and registry_entry.merged_spec.module ~= false - and claims_topmod(plugin.path, topmod) then - if module_file(plugin.path, modname) then - return src - end - first_pending = first_pending or src - pending_count = pending_count + 1 + and claims_topmod(plugin.path, topmod) and module_file(plugin.path, modname) then + return src end end - - return pending_count == 1 and first_pending or nil end ---Initialize module loader with lazy plugins diff --git a/tests/startup_process_test.lua b/tests/startup_process_test.lua index 9448ba5..00667e2 100644 --- a/tests/startup_process_test.lua +++ b/tests/startup_process_test.lua @@ -1416,17 +1416,77 @@ describe("sessions over one data directory", function() assert.is_false(loads(result, 'cc')) end) - it("loads what's left once another trigger has loaded the other claimant", function() - local result = session(setup_args('aa', 'bb'), report('shared.nope') .. "vim.cmd('ZPack load bb'); " .. report('shared.nope2')) - assert.is_true(loads(result, 'aa')) + it("loads neither for such a module once another trigger has loaded the other claimant", function() + local result = session(setup_args('aa', 'bb'), report('shared.nope') .. "vim.cmd('ZPack load bb'); " + .. report('shared.nope2') .. "table.insert(_G.LOG, 'aa=' .. require('zpack.api').get_plugin('aa').status); " + .. report('shared.ina')) + assert.are.same({ 'false=false', 'false=false', 'aa=pending', 'true=aa' }, vim.tbl_filter(function(entry) + return entry:find('=', 1, true) ~= nil + end, result.log)) end) + end) + + describe("a module no lazy plugin holds, required at startup", function() + local probe = "config = function() local ok, err = pcall(require, %q); table.insert(_G.LOG, 'probe=' .. tostring(ok)); table.insert(_G.LOG, tostring(err)) end" + local status = "table.insert(_G.LOG, 'status=' .. require('zpack.api').get_plugin(%q).status); " - it("still loads a sole claimant for a submodule it doesn't hold", function() + local function setup_args(lazy_spec, module) + return ("{ spec = { %s, { url = ee, name = 'ee', %s } }, defaults = { confirm = false } }"):format(lazy_spec, probe:format(module)) + end + + local function assert_not_found(result, module) + assert.is_true(vim.list_contains(result.log, 'probe=false'), vim.inspect(result.log)) + local err = result.log[vim.fn.index(result.log, 'probe=false') + 2] + assert.are.equal(1, (err:find(("module '%s' not found:"):format(module), 1, true)), err) + end + + before_each(function() + make_repo('ee') + make_repo('pairs', { ['lua/mini/pairs.lua'] = { "return { setup = function() table.insert(_G.LOG, 'pairs.setup') end }" } }) + make_repo('ts', { + ['lua/nvim-treesitter/init.lua'] = { 'return {}' }, + ['lua/nvim-treesitter/config.lua'] = { 'return {}' }, + }) + end) + + it("leaves a plugin sharing its namespace lazy, as mini.pairs beside a require of mini.icons", function() + local result = session( + setup_args("{ url = pairs, name = 'mini.pairs', event = 'InsertEnter', opts = {} }", 'mini.icons'), + status:format('mini.pairs') .. "vim.api.nvim_exec_autocmds('InsertEnter', {}); " .. status:format('mini.pairs') + ) + assert_not_found(result, 'mini.icons') + assert.are.same({ 'status=pending', 'pairs.plugin', 'pairs.setup', 'status=loaded' }, + vim.list_slice(result.log, #result.log - 3)) + end) + + it("leaves a plugin lazy whose old submodule is gone, as nvim-treesitter's main branch without configs", function() local result = session( - "{ spec = { { url = bb, name = 'bb', lazy = true } }, defaults = { confirm = false } }", - report('only_bb.nope') + setup_args("{ url = ts, name = 'nvim-treesitter', event = 'BufReadPost' }", 'nvim-treesitter.configs'), + status:format('nvim-treesitter') ) - assert.is_true(loads(result, 'bb')) + assert_not_found(result, 'nvim-treesitter.configs') + assert.is_false(vim.list_contains(result.log, 'ts.plugin')) + assert.are.equal('status=pending', result.log[#result.log]) + end) + + for label, module in pairs({ ['its file'] = 'mini.pairs', ['a directory with init.lua'] = 'nvim-treesitter' }) do + it("still loads the plugin holding " .. label, function() + local result = session(("{ spec = { %s, %s, { url = ee, name = 'ee', %s } }, defaults = { confirm = false } }"):format( + "{ url = pairs, name = 'mini.pairs', event = 'InsertEnter', opts = {} }", + "{ url = ts, name = 'nvim-treesitter', event = 'BufReadPost' }", + probe:format(module) + )) + assert.is_true(vim.list_contains(result.log, 'probe=true'), vim.inspect(result.log)) + assert.are.equal(module == 'mini.pairs', vim.list_contains(result.log, 'pairs.setup')) + assert.are.equal(module == 'mini.pairs', vim.list_contains(result.log, 'pairs.plugin')) + assert.are.equal(module ~= 'mini.pairs', vim.list_contains(result.log, 'ts.plugin')) + end) + end + + it("still loads the plugin holding a submodule", function() + local result = session(setup_args("{ url = ts, name = 'nvim-treesitter', event = 'BufReadPost' }", 'nvim-treesitter.config')) + assert.is_true(vim.list_contains(result.log, 'probe=true'), vim.inspect(result.log)) + assert.is_true(vim.list_contains(result.log, 'ts.plugin')) end) end) From 5a2e0a6549c4b2a1d9ef16422c45a11560c63617 Mon Sep 17 00:00:00 2001 From: zuqini Date: Mon, 2 Nov 2026 17:41:00 -0800 Subject: [PATCH 2/7] fix(module_loader): load a pending plugin for a slash-form require require('lspconfig/util'), still written in nvim-lspconfig's own configs, never loaded a pending plugin holding lua/lspconfig/util.lua: the top-level name came out as 'lspconfig/util', which no plugin claims. 2.x did the same. As lazy.nvim's Cache.find and vim.loader.find do, turn '/' into '.' and drop leading dots before the lookup. The top-level name may now be empty, so require('') and require('/') fail with "module not found" rather than an error inside the loader. --- lua/zpack/module_loader.lua | 3 ++- tests/startup_process_test.lua | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lua/zpack/module_loader.lua b/lua/zpack/module_loader.lua index 57a62ca..8eeadff 100644 --- a/lua/zpack/module_loader.lua +++ b/lua/zpack/module_loader.lua @@ -40,7 +40,7 @@ end ---@param modname string ---@return string? src plugin source if found local function find_src_by_scanning(modname) - local topmod = modname:match("^[^.]+") + local topmod = modname:match("^[^.]*") for _, pack_spec in ipairs(lazy_pack_specs) do local src = pack_spec.src @@ -100,6 +100,7 @@ end ---@param modname string ---@return function|string|nil function M.loader(modname) + modname = modname:gsub("/", "."):gsub("^%.+", "") local src = find_src_for_module(modname) if not src then return nil diff --git a/tests/startup_process_test.lua b/tests/startup_process_test.lua index 00667e2..0a34b65 100644 --- a/tests/startup_process_test.lua +++ b/tests/startup_process_test.lua @@ -1507,6 +1507,22 @@ describe("sessions over one data directory", function() end) end + for label, name in pairs({ ["a slash-form name, as require('lspconfig/util')"] = 'mine/sub', ['a leading dot'] = '.mine.sub' }) do + it("loads for " .. label, function() + make_repo('aa', { ['lua/mine/sub.lua'] = { "return { from = 'aa' }" } }) + local result = session(setup_args("{ url = aa, name = 'aa', lazy = true }"), report(name)) + assert.are.equal('aa', result.log[#result.log]) + end) + end + + for _, name in ipairs({ '', '/' }) do + it(("reports %q as not found"):format(name), function() + make_repo('aa', { ['lua/mine.lua'] = { "return { from = 'aa' }" } }) + local result = session(setup_args("{ url = aa, name = 'aa', lazy = true }"), report(name)) + assert.are.equal(1, (result.log[#result.log]:find(("module '%s' not found:"):format(name), 1, true)), result.log[#result.log]) + end) + end + it("comes from the plugin it loaded, ahead of a dependency holding the same one", function() make_repo('parent', { ['lua/same.lua'] = { "return { from = 'parent' }" } }) make_repo('dep', { ['lua/same.lua'] = { "return { from = 'dep' }" } }) From 9a7d6f8812c5d6595079dee2904ecc081b67740a Mon Sep 17 00:00:00 2001 From: zuqini Date: Mon, 2 Nov 2026 17:41:31 -0800 Subject: [PATCH 3/7] docs(tips): name optional parents beside disabled ones, and a throwing trigger's other triggers --- docs/tips.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/tips.md b/docs/tips.md index 43ac5bb..cad19f3 100644 --- a/docs/tips.md +++ b/docs/tips.md @@ -170,9 +170,9 @@ A dependency whose function-form `opts` throws used to stop a lazy plugin listin { 'u/p', enabled = false, dependencies = { 'u/d' } } ``` -### A disabled plugin's `dependencies` and `specs` no longer count +### A disabled or `optional` plugin's `dependencies` and `specs` no longer count -`opts` in a disabled plugin's `dependencies` spec used to reach that dependency when another spec kept it. Now that spec goes with the plugin listing it. Move what should stay to a spec that stays: +`opts` in a `dependencies` spec of a plugin that is disabled, or left out as `optional`, used to reach that dependency when another spec kept it. Now that spec goes with the plugin listing it. LazyVim's lang extras have this shape but lose nothing: an `optional` nvim-lint or nvim-dap carrying `{ 'mason-org/mason.nvim', opts = { ensure_installed = ... } }` never added to mason's list in 2.x, which ignored `opts_extend`, so LazyVim's own mason spec replaced the list. 3.0 adds it when another spec lists that plugin without `optional`. Move what should stay to a spec that stays: ```lua -- 2.x: u/d gets { a = 1, b = 2 }. 3.0: only { b = 2 } @@ -272,7 +272,7 @@ A helper in an imported directory, such as `lua/plugins/util.lua` returning `{ o - `lua/plugins/*.lua` import by sorted module name, where it used to be file system order, and later specs win: `foo` comes before `foo-extra`, so `foo-extra`'s `opts` now override `foo`'s. - A table listed both at the top level and under a lazy plugin's `dependencies` used to stay lazy. Now it's a plugin of its own and loads at startup, in `local d = { 'u/d' }` with `{ d, { 'u/p', cmd = 'P', dependencies = { d } } }`. `lazy = true` on the table keeps it lazy. - A `nil` in a spec list used to end the list. `{ { 'u/x' }, flag and { 'u/y' } or nil, { 'u/d' } }` now installs `u/d` when `flag` is false. -- A function-form `event`, `cmd`, `ft` or `keys` that throws used to leave the plugin loading at startup. The error is still reported, but the plugin now stays lazy, and only a `require` of its modules loads it. +- A function-form `event`, `cmd`, `ft` or `keys` that throws used to leave the plugin loading at startup. The error is still reported, but the plugin now stays lazy, and only its other triggers or a `require` of its modules load it. - `opts_extend` used to be ignored. Now it applies, so lists that used to replace each other are joined: `{ 'u/x', opts_extend = { 'l' }, opts = { l = { 'a' } } }` beside `{ 'u/x', opts = { l = { 'b' } } }` gave `{ 'b' }` and now gives `{ 'a', 'b' }`. - A bare string in a spec list used to be reported as an invalid spec and skipped. `{ 'u/a', { 'u/b' } }` now installs both. A string without an owner, as in a module returning `{ 'lua_ls', 'rust_analyzer' }`, is still skipped with a warning. - A `require` used to load a lazy plugin whose name or `main` looked like the module's name, or whose `lua/` had only the module's top-level name. So an optional `pcall(require, 'mini.icons')` at startup loaded and configured a lazy `mini.pairs`, and `pcall(require, 'nvim-treesitter.configs')` loaded nvim-treesitter's main branch, which has no such module. As in lazy.nvim, a `require` now loads only the plugin holding the module's file under `lua/`, and fails as usual when none does. A plugin that makes a module at load time, from `plugin/` or `package.preload`, needs a trigger of its own. From 082dc83bc36ef7453701a9c4890a6bc67378c185 Mon Sep 17 00:00:00 2001 From: zuqini Date: Mon, 2 Nov 2026 17:50:31 -0800 Subject: [PATCH 4/7] refactor(merge): derive has_opts in merge_entry prune and resolve_all each set entry.has_opts by hand after their own merge, and fold's re-merge left it to resolve_all's later loop. Any new re-merge that forgot the line would leave a stale has_opts, and setup() would run or skip on specs that are gone. Every merge now sets it. --- lua/zpack/merge.lua | 1 - lua/zpack/merge_fields.lua | 14 ++++++-------- lua/zpack/prune.lua | 1 - 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lua/zpack/merge.lua b/lua/zpack/merge.lua index ceea6d9..4fd91d5 100644 --- a/lua/zpack/merge.lua +++ b/lua/zpack/merge.lua @@ -47,7 +47,6 @@ function M.resolve_all(graph, enabled_results, notify) for src, entry in pairs(state.spec_registry) do fields.resolve_enabled(enabled_results, entry, src) - entry.has_opts = fields.contributes_opts(entry) -- cond_result is intentionally left nil here. It is written later by -- registration.register_all's vim.pack.add load callback (which needs -- the live plugin arg for function-form conds). Readers must treat diff --git a/lua/zpack/merge_fields.lua b/lua/zpack/merge_fields.lua index 3264514..9416d6a 100644 --- a/lua/zpack/merge_fields.lua +++ b/lua/zpack/merge_fields.lua @@ -159,21 +159,19 @@ function M.merge_spec_array(specs) return result end +---`opts` is deliberately not stored on merged_spec; `has_opts` lets +---plugin_loader / startup answer "does any spec contribute opts?" without +---re-scanning. function M.merge_entry(entry) entry.sorted_specs = M.sort_specs(entry.specs) entry.merged_spec = M.merge_spec_array(entry.sorted_specs) -end - ----`opts` is deliberately not stored on merged_spec; this boolean lets ----plugin_loader / startup answer "does any spec contribute opts?" without ----re-scanning. -function M.contributes_opts(entry) + entry.has_opts = false for _, s in ipairs(entry.sorted_specs) do if s.opts ~= nil then - return true + entry.has_opts = true + break end end - return false end ---lazy.nvim parity: the last spec to set `enabled`, in merge order, decides, so diff --git a/lua/zpack/prune.lua b/lua/zpack/prune.lua index 9eee1c2..c804ced 100644 --- a/lua/zpack/prune.lua +++ b/lua/zpack/prune.lua @@ -77,7 +77,6 @@ local function refresh_entry(prune, src, entry) fields.merge_entry(entry) entry.merged_spec.name = name fields.resolve_enabled(prune.enabled_results, entry, src) - entry.has_opts = fields.contributes_opts(entry) end end From e6cbbb29369c6f9f76f5fcbda14a6d82b3e93b72 Mon Sep 17 00:00:00 2001 From: zuqini Date: Mon, 2 Nov 2026 23:28:04 -0800 Subject: [PATCH 5/7] fix(module_loader): prefer the plugin named like the module among holders When two pending plugins hold a required module's file, load the one whose normalised name matches the top-level module, else the first listed. lazy.nvim's get_unloaded_rtp puts that plugin first, so require('mini.ai') with mini.nvim and a standalone mini.ai both pending loads mini.nvim in either order. zpack took the first in spec order. Once a first holder is found, only plugins named like the module are scanned, so a hit still lists no other plugin's lua/ directory. --- doc/zpack.txt | 14 +++++++++----- docs/tips.md | 2 +- lua/zpack/module_loader.lua | 19 ++++++++++++++----- tests/startup_process_test.lua | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/doc/zpack.txt b/doc/zpack.txt index 5996f42..a386f67 100644 --- a/doc/zpack.txt +++ b/doc/zpack.txt @@ -1048,11 +1048,15 @@ module (boolean, optional) loaded when its modules are `require()`d. zpack automatically loads lazy plugins when their modules are - required: the first pending plugin holding the module's file - under `/lua` (`.lua`, `/init.lua`, or a compiled `.so` or - `.dll`), as in lazy.nvim. A plugin that has only the module's - top-level name, such as `mini.pairs` for a `require` of - `mini.icons`, stays lazy, and the `require` fails as usual. + required: a pending plugin holding the module's file under + `/lua` (`.lua`, `/init.lua`, or a compiled `.so` or `.dll`). + Of several such plugins, the one named like the top-level + module loads, as in lazy.nvim, so `require('mini.ai')` loads + `mini.nvim` rather than a standalone `mini.ai`. Otherwise the + first listed loads, higher `priority` first. A plugin that has + only the module's top-level name, such as `mini.pairs` for a + `require` of `mini.icons`, stays lazy, and the `require` fails + as usual. Default: true (module loading enabled) diff --git a/docs/tips.md b/docs/tips.md index cad19f3..1afdf83 100644 --- a/docs/tips.md +++ b/docs/tips.md @@ -275,7 +275,7 @@ A helper in an imported directory, such as `lua/plugins/util.lua` returning `{ o - A function-form `event`, `cmd`, `ft` or `keys` that throws used to leave the plugin loading at startup. The error is still reported, but the plugin now stays lazy, and only its other triggers or a `require` of its modules load it. - `opts_extend` used to be ignored. Now it applies, so lists that used to replace each other are joined: `{ 'u/x', opts_extend = { 'l' }, opts = { l = { 'a' } } }` beside `{ 'u/x', opts = { l = { 'b' } } }` gave `{ 'b' }` and now gives `{ 'a', 'b' }`. - A bare string in a spec list used to be reported as an invalid spec and skipped. `{ 'u/a', { 'u/b' } }` now installs both. A string without an owner, as in a module returning `{ 'lua_ls', 'rust_analyzer' }`, is still skipped with a warning. -- A `require` used to load a lazy plugin whose name or `main` looked like the module's name, or whose `lua/` had only the module's top-level name. So an optional `pcall(require, 'mini.icons')` at startup loaded and configured a lazy `mini.pairs`, and `pcall(require, 'nvim-treesitter.configs')` loaded nvim-treesitter's main branch, which has no such module. As in lazy.nvim, a `require` now loads only the plugin holding the module's file under `lua/`, and fails as usual when none does. A plugin that makes a module at load time, from `plugin/` or `package.preload`, needs a trigger of its own. +- A `require` used to load a lazy plugin whose name or `main` looked like the module's name, or whose `lua/` had only the module's top-level name. So an optional `pcall(require, 'mini.icons')` at startup loaded and configured a lazy `mini.pairs`, and `pcall(require, 'nvim-treesitter.configs')` loaded nvim-treesitter's main branch, which has no such module. As in lazy.nvim, a `require` now loads only the plugin holding the module's file under `lua/`, and fails as usual when none does. When two do, the one named like the top-level module loads, so `require('mini.ai')` loads `mini.nvim`, not a standalone `mini.ai` listed beside it. A plugin that makes a module at load time, from `plugin/` or `package.preload`, needs a trigger of its own. - `setup({ profiling = ... })` is ignored without a word, and `print_profile` and `get_profile` are gone from `zpack.module_loader`. ## Upgrading from 1.x to 2.0 diff --git a/lua/zpack/module_loader.lua b/lua/zpack/module_loader.lua index 8eeadff..06b8c31 100644 --- a/lua/zpack/module_loader.lua +++ b/lua/zpack/module_loader.lua @@ -35,22 +35,31 @@ local function claims_topmod(path, topmod) return false end ----Find the pending plugin to load for a module: the first claimant of the top-level ----name holding its file +---Find the pending plugin to load for a module: of the claimants of the top-level name +---holding its file, the one named like that name as in lazy.nvim, else the first. +---Once the first is found, only plugins named like it are scanned. ---@param modname string ---@return string? src plugin source if found local function find_src_by_scanning(modname) local topmod = modname:match("^[^.]*") + local norm = util.normalize_name(topmod) + local first for _, pack_spec in ipairs(lazy_pack_specs) do local src = pack_spec.src local registry_entry = state.spec_registry[src] local plugin = registry_entry and registry_entry.plugin - if plugin and registry_entry.load_status == "pending" and registry_entry.merged_spec.module ~= false - and claims_topmod(plugin.path, topmod) and module_file(plugin.path, modname) then - return src + if plugin and registry_entry.load_status == "pending" and registry_entry.merged_spec.module ~= false then + local named = util.normalize_name(plugin.spec.name) == norm + if (named or not first) and claims_topmod(plugin.path, topmod) and module_file(plugin.path, modname) then + if named then + return src + end + first = src + end end end + return first end ---Initialize module loader with lazy plugins diff --git a/tests/startup_process_test.lua b/tests/startup_process_test.lua index 0a34b65..817b865 100644 --- a/tests/startup_process_test.lua +++ b/tests/startup_process_test.lua @@ -1399,6 +1399,37 @@ describe("sessions over one data directory", function() assert.is_false(loads(result, 'bb')) end) + describe("when both hold the file", function() + local names = { mininvim = 'mini.nvim', miniai = 'mini.ai' } + local function both_args(order) + return ("{ spec = { { url = %s, name = '%s', lazy = true }, { url = %s, name = '%s', lazy = true } }, defaults = { confirm = false } }") + :format(order[1], names[order[1]], order[2], names[order[2]]) + end + + before_each(function() + for repo, plugin in pairs(names) do + make_repo(repo, { + ['lua/mini/ai.lua'] = { ("return { from = '%s' }"):format(plugin) }, + ['lua/other/x.lua'] = { ("return { from = '%s' }"):format(plugin) }, + }) + end + end) + + for name, order in pairs({ ['listed first'] = { 'mininvim', 'miniai' }, ['listed second'] = { 'miniai', 'mininvim' } }) do + it("loads the one named like the top-level module when it's " .. name, function() + local result = session(both_args(order), report('mini.ai')) + assert.is_true(vim.list_contains(result.log, 'true=mini.nvim'), vim.inspect(result.log)) + assert.is_false(loads(result, 'miniai')) + end) + + it("loads the first listed when neither is named like it, with " .. names[order[1]] .. ' first', function() + local result = session(both_args(order), report('other.x')) + assert.is_true(vim.list_contains(result.log, 'true=' .. names[order[1]]), vim.inspect(result.log)) + assert.is_false(loads(result, order[2])) + end) + end + end) + it("loads neither for a module neither holds, and a later require still works", function() local result = session(setup_args('aa', 'bb'), report('shared.nope') .. report('shared.nope') .. report('shared.inb')) assert.is_true(vim.list_contains(result.log, 'false=false')) From 4108115dafc4b5cf9a887001720c149857dbb33d Mon Sep 17 00:00:00 2001 From: zuqini Date: Tue, 3 Nov 2026 01:21:26 -0800 Subject: [PATCH 6/7] fix(module_loader): raise a module's load error after its config's require threw When a lazy plugin's own opts or config requires its module and the module throws, require leaves its loading sentinel in package.loaded. The loader handed that back, so the first require and every later one returned true. Fall through to the plugin's file instead, as lazy.nvim does, so the require raises the error and later ones give "loop or previous error". A module that loaded is still returned as is, whatever its type, so a non-table module isn't run twice. --- lua/zpack/module_loader.lua | 5 ++++- tests/startup_process_test.lua | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lua/zpack/module_loader.lua b/lua/zpack/module_loader.lua index 06b8c31..f9a1c7e 100644 --- a/lua/zpack/module_loader.lua +++ b/lua/zpack/module_loader.lua @@ -11,6 +11,9 @@ local module_lookup_cache = {} ---@type vim.pack.Spec[] local lazy_pack_specs = {} +---What require leaves in package.loaded while a module loads, and keeps there if the load throws. +local LOADING = package.loaded[...] + ---@param path string A plugin's directory ---@param modname string ---@return string? file @@ -128,7 +131,7 @@ function M.loader(modname) require('zpack.plugin_loader').process_spec(pack_spec, { bang = true }) local mod = package.loaded[modname] - if mod ~= nil then + if mod ~= nil and mod ~= LOADING then return function() return mod end diff --git a/tests/startup_process_test.lua b/tests/startup_process_test.lua index 817b865..990965e 100644 --- a/tests/startup_process_test.lua +++ b/tests/startup_process_test.lua @@ -1569,6 +1569,23 @@ describe("sessions over one data directory", function() assert.is_truthy(result.log[#result.log]:find('aa/lua/mine.lua', 1, true), result.log[#result.log]) end) + it("raises its error when its own auto-setup's require of it threw", function() + make_repo('aa', { ['lua/mine/init.lua'] = { "error('boom in mine')" } }) + local probe = "local ok, mod = pcall(require, 'mine'); table.insert(_G.LOG, tostring(ok) .. ' ' .. tostring(mod))" + local result = session(setup_args("{ url = aa, name = 'aa', main = 'mine', lazy = true, opts = {} }"), probe .. '; ' .. probe) + assert.is_truthy(result.log[#result.log - 1]:find('boom in mine', 1, true), result.log[#result.log - 1]) + assert.is_truthy(result.log[#result.log]:find("loop or previous error loading module 'mine'", 1, true), result.log[#result.log]) + end) + + it("is what its config required, run once, when it returns no table", function() + make_repo('aa', { ['lua/mine.lua'] = { "table.insert(_G.LOG, 'mine.run')", 'return function() end' } }) + local result = session( + setup_args("{ url = aa, name = 'aa', lazy = true, config = function() _G.FROM_CONFIG = require('mine') end }"), + "table.insert(_G.LOG, tostring(require('mine') == _G.FROM_CONFIG))") + assert.are.same({ 'mine.run', 'true' }, vim.list_slice(result.log, #result.log - 1)) + assert.are.equal(1, #vim.tbl_filter(function(l) return l == 'mine.run' end, result.log)) + end) + for _, vim_loader in ipairs({ true, false }) do it("opens a compiled one with vim_loader = " .. tostring(vim_loader), function() make_repo('aa', { ['lua/nat/compiled.so'] = { "not a real library" } }) From e313fdfe54e6162d5f078581c46eb3af451847d9 Mon Sep 17 00:00:00 2001 From: zuqini Date: Tue, 3 Nov 2026 02:20:35 -0800 Subject: [PATCH 7/7] test(module_loader): check require('') stays out of zpack, not Lua's wording On CI, leafo/gh-actions-lua installs Lua into ./.lua, so Lua's own ./?.lua searcher opens that directory for '' and '/' and reports "error loading module" rather than "not found". Assert what 5a2e0a6 fixed instead: the error doesn't come from inside zpack. --- tests/startup_process_test.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/startup_process_test.lua b/tests/startup_process_test.lua index 990965e..2cd94b5 100644 --- a/tests/startup_process_test.lua +++ b/tests/startup_process_test.lua @@ -1547,10 +1547,12 @@ describe("sessions over one data directory", function() end for _, name in ipairs({ '', '/' }) do - it(("reports %q as not found"):format(name), function() + it(("leaves %q to Lua's own searchers"):format(name), function() make_repo('aa', { ['lua/mine.lua'] = { "return { from = 'aa' }" } }) local result = session(setup_args("{ url = aa, name = 'aa', lazy = true }"), report(name)) - assert.are.equal(1, (result.log[#result.log]:find(("module '%s' not found:"):format(name), 1, true)), result.log[#result.log]) + local msg = result.log[#result.log] + assert.is_string(msg) + assert.is_falsy(msg:find('/lua/zpack/', 1, true), msg) end) end