Skip to content

Commit

Permalink
fix: make sure plugin/* is loaded before plugin.run
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
lewis6991 committed Dec 13, 2024
1 parent 23fd6a0 commit 02c7f26
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lua/pckr/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ local function packadd(plugin, bang)
end

--- @param plugin Pckr.Plugin
function M.load_plugin(plugin)
--- @param load_plugin_star? boolean
function M.load_plugin(plugin, load_plugin_star)
local plugin_name = plugin.name

if plugin.loaded then
Expand Down Expand Up @@ -218,13 +219,13 @@ function M.load_plugin(plugin)
fmt_debug('Loading dependencies of %s', plugin_name)
for _, name in ipairs(plugin.requires) do
local all_plugins = require('pckr.plugin').plugins_by_name
M.load_plugin(all_plugins[name])
M.load_plugin(all_plugins[name], load_plugin_star)
end
end

fmt_debug('Loading %s', plugin_name)
measure('packadd', function()
packadd(plugin, true)
packadd(plugin, not load_plugin_star)
end)

apply_config(plugin, 'config')
Expand Down
2 changes: 1 addition & 1 deletion lua/pckr/sync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ local function post_update_hook(plugin, disp)
if plugin.run or plugin.start then
async.schedule()
local loader = require('pckr.loader')
loader.load_plugin(plugin)
loader.load_plugin(plugin, true)
end

if not plugin.run then
Expand Down
92 changes: 92 additions & 0 deletions test/actions_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local helpers = require('nvim-test.helpers')
local exec_lua = helpers.exec_lua
local eq = helpers.eq
local uv = vim.uv or vim.loop

--- @param path string
local function wait_for_file(path)
local i = 0
while i < 100 and not uv.fs_stat(path) do
i = i + 1
helpers.sleep(10)
end
if i == 100 then
error('timeout waiting for ' .. path, 2)
end
end

--- @param path string
--- @param text string|string[]
local function write_file(path, text)
local f = assert(io.open(path, 'w'))
if type(text) == 'string' then
text = { text }
end

for _, line in ipairs(text) do
f:write(line)
f:write('\n')
end
f:close()
end

describe('pckr actions', function()
local tmpdir --- @type string

setup(function()
helpers.clear()

tmpdir = uv.cwd() .. '/scratch/pckr'
-- tmpdir = assert(uv.os_tmpdir()) .. '/pckr'
helpers.fn.system({ 'rm', '-rf', tmpdir })
helpers.fn.system({ 'mkdir', '-p', tmpdir })
end)

it('can install local plugins', function()
-- Fixes #50 where 'run' was being called before plugin/* was loaded

local foo_plugin_dir = tmpdir .. '/foo_plugin'

helpers.fn.system({ 'mkdir', foo_plugin_dir })
helpers.fn.system({ 'mkdir', foo_plugin_dir..'/plugin' })

write_file(foo_plugin_dir..'/plugin/foo.lua', {
'_G.loaded_plugin_foo = true'
})

local init_lua = tmpdir .. '/init.lua'
write_file(init_lua, string.format([[
-- Make pckr available
vim.opt.rtp:append('.')
local pckr = require('pckr')
pckr.setup({
pack_dir = '%s'
})
pckr.add({
{
'%s',
run = function()
_G.did_run_pre = true
assert(_G.loaded_plugin_foo)
_G.did_run_post = true
end,
}
})
]], tmpdir, foo_plugin_dir))

helpers.clear(init_lua)

wait_for_file(tmpdir..'/pack/pckr/opt/foo_plugin')

local install_dir = tmpdir..'/pack/pckr/opt/foo_plugin'

eq('link', uv.fs_lstat(install_dir).type)
eq('file', uv.fs_stat(install_dir..'/plugin/foo.lua').type)

eq(true, exec_lua[[return _G.did_run_pre]])
eq(true, exec_lua[[return _G.did_run_post]])
end)

end)

0 comments on commit 02c7f26

Please sign in to comment.