wbthomason / packer.nvim

A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config
MIT License
7.75k stars 261 forks source link

Clear definition between install and update hooks #1050

Open itmecho opened 1 year ago

itmecho commented 1 year ago

Describe the feature

I've got this minimal init.lua which installs packer and then nvim-treesitter. I've got run = ':TSUpdate' set but I get an error that TSUpdate is not an editor command. I'm not sure if this is a bug or if it's only supposed to be run on update rather than install? If it isn't a bug, how can I tell in a run function whether it's being run as part of an install or update hook?

Demo

Use the init.lua below, remove any packer files with rm -Rf ~/.local/share/nvim/site/pack/packer ~/.config/nvim/plugin/packer_compiled.lua, then open neovim

init.lua ```lua local ensure_packer = function() local fn = vim.fn local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim" if fn.empty(fn.glob(install_path)) > 0 then fn.system({ "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }) vim.cmd([[packadd packer.nvim]]) return true end return false end local packer_bootstrap = ensure_packer() return require("packer").startup(function(use) use("wbthomason/packer.nvim") use({ "nvim-treesitter/nvim-treesitter", run = ":TSUpdate", config = function() require("nvim-treesitter.configs").setup({ hightlight = { enabled = true, }, }) end, }) -- Automatically set up your configuration after cloning packer.nvim -- Put this at the end after all plugins if packer_bootstrap then require("packer").sync() end end) ```
wbthomason commented 1 year ago

Right now, packer doesn't differentiate between install and update hooks - run executes for both events. If you'd like finer-grained control, this would be pretty easy to add, and I'm happy to help guide a PR.

itmecho commented 1 year ago

Thanks for the reply! For the moment, I've got around the issue by changing run to a function that only runs the command if it exists:

  ...
  run = function()
    if vim.fn.exists(':TSUpdate') == 2 then
      vim.cmd(':TSUpdate')
    end
  end,
  ...
itmecho commented 1 year ago

I've done a small PR that implements a solution for this, let me know if it's what you had in mind!

masaeedu commented 1 year ago

The odd thing is that:

use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate' }

is actually given as an example in the README, but copying this example exactly into one's packer config leads to the error you are observing.

Forgetting about the specifics of treesitter for the moment, if a plugin requires that you run some setup code once, immediately after installation, and this setup code depends on the plugin being loaded, is run the wrong place to put it? Should there be some other mechanism for doing so?