1

I'm using this minimal setup for easy repro:

call plug#begin()
Plug 'nvim-tree/nvim-tree.lua'
lua require('nvim-tree-init')
call plug#end()

Running neovim results in this error upon opening of neovim:

Error detected while processing C:\Users\myUser\AppData\Local\nvim\init.vim:
line    3:
E5108: Error executing lua ...rs\myUser\AppData\Local\nvim/lua/nvim-tree-init.lua:9: module 'nvim-tree' not found:
    no field package.preload['nvim-tree']
    no file '.\nvim-tree.lua'
    no file 'C:\Program Files\Neovim\bin\lua\nvim-tree.lua'
    no file 'C:\Program Files\Neovim\bin\lua\nvim-tree\init.lua'
    no file '.\nvim-tree.dll'
    no file 'C:\Program Files\Neovim\bin\nvim-tree.dll'
    no file 'C:\Program Files\Neovim\bin\loadall.dll'
stack traceback:
    [C]: in function 'require'
    ...rs\myUser\AppData\Local\nvim/lua/nvim-tree-init.lua:9: in main chunk
    [C]: in function 'require'
    [string ":lua"]:1: in main chunk
Press ENTER or type command to continue

This error disappears when lua require line is called after call plug#end() line.

Why does this happen?

For nvim-tree-init.lua, I'm using code from here.

Why am I doing this?

I use config model in which every source plugin is coupled with all of that plugins settings in separate file for each plugin. I don't want lua require(plugin) to be decoupled from all other plugin settings (which are all in plug#begin()-plug#end() block).

1
  • 3
    I suspect that vim-plug plays with the vim runtime path (rtp) that is only restored after the plug#end() call. Dec 18, 2023 at 12:31

1 Answer 1

1

This is by design. You can start requiring lua modules only after plug#end() is called where &runtimepath is updated w.r.t. the registered plugins.

vim-plug is not very suited for configuring Lua plugins in a modular way; in many vim plugins configs are expected to sourced before the plugin sources (plugin/*.vim) are executed, but for Lua plugins it's not the case. If you still want to make the plugin declaration and config stay closer, then your option includes:

  1. Defer the execution, e.g.:
vim.schedule(function()
   require('nvim-tree-init').setup(...)
end)
  1. Use the autocmd that will be fired when plugin loads:
vim.api.nvim_create_autocmd('User', {
  pattern = 'nvim-tree',
  callback = function()
    -- ... setup
  end,
})

and many more. After all, you may want to use a different plugin manager.

1
  • 2
    I wanted to stick to vim-plug since I try my best for config to be valid on both vanilla and neovim
    – Fedja
    Jan 19 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.