vim-plug
vim-plug copied to clipboard
Source lua files (if any) from rtp when loading a plugin
Neovim 0.5.0 allows lua files to be used in runtime files (such as
plugin, ftdetect, etc.) as well as vimscript files. Indeed, some
plugins have plugin/*.lua scripts only, but not plugin/*.vim;
such plugins cannot be sourced and work properly if it is lazy-loaded.
I think this still would need more thoroughly tested, but not sure how I would cover all the possible scenarios.
It looks like that this has only in effect when it comes to lazy-loading (or manually calling plug#load). When I don't lazy-load the plugin, it seems that the plugin/*.lua runtime files are being sourced even without this patch (not sure why and where it happens...). But when lazy-loading such plugins, plugin/*.lua are currently not being sourced.
Any plan to merg this PR?
I don't use Neovim anymore, so I can't verify if it works as described. But if any of you can test it and confirm that it works, I'll merge the PR.
" it seems that the plugin/*.lua runtime files are being sourced even without this patch (not sure why and where it happens...)"
@wookayin
I thinks this is neovim's behavior, once you add plugin's directory into runtimepath, neovim will source lua files in plugin directory when starting neovim.
It is easy to test
Use following script as neovim's config, nvim -u mini_vimrc --startuptime nvim_start.log
set nocompatible
"telescope
execute 'set runtimepath+='.$HOME.'/.vim/bundle/plenary.nvim'
execute 'set runtimepath+='.$HOME.'/.vim/bundle/telescope.nvim'
filetype plugin indent on
syntax on
syntax enable
set background=dark
set termguicolors
colorscheme default
set t_Co=256
Here is the start log:
044.828 001.519 001.498: sourcing /Users/tracyone/.vim/bundle/telescope.nvim/plugin/telescope.lua
Why we need this PR?
I found some of neovim plugins can't be lazy loaded using vim-plug.
Following is an example code of lazy loading.
"Do not load plugin by default.
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate', 'on': []}
Plug 'nvim-treesitter/nvim-treesitter-refactor', {'on': []}
Plug 'nvim-treesitter/nvim-treesitter-context', {'on': []}
function! LoadPlugins(timer) abort
call plug#load(['nvim-treesitter', 'nvim-treesitter-refactor', 'nvim-treesitter-context'])
"require setup module of treesittier(lua/treesittier_nvim.lua)
call v:lua.require("treesittier_nvim")
endfunction
"run LoadPlugins function after 300ms
call timer_start(300, function('LoadPlugins'), {'repeat': 1})
Following plugins only have lua files in plugin directory, when call plug#load, lua files were skiped, so They are failing to lazy-loaded.
- nvim-treesitter/nvim-treesitter and plugins that base on it
- hrsh7th/nvim-cmp and plugins that base on it
- nvim-telescope/telescope.nvim and plugins that base on it
After apply this PR, plugins that are mentioned above can be lazy-loaded, I think this PR is OK to merge.
But if any of you can test it and confirm that it works, I'll merge the PR.
It does not, unfortunately. I mean—it's not enough for the :TSUpdate post-install hook of https://github.com/nvim-treesitter/nvim-treesitter.
Their README recommends the following:
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
Note there's no on: nor for: so vim-plug adds it to runtimepath immediately (before it's installed) and then at https://github.com/junegunn/vim-plug/blob/154f60b7ecbfb158a497e751aacab590247c69d4/plug.vim#L1056-L1061 the runtimepath isn't updated as it doesn't need to change (s:loaded[name] == 1). Neovim, however, seems to cache the available lua modules (lua/*.lua, not plugin/*.lua) only when runtimepath is (re)set (more about this at https://neovim.io/doc/user/lua.html#lua-package-path). So when plugin/nvim-treesitter.lua tries to require("nvim-treesitter").setup(), it fails:
Vim(source):E5113: Error while calling lua chunk: .../nvim/plugged/nvim-treesitter/plugin/nvim-treesitter.lua:9: module 'nvim-treesitter' not found:
no field package.preload['nvim-treesitter']
no file './nvim-treesitter.lua'
no file '/__w/neovim/neovim/.deps/usr/share/luajit-2.1/nvim-treesitter.lua'
no file '/usr/local/share/lua/5.1/nvim-treesitter.lua'^@^Ino file '/usr/local/share/lua/5.1/nvim-treesitter/init.lua'
no file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/nvim-treesitter.lua'
no file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/nvim-treesitter/init.lua'
no file './nvim-treesitter.so'
no file '/usr/local/lib/lua/5.1/nvim-treesitter.so'
no file '/__w/neovim/neovim/.deps/usr/lib/lua/5.1/nvim-treesitter.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'require'
I.../nvim/plugged/nvim-treesitter/plugin/nvim-treesitter.lua:9: in main chunk
Adding let &rtp = &rtp before call s:load_plugin(spec) fixes this. (It probably belongs elsewhere though.)
If anyone wants to play in the same environment I'm doing this in, then:
FROM debian:unstable@sha256:0862a2d6f153cb5287d7a4e25869cb5a8852765e62ff370e970d9cfcc26658aa
RUN apt-get update -y
RUN apt-get install -q -y curl git build-essential
RUN curl -LSsf https://github.com/neovim/neovim/releases/download/v0.9.4/nvim-linux64.tar.gz | tar xz
RUN sh -c 'curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/154f60b7ecbfb158a497e751aacab590247c69d4/plug.vim'
RUN mkdir -p ~/.config/nvim \
&& echo "call plug#begin()" >>~/.config/nvim/vimrc.vim \
&& echo "Plug 'nvim-treesitter/nvim-treesitter', #{do: ':TSUpdate', commit: '075a64addc33390028ea124a1046a43497f05cd1'}" >>~/.config/nvim/vimrc.vim \
&& echo "call plug#end()" >>~/.config/nvim/vimrc.vim \
&& echo "vim.cmd.source(vim.fn.stdpath('config') .. '/vimrc.vim')" >>~/.config/nvim/init.lua \
&& :
docker build -t nvim-treesitter-lua-bug .
docker run --rm -it nvim-treesitter-lua-bug:latest
Merged, thank you all for your help!