vim-plug icon indicating copy to clipboard operation
vim-plug copied to clipboard

Source lua files (if any) from rtp when loading a plugin

Open wookayin opened this issue 3 years ago • 6 comments

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.

wookayin avatar Feb 06 '22 19:02 wookayin

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.

wookayin avatar Feb 06 '22 19:02 wookayin

Any plan to merg this PR?

tracyone avatar Nov 12 '23 14:11 tracyone

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.

junegunn avatar Nov 13 '23 10:11 junegunn

" 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:

nvim_start.log

044.828  001.519  001.498: sourcing /Users/tracyone/.vim/bundle/telescope.nvim/plugin/telescope.lua

tracyone avatar Nov 13 '23 15:11 tracyone

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.

  1. nvim-treesitter/nvim-treesitter and plugins that base on it
  2. hrsh7th/nvim-cmp and plugins that base on it
  3. 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.

tracyone avatar Nov 13 '23 15:11 tracyone

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

liskin avatar Nov 13 '23 22:11 liskin

Merged, thank you all for your help!

junegunn avatar Feb 24 '24 14:02 junegunn