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.72k stars 262 forks source link

Bootstrap with configs #1138

Open Susensio opened 1 year ago

Susensio commented 1 year ago

When I follow the steps documented for bootstraping packer, if I put some pluging specific config, it breaks. This is running from a clean nvim installation

my config file

-- Automatically install packer
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()

packer = require("packer")

-- Install your plugins here
return packer.startup(function(use)
  use "wbthomason/packer.nvim"

  -- Auto close brackets
  use {"windwp/nvim-autopairs",
    config = function() require("nvim-autopairs").setup() end
  }

  -- Automatically set up your configuration after cloning packer.nvim
  -- Put this at the end after all plugins
  if packer_bootstrap then
    packer.sync()
  end
end)

the command I'm calling:

nvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'

the output

[packer.nvim] [WARN  15:27:17] clean.lua:75: Could not remove /home/susensio/.local/share/nvim/site/pack/packer/start/nvim-autopairspacker.nvim: Error running config for nvim-autopairs: [string "..."]:0: module 'nvim-autopairs' not found:
^Ino field package.preload['nvim-autopairs']
^Ino file './nvim-autopairs.lua'
^Ino file '/usr/share/luajit-2.1.0-beta3/nvim-autopairs.lua'
^Ino file '/usr/local/share/lua/5.1/nvim-autopairs.lua'
^Ino file '/usr/local/share/lua/5.1/nvim-autopairs/init.lua'
^Ino file '/usr/share/lua/5.1/nvim-autopairs.lua'
^Ino file '/usr/share/lua/5.1/nvim-autopairs/init.lua'
^Ino file '/home/susensio/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/nvim-autopairs.lua'
^Ino file '/home/susensio/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/nvim-autopairs/init.lua'
^Ino file '/home/susensio/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/nvim-autopairs.lua'
^Ino file '/home/susensio/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/nvim-autopairs/init.lua'
^Ino file './nvim-autopairs.so'
^Ino file '/usr/local/lib/lua/5.1/nvim-autopairs.so'
^Ino file '/usr/lib/x86_64-linux-gnu/lua/5.1/nvim-autopairs.so'
^Ino file '/usr/local/lib/lua/5.1/loadall.so'
^Ino file '/home/susensio/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/nvim-autopairs.so'⏎                      

What am I doing wrong? How can I bootstrap packer and my plugins? BTW, if I just start vim, it works without errors. Same if I run nvim --headless -c 'autocmd User PackerComplete quitall', but that wont update any new plugins if packer is already installed.

Susensio commented 1 year ago

It seems that the problem is I'm running PackerSync twice. I've managed to solve it by checking if headless, but I'm not sure if this is a good approach.

-- Automatically install packer
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()
local in_headless = #vim.api.nvim_list_uis() == 0

packer = require("packer")

-- Install your plugins here
return packer.startup(function(use)
  use "wbthomason/packer.nvim"

  -- Auto close brackets
  use {"windwp/nvim-autopairs",
    config = function() require("nvim-autopairs").setup() end
  }

  -- Automatically set up your configuration after cloning packer.nvim
  -- Put this at the end after all plugins
  if (packer_bootstrap and not in_headless) then
    packer.sync()
  end
end)
dangh commented 1 year ago

You can drop PackerSync command, just nvim --headless -c 'autocmd User PackerComplete quitall' is enough. First time you run nvim (in headless mode) it will wait for packer.sync() to complete and exit.

Susensio commented 1 year ago

If I do that, that command won't sync my plugins unless i'm bootstraping packer as well. I was looking for an idempotent command I could run no matter if packer is installed or not. That way, I can share my dotfiles easily between machines. I'm trying to script a yadm bootstrap that works always.

sohooo commented 1 year ago

I'm also interested in a clean way to headless bootstrap the config, including plugins, treesitter grammars, etc.

RoryNesbitt commented 1 year ago

~I have almost the same error but even when not making use of headless.~

~I make a lot of use of config = for plugins. Whenever I bootstrap the first plugin it reaches that requires itself in config = causes an error and the entire install fails.~

~It's as if config (or setup) is running before the plugin is downloaded~

My issue was because I done config = require("plugin").setup() not config = function() ... end
The former works when the plugin is already installed but throws an error when it doesn't exist, the latter works perfectly. Thanks to this post for helping me debug that though