junegunn / junegunn/vim-plug

Syntax loaded incorrectly/twice with lazy loaded filetype plugin

Open
#581 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Vim Script
Stars
35.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

When a plugin has a `after/syntax/lua.vim` file, and this plugin gets loaded `'for': ['lua']`, the main syntax plugin(s) (`syntax/lua.vim`) will not be loaded, when `b:current_syntax` might exist already.

In the case of https://github.com/raymond-w-ko/vim-lua-indent/blob/master/after/syntax/lua.vim#L7 this relies on a variable `lua_version` being set, but it gets not set from Vim's `syntax/lua.vim` when`b:current_syntax` is set already.

TEST CASE:
1. Create a `config.ld` file, it will have `ft=ld` and `b:current_syntax='ld'`
2. `set ft=lua` (manually or through a modeline)

vim-plug will call `runtime syntax/lua.vim`, but that finishes, because `b:current_syntax` is set to `'ld'` already.
Then `after/syntax/lua.vim` from the plugin gets sourced, and throws an error because of the unknown var.

I've noticed that passing in `, syn, 'after/'.syn` to `s:lod` will even cause it to be loaded twice in the end?!

So the following would actually fix it for this use case, but breaks the "Filetype-based on-demand loading" test (`'xxx/syntax', 'xxx/after/syntax'` is missing from `g:xxx`).

```diff
diff --git i/plug.vim w/plug.vim
index 95d8eab..a0d6b1c 100644
--- i/plug.vim
+++ w/plug.vim
@@ -489,7 +489,7 @@ endfunction

function! s:lod_ft(pat, names)
let syn = 'syntax/'.a:pat.'.vim'
- call s:lod(a:names, ['plugin', 'after/plugin'], syn, 'after/'.syn)
+ call s:lod(a:names, ['plugin', 'after/plugin'])
execute 'autocmd! PlugLOD FileType' a:pat
call s:doautocmd('filetypeplugin', 'FileType')
call s:doautocmd('filetypeindent', 'FileType')
```

When testing it manually, the `syntaxset` FileType event will trigger the syntax files to be loaded automatically, but for some reason that is not happening during the tests, although the autocommand is there?!
```
syntaxset FileType
* exe "set syntax=" . expand("")
```

A test case (`t/minimal.vim`):
```
set rtp+=../$PWD

call plug#begin('~/.vim/plugged')
exe "Plug '".expand("$PWD")."/p1', {'for': 'lua'}"
" Plug 'raymond-w-ko/vim-lua-indent', {'for': 'lua'}
call plug#end()
```

With `p1/after/syntax/lua.vim`:
```vim
echom lua_version
```

Then run it with `vim -Nu minimal.vim t.ld` and call `set ft=lua` in Vim:
```
Error detected while processing /home/user/Vcs/vim-plug/t/p1/after/syntax/lua.vim:
line 1:
E121: Undefined variable: lua_version
E15: Invalid expression: lua_version
5
```

A fix for this might be to `unlet! b:current_syntax` in `s:lod_ft`, but then it will (still) cause the syntax files to be sourced twice - which seems to be wrong in the first place?!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.