Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 24, 2024

Conversation

wookayin
Copy link
Contributor

@wookayin wookayin commented Feb 6, 2022

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.

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
Copy link
Contributor Author

wookayin commented Feb 6, 2022

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.

plug.vim Outdated Show resolved Hide resolved
tomtomjhj added a commit to tomtomjhj/init.vim that referenced this pull request Oct 4, 2022
@tracyone
Copy link

Any plan to merg this PR?

@junegunn
Copy link
Owner

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.

@tracyone
Copy link

" 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
Copy link

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.

@liskin
Copy link

liskin commented Nov 13, 2023

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

vim-plug/plug.vim

Lines 1056 to 1061 in 154f60b

if spec.do[0] == ':'
if !get(s:loaded, name, 0)
let s:loaded[name] = 1
call s:reorg_rtp()
endif
call s:load_plugin(spec)
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

@junegunn junegunn merged commit 2f8f04c into junegunn:master Feb 24, 2024
1 check passed
@junegunn
Copy link
Owner

Merged, thank you all for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants