Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/zpack.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -1248,9 +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?, -- Detected main module name (set
-- before the first `config` runs; nil
-- inside `opts` functions 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
}
<

Expand Down
2 changes: 1 addition & 1 deletion docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?, -- The spec's `main`, in every hook. Without one, the detected module, set before the first `config` runs and nil until then
}
```

Expand Down
2 changes: 1 addition & 1 deletion lua/zpack/plugin_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)]]
Expand Down
1 change: 1 addition & 0 deletions lua/zpack/registration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 or nil
local dep_set = state.dependency_graph[pack_spec.src]
if dep_set then
local dep_names = {}
Expand Down
5 changes: 3 additions & 2 deletions lua/zpack/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -35,7 +36,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[]
Expand Down
13 changes: 5 additions & 8 deletions lua/zpack/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -362,11 +357,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 compiled
if mod and not seen[mod] then
seen[mod] = true
mods[#mods + 1] = mod
Expand Down
79 changes: 79 additions & 0 deletions tests/opts_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,85 @@ 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 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 = {}

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"

Expand Down
26 changes: 26 additions & 0 deletions tests/utils_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,30 @@ 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)

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'
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)
Loading