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.9k stars 263 forks source link

Packer breaks if `packer_compiled.vim` depends on lua files in `stdpath('config')/plugin/` #401

Open francisco285 opened 3 years ago

francisco285 commented 3 years ago

Steps to reproduce

(these steps are also described here)

Let's say we use the following init.lua file in ~/.config/nvim:

-- vim: expandtab:shiftwidth=2:tabstop=2:softtabstop=2
vim.cmd('set noundofile')
vim.cmd('set nowritebackup')
vim.cmd('set noswapfile')
vim.cmd('set nowritebackup')
vim.cmd('set clipboard=unnamed,unnamedplus')
vim.cmd('set wildignorecase')
vim.cmd('set ignorecase')
vim.cmd('set hidden')

Global = {
  colorscheme = 'tokyonight'
}

local install_path = vim.fn.expand(vim.fn.stdpath('data') .. '/site/pack/packer/opt/packer.nvim')

if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  vim.cmd('!git clone https://github.com/wbthomason/packer.nvim ' .. install_path)
end

vim.cmd([[packadd packer.nvim]])

require('packer').startup(
  function(use)
    use { 'wbthomason/packer.nvim', opt = true }

    use {
      'folke/tokyonight.nvim',
      config = function()

        -- Using the global variable here makes the generated packer_compile.vim try to use it as well
        if Global.colorscheme == 'tokyonight' then
          vim.cmd('colorscheme tokyonight')
        end
      end
    }
  end
)

If we use it in ~/.config/nvim/init.lua and run :PackerCompile, everything works (assuming there wasn't already a packer_compiled.vim created with some error), if we restart neovim, everything still works.

If we put it in ~/.config/nvim/plugin/init.lua, open neovim, run :PackerCompile, everything works (because the global variable was already set on startup), but if we restart neovim, packer breaks: image

This happens because plugin/init.lua is loaded after packer_compiled.vim, packer_compiled.vim tries to use global "Global" and fails because it is only set in plugin/init.lua

Actual behavior

Packer breaks if packer_compiled.vim depends on lua files in stdpath('config')/plugin/ (in the example I used a global variable, but there may be other cases)

Expected behavior

Packer should work as usual when using lua files in stdpath('config')/plugin/ (this is possible with #14686)

Aditional context

"Fixing" #201 would probably fix this issue as well

wbthomason commented 3 years ago

I commented on #201, but: this will soon not matter. As a temporary workaround, I would suggest changing the value of config.compile_path.