packer.nvim icon indicating copy to clipboard operation
packer.nvim copied to clipboard

Clear definition between install and update hooks

Open itmecho opened this issue 2 years ago • 4 comments

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

itmecho avatar Sep 05 '22 12:09 itmecho

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.

wbthomason avatar Sep 05 '22 18:09 wbthomason

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 avatar Sep 06 '22 09:09 itmecho

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

itmecho avatar Sep 06 '22 09:09 itmecho

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?

masaeedu avatar Dec 07 '22 18:12 masaeedu