From 0a9fc8319dca384ba93d8cae6819f4c839c46496 Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 02:30:59 -0800 Subject: [PATCH 1/9] fix(opts): a compiled module beside a plugin's one Lua module leaves no `main` --- lua/zpack/utils.lua | 1 + tests/utils_test.lua | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lua/zpack/utils.lua b/lua/zpack/utils.lua index 91ae60a..60dc311 100644 --- a/lua/zpack/utils.lua +++ b/lua/zpack/utils.lua @@ -367,6 +367,7 @@ M.resolve_main = function(plugin, spec) mods = { mod } break end + mod = mod or dir_entry.name:match("^(.+)%.so$") or dir_entry.name:match("^(.+)%.dll$") if mod and not seen[mod] then seen[mod] = true mods[#mods + 1] = mod diff --git a/tests/utils_test.lua b/tests/utils_test.lua index 47f7893..c6cf0bb 100644 --- a/tests/utils_test.lua +++ b/tests/utils_test.lua @@ -293,4 +293,10 @@ describe("resolve_main", function() assert.are.equal('alpha', main_of('alpha-nvim', { 'alpha.lua', 'alpha/themes/x.lua' })) assert.is_nil(main_of('two-mods.nvim', { 'first/init.lua', 'second.lua' })) end) + + it("counts a compiled module among a plugin's modules, as lazy.nvim does", function() + assert.is_nil(main_of('mixed.nvim', { 'helper.lua', 'native.so' })) + assert.is_nil(main_of('mixed-dll.nvim', { 'helper.lua', 'native.dll' })) + assert.are.equal('native', main_of('compiled-only.nvim', { 'native.so' })) + end) end) From f80c769427d79cb87f19dd4d7546eaab4356bed0 Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 02:31:17 -0800 Subject: [PATCH 2/9] fix(registration): `plugin.main` is the spec's `main` in every hook --- doc/zpack.txt | 7 ++++--- docs/spec.md | 2 +- lua/zpack/registration.lua | 1 + lua/zpack/types.lua | 2 +- tests/opts_test.lua | 29 +++++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/doc/zpack.txt b/doc/zpack.txt index 9dbfd75..f79c732 100644 --- a/doc/zpack.txt +++ b/doc/zpack.txt @@ -1248,9 +1248,10 @@ The plugin data object passed to hooks and trigger functions: -- as in lazy.nvim) dependencies = string[], -- Sorted list of resolved dependency -- names, as in lazy.nvim - main = string?, -- Detected main module name (set - -- before the first `config` runs; nil - -- inside `opts` functions until then) + main = string?, -- Main module name: the spec's `main`, + -- else the detected one (set before + -- the first `config` runs; nil until + -- then) } < diff --git a/docs/spec.md b/docs/spec.md index 1d6ae2f..885d59c 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -96,7 +96,7 @@ The plugin data object passed to hooks and trigger functions: name = string, -- Resolved plugin name (alias for spec.name, as in lazy.nvim) dir = string, -- Plugin directory (alias for path, as in lazy.nvim) dependencies = string[], -- Sorted list of resolved dependency names, as in lazy.nvim - main = string?, -- Detected main module name (set before the first `config` runs; nil inside `opts` functions until then) + main = string?, -- Main module name: the spec's `main`, else the detected one (set before the first `config` runs; nil until then) } ``` diff --git a/lua/zpack/registration.lua b/lua/zpack/registration.lua index cf0f84e..66ff3f2 100644 --- a/lua/zpack/registration.lua +++ b/lua/zpack/registration.lua @@ -70,6 +70,7 @@ M.register_all = function(ctx) -- value is stable across runs even though dependency_graph is a set. plugin.name = pack_spec.name plugin.dir = plugin.path + plugin.main = registry_entry.merged_spec.main local dep_set = state.dependency_graph[pack_spec.src] if dep_set then local dep_names = {} diff --git a/lua/zpack/types.lua b/lua/zpack/types.lua index 509cb4d..ab47eb5 100644 --- a/lua/zpack/types.lua +++ b/lua/zpack/types.lua @@ -35,7 +35,7 @@ ---@field name? string Resolved plugin name (alias for spec.name; lazy.nvim parity) ---@field dir? string Plugin directory (alias for path; lazy.nvim parity) ---@field dependencies? string[] Sorted list of dependency names (lazy.nvim parity) ----@field main? string The detected main module name (available in config hooks) +---@field main? string The spec's `main`, else the detected main module name (set before the first `config` runs) ---@alias zpack.EventValue string|string[]|zpack.EventSpec|(string|zpack.EventSpec)[] ---@alias zpack.CmdValue string|string[] diff --git a/tests/opts_test.lua b/tests/opts_test.lua index 5dc2803..3554f4d 100644 --- a/tests/opts_test.lua +++ b/tests/opts_test.lua @@ -394,6 +394,35 @@ describe("plugin.main in config hooks", function() assert.are.equal('test.module', received_main) end) + it("plugin.main is the spec's `main` in init and opts functions, as in lazy.nvim", function() + local seen = {} + + require('zpack').setup({ + spec = { + { + 'test/bare', + main = 'bare.module', + init = function(plugin) + seen.init = plugin.main + end, + }, + { + 'test/plugin', + main = 'test.module', + opts = function(plugin) + seen.opts = plugin.main + return {} + end, + config = function() end, + }, + }, + defaults = { confirm = false }, + }) + + helpers.flush_pending() + assert.are.same({ init = 'bare.module', opts = 'test.module' }, seen) + end) + it("plugin.main is nil when not detected", function() local received_main = "not_set" From da77c4314b3d37a5f68e0553dc070834fa280d63 Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 03:36:19 -0800 Subject: [PATCH 3/9] fix(registration): a `main` of false still leaves the module to detection --- lua/zpack/registration.lua | 2 +- tests/opts_test.lua | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lua/zpack/registration.lua b/lua/zpack/registration.lua index 66ff3f2..c7e06f6 100644 --- a/lua/zpack/registration.lua +++ b/lua/zpack/registration.lua @@ -70,7 +70,7 @@ M.register_all = function(ctx) -- value is stable across runs even though dependency_graph is a set. plugin.name = pack_spec.name plugin.dir = plugin.path - plugin.main = registry_entry.merged_spec.main + plugin.main = registry_entry.merged_spec.main or nil local dep_set = state.dependency_graph[pack_spec.src] if dep_set then local dep_names = {} diff --git a/tests/opts_test.lua b/tests/opts_test.lua index 3554f4d..40922fb 100644 --- a/tests/opts_test.lua +++ b/tests/opts_test.lua @@ -423,6 +423,30 @@ describe("plugin.main in config hooks", function() assert.are.same({ init = 'bare.module', opts = 'test.module' }, seen) end) + it("a `main` of false leaves the plugin's module to detection, as in lazy.nvim", function() + helpers.write_plugin_files('falsy', { 'falsy/init.lua' }) + local seen = {} + + require('zpack').setup({ + spec = { + { + 'test/falsy', + main = false, + init = function(plugin) + seen.init = tostring(plugin.main) + end, + config = function(plugin) + seen.config = plugin.main + end, + }, + }, + defaults = { confirm = false }, + }) + + helpers.flush_pending() + assert.are.same({ init = 'nil', config = 'falsy' }, seen) + end) + it("plugin.main is nil when not detected", function() local received_main = "not_set" From 73b894ffcfcd57dbec3dc65d5b141a51d9646364 Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 03:36:32 -0800 Subject: [PATCH 4/9] fix(opts): a symlinked compiled module is read by its module name in main detection --- lua/zpack/utils.lua | 5 +++-- tests/utils_test.lua | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lua/zpack/utils.lua b/lua/zpack/utils.lua index 60dc311..4ffd415 100644 --- a/lua/zpack/utils.lua +++ b/lua/zpack/utils.lua @@ -362,12 +362,13 @@ M.resolve_main = function(plugin, spec) local mods, seen = {}, {} for _, dir_entry in ipairs(M.lsdir(lua_dir)) do - local mod = M.top_module(dir_entry) + local compiled = dir_entry.name:match("^(.+)%.so$") or dir_entry.name:match("^(.+)%.dll$") + local mod = not compiled and M.top_module(dir_entry) or nil if mod and M.normalize_name(mod) == norm_name then mods = { mod } break end - mod = mod or dir_entry.name:match("^(.+)%.so$") or dir_entry.name:match("^(.+)%.dll$") + mod = mod or compiled if mod and not seen[mod] then seen[mod] = true mods[#mods + 1] = mod diff --git a/tests/utils_test.lua b/tests/utils_test.lua index c6cf0bb..7287ff3 100644 --- a/tests/utils_test.lua +++ b/tests/utils_test.lua @@ -299,4 +299,19 @@ describe("resolve_main", function() assert.is_nil(main_of('mixed-dll.nvim', { 'helper.lua', 'native.dll' })) assert.are.equal('native', main_of('compiled-only.nvim', { 'native.so' })) end) + + it("reads a symlinked compiled module by its module name, as lazy.nvim does", function() + local function link_compiled(name) + local lua_dir = vim.fn.stdpath('data') .. '/site/pack/zpack/opt/' .. name .. '/lua' + vim.fn.mkdir(lua_dir, 'p') + assert(vim.uv.fs_symlink('../target/libnative.so', lua_dir .. '/native.so')) + end + + link_compiled('linked-only.nvim') + assert.are.equal('native', main_of('linked-only.nvim', {})) + link_compiled('linked-beside.nvim') + assert.are.equal('native', main_of('linked-beside.nvim', { 'native.lua' })) + link_compiled('linked-mixed.nvim') + assert.is_nil(main_of('linked-mixed.nvim', { 'helper.lua' })) + end) end) From 0592b22e7af149c99d7154bb9f26e2beb56434cd Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 03:36:41 -0800 Subject: [PATCH 5/9] test(opts): pin no `main` for a compiled module named like the plugin beside a Lua one --- tests/utils_test.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/utils_test.lua b/tests/utils_test.lua index 7287ff3..78e31e2 100644 --- a/tests/utils_test.lua +++ b/tests/utils_test.lua @@ -300,6 +300,11 @@ describe("resolve_main", function() assert.are.equal('native', main_of('compiled-only.nvim', { 'native.so' })) end) + it("leaves no `main` when only a compiled module is named like the plugin beside a Lua one", function() + assert.is_nil(main_of('foo.nvim', { 'foo.so', 'bar.lua' })) + assert.is_nil(main_of('foo-dll.nvim', { 'foo_dll.dll', 'bar.lua' })) + end) + it("reads a symlinked compiled module by its module name, as lazy.nvim does", function() local function link_compiled(name) local lua_dir = vim.fn.stdpath('data') .. '/site/pack/zpack/opt/' .. name .. '/lua' From f6a3d9e74f4077e73773dc3368435fff44a67e2b Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 03:36:41 -0800 Subject: [PATCH 6/9] docs(opts): say how a compiled module counts in `main` detection --- doc/zpack.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/zpack.txt b/doc/zpack.txt index f79c732..46e13fc 100644 --- a/doc/zpack.txt +++ b/doc/zpack.txt @@ -1116,6 +1116,10 @@ main (string, optional) `nvim-` prefix and its `.vim` or `.nvim` suffix. 3. Otherwise, as in lazy.nvim, the one module in the plugin's `lua/`, if there is only one (`null-ls` for none-ls.nvim). + A compiled `.so` or `.dll` counts as a module here, but + only a Lua module is matched against the name in 2: + foo.nvim with `lua/foo.so` beside another module gets + none, where lazy.nvim takes `foo`. Set it when detection fails. From 06f7088491845dc01b4547f2751a1946e437dd0f Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 03:36:41 -0800 Subject: [PATCH 7/9] docs(registration): say the spec's `main` is on the plugin in every hook --- doc/zpack.txt | 8 ++++---- docs/spec.md | 2 +- lua/zpack/types.lua | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/zpack.txt b/doc/zpack.txt index 46e13fc..3cb359b 100644 --- a/doc/zpack.txt +++ b/doc/zpack.txt @@ -1252,10 +1252,10 @@ The plugin data object passed to hooks and trigger functions: -- as in lazy.nvim) dependencies = string[], -- Sorted list of resolved dependency -- names, as in lazy.nvim - main = string?, -- Main module name: the spec's `main`, - -- else the detected one (set before - -- the first `config` runs; nil until - -- then) + main = string?, -- The spec's `main`, in every hook. + -- Without one, the detected module, + -- set before the first `config` runs + -- and nil until then } < diff --git a/docs/spec.md b/docs/spec.md index 885d59c..81d76fa 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -96,7 +96,7 @@ The plugin data object passed to hooks and trigger functions: name = string, -- Resolved plugin name (alias for spec.name, as in lazy.nvim) dir = string, -- Plugin directory (alias for path, as in lazy.nvim) dependencies = string[], -- Sorted list of resolved dependency names, as in lazy.nvim - main = string?, -- Main module name: the spec's `main`, else the detected one (set before the first `config` runs; nil until then) + main = string?, -- The spec's `main`, in every hook. Without one, the detected module, set before the first `config` runs and nil until then } ``` diff --git a/lua/zpack/types.lua b/lua/zpack/types.lua index ab47eb5..19bb4b1 100644 --- a/lua/zpack/types.lua +++ b/lua/zpack/types.lua @@ -25,7 +25,8 @@ ---Plugin data passed to load callback from vim.pack.add and forwarded to ---spec hooks (init/config/opts/cond/build/deactivate). zpack's own fields are ----`spec`, `path`, and the late-resolved `main`; lazy.nvim parity adds +---`spec`, `path`, and `main` (the spec's, else detected before the first +---`config`); lazy.nvim parity adds ---`name` (alias for spec.name), `dir` (alias for path), and `dependencies` ---(sorted list of resolved dependency names from this plugin's outgoing ---deps in the resolved spec tree). From 6014b4aa96debe0150c2233bef31092d97ed96aa Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 04:12:07 -0800 Subject: [PATCH 8/9] test(registration): pin the spec's `main` on the plugin in cond and trigger functions --- tests/opts_test.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/opts_test.lua b/tests/opts_test.lua index 40922fb..65f3ac3 100644 --- a/tests/opts_test.lua +++ b/tests/opts_test.lua @@ -423,6 +423,32 @@ describe("plugin.main in config hooks", function() assert.are.same({ init = 'bare.module', opts = 'test.module' }, seen) end) + it("plugin.main is the spec's `main` in cond and trigger functions", function() + local seen = {} + + require('zpack').setup({ + spec = { + { + 'test/plugin', + main = 'test.module', + cond = function(plugin) + seen['cond: ' .. tostring(plugin.main)] = true + return true + end, + cmd = function(plugin) + seen['cmd: ' .. tostring(plugin.main)] = true + return 'MainInTrigger' + end, + }, + }, + defaults = { confirm = false }, + }) + + helpers.flush_pending() + pcall(vim.api.nvim_del_user_command, 'MainInTrigger') + assert.are.same({ ['cond: test.module'] = true, ['cmd: test.module'] = true }, seen) + end) + it("a `main` of false leaves the plugin's module to detection, as in lazy.nvim", function() helpers.write_plugin_files('falsy', { 'falsy/init.lua' }) local seen = {} From 61f42e07b7f412f635fd6cf96f99d596d05adfd7 Mon Sep 17 00:00:00 2001 From: zuqini Date: Sat, 14 Nov 2026 04:12:25 -0800 Subject: [PATCH 9/9] refactor(opts): drop `resolve_main`'s unreachable read of the spec's `main` --- lua/zpack/plugin_loader.lua | 2 +- lua/zpack/utils.lua | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lua/zpack/plugin_loader.lua b/lua/zpack/plugin_loader.lua index 9c07e92..4652d10 100644 --- a/lua/zpack/plugin_loader.lua +++ b/lua/zpack/plugin_loader.lua @@ -55,7 +55,7 @@ function M.run_config(src, plugin) utils.schedule_notify(("Failed to run config for %s: %s"):format(label, tostring(resolved_opts)), vim.log.levels.ERROR) return end - local main = utils.resolve_main(plugin, spec) + local main = utils.resolve_main(plugin) if type(spec.config) == "function" then local config_fn = spec.config --[[@as fun(plugin: zpack.Plugin, opts: table)]] diff --git a/lua/zpack/utils.lua b/lua/zpack/utils.lua index 4ffd415..ea935eb 100644 --- a/lua/zpack/utils.lua +++ b/lua/zpack/utils.lua @@ -327,11 +327,11 @@ end ---Resolve the main module for a plugin (for auto-setup) ---Inspired by lazy.nvim's loader.get_main() +---The spec's `main` is on the plugin from registration, so it is returned as is. ---Results are cached in plugin.main (for found modules) and state.resolve_main_not_found (for not-found) ---@param plugin zpack.Plugin ----@param spec zpack.Spec ---@return string? main_module The main module name, or nil if not found -M.resolve_main = function(plugin, spec) +M.resolve_main = function(plugin) if plugin.main ~= nil then return plugin.main end @@ -341,11 +341,6 @@ M.resolve_main = function(plugin, spec) return nil end - if spec.main then - plugin.main = spec.main - return spec.main - end - local name = plugin.spec.name if not name then state.resolve_main_not_found[cache_key] = true