diff --git a/runtime/doc/pack.txt b/runtime/doc/pack.txt index 8a9da300e1..f3a8cf2948 100644 --- a/runtime/doc/pack.txt +++ b/runtime/doc/pack.txt @@ -379,7 +379,7 @@ add({specs}, {opts}) *vim.pack.add()* Load `plugin/` files and `ftdetect/` scripts. If `false`, works like `:packadd!`. If function, called with plugin data and is fully responsible for loading plugin. Default - `false` during startup and `true` afterwards. + `false` during |init.lua| sourcing and `true` afterwards. • {confirm}? (`boolean`) Whether to ask user to confirm initial install. Default `true`. diff --git a/runtime/lua/vim/pack.lua b/runtime/lua/vim/pack.lua index 6180f23db5..265a366d48 100644 --- a/runtime/lua/vim/pack.lua +++ b/runtime/lua/vim/pack.lua @@ -743,8 +743,9 @@ local function pack_add(plug, load) -- NOTE: The `:packadd` specifically seems to not handle spaces in dir name vim.cmd.packadd({ vim.fn.escape(plug.spec.name, ' '), bang = not load, magic = { file = false } }) - -- Execute 'after/' scripts if not during startup (when they will be sourced - -- automatically), as `:packadd` only sources plain 'plugin/' files. + -- The `:packadd` only sources plain 'plugin/' files. Execute 'after/' scripts + -- if not during startup (when they will be sourced later, even if + -- `vim.pack.add` is inside user's 'plugin/') -- See https://github.com/vim/vim/issues/15584 -- Deliberately do so after executing all currently known 'plugin/' files. if vim.v.vim_did_enter == 1 and load then @@ -760,7 +761,7 @@ end --- @inlinedoc --- Load `plugin/` files and `ftdetect/` scripts. If `false`, works like `:packadd!`. --- If function, called with plugin data and is fully responsible for loading plugin. ---- Default `false` during startup and `true` afterwards. +--- Default `false` during |init.lua| sourcing and `true` afterwards. --- @field load? boolean|fun(plug_data: {spec: vim.pack.Spec, path: string}) --- --- @field confirm? boolean Whether to ask user to confirm initial install. Default `true`. @@ -788,7 +789,7 @@ end --- @param opts? vim.pack.keyset.add function M.add(specs, opts) vim.validate('specs', specs, vim.islist, false, 'list') - opts = vim.tbl_extend('force', { load = vim.v.vim_did_enter == 1, confirm = true }, opts or {}) + opts = vim.tbl_extend('force', { load = vim.v.vim_did_init == 1, confirm = true }, opts or {}) vim.validate('opts', opts, 'table') local plug_dir = get_plug_dir() diff --git a/test/functional/plugin/pack_spec.lua b/test/functional/plugin/pack_spec.lua index 5662e173d0..82e0277775 100644 --- a/test/functional/plugin/pack_spec.lua +++ b/test/functional/plugin/pack_spec.lua @@ -620,35 +620,42 @@ describe('vim.pack', function() end) describe('startup', function() - local init_lua = '' + local config_dir, pack_add_cmd = '', '' + before_each(function() - init_lua = vim.fs.joinpath(fn.stdpath('config'), 'init.lua') - fn.mkdir(vim.fs.dirname(init_lua), 'p') + config_dir = fn.stdpath('config') + fn.mkdir(vim.fs.joinpath(config_dir, 'plugin'), 'p') + + pack_add_cmd = ('vim.pack.add({ %s })'):format(vim.inspect(repos_src.plugindirs)) end) + after_each(function() - pcall(vim.fs.rm, init_lua, { force = true }) + vim.fs.rm(config_dir, { recursive = true, force = true }) end) - it('works in init.lua', function() - local pack_add_cmd = ('vim.pack.add({ %s })'):format(vim.inspect(repos_src.plugindirs)) - fn.writefile({ pack_add_cmd, '_G.done = true' }, init_lua) + local function assert_loaded() + eq('plugindirs main', exec_lua('return require("plugindirs")')) - local function assert_loaded() - eq('plugindirs main', exec_lua('return require("plugindirs")')) - - -- Should source 'plugin/' and 'after/plugin/' exactly once - eq({ true, true }, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }')) - eq({ 'p', 'a' }, n.exec_lua('return _G.DL')) - end + -- Should source 'plugin/' and 'after/plugin/' exactly once + eq({ true, true }, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }')) + eq({ 'p', 'a' }, n.exec_lua('return _G.DL')) + end + local function assert_works() -- Should auto-install but wait before executing code after it n.clear({ args_rm = { '-u' } }) n.exec_lua('vim.wait(500, function() return _G.done end, 50)') assert_loaded() - -- Should only `:packadd!` already installed plugin + -- Should only `:packadd!`/`:packadd` already installed plugin n.clear({ args_rm = { '-u' } }) assert_loaded() + end + + it('works in init.lua', function() + local init_lua = vim.fs.joinpath(config_dir, 'init.lua') + fn.writefile({ pack_add_cmd, '_G.done = true' }, init_lua) + assert_works() -- Should not load plugins if `--noplugin`, only adjust 'runtimepath' n.clear({ args = { '--noplugin' }, args_rm = { '-u' } }) @@ -656,6 +663,13 @@ describe('vim.pack', function() eq({}, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }')) eq(vim.NIL, n.exec_lua('return _G.DL')) end) + + it('works in plugin/', function() + local plugin_file = vim.fs.joinpath(config_dir, 'plugin', 'mine.lua') + fn.writefile({ pack_add_cmd, '_G.done = true' }, plugin_file) + -- Should source plugin's 'plugin/' files without explicit `load=true` + assert_works() + end) end) it('shows progress report during installation', function()