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

[Bug Report] ftdetect/*.vim files are sourced twice #706

Open
laggardkernel opened this issue Apr 24, 2021 · 4 comments
Open

[Bug Report] ftdetect/*.vim files are sourced twice #706

laggardkernel opened this issue Apr 24, 2021 · 4 comments

Comments

@laggardkernel
Copy link

laggardkernel commented Apr 24, 2021

Does this bug happen when you install plugin without vim-polyglot?

No

Describe the bug:

From vim --startuptime log, I found my ftdetect/*.vim files are loaded twice. After lots of time digging, I figured out it was related with theses lines in autoload/polyglot/init.vim, first introduces in c45f0b8

" Load user-defined filetype.vim and oter plugins ftdetect first
" This is to use polyglot-defined ftdetect always as fallback to user settings
augroup filetypedetect
  runtime! filetype.vim
  runtime! ftdetect/*.vim
augroup END

To put it in a simple way, runtime! filetype.vim sources share/vim/vim82/filetype.vim, which also run runtime! ftdetect/*.vim

For detailed analysis

Analysis base from verbose log vim -V99debug.log.

Let's begin with how a filetype.vim is loaded. Unlike plugin/ files handled by vim automatically, filetype.vim files are only loaded with executing filetype plugin indent on.

filetype plugin indent on triggers searching for filetype.vim

  • Found this plugin vim-polyglot first, sources vim-ployglot/filetype.vim, which just call polyglot#init#init()

    • Within init.vim
      • Redefine func! polyglot#init#init empty to avoid call loop
      • Calls runtime! filetype.vim
        • Searching for filetype.vim
          • Found vim-ployglot/filetype.vim, skipped
          • Found default share/vim/vim82/filetype.vim
            • Runs runtime! ftdetect/*.vim
              • Search for ftdetect/*.vim and sourcing them (first time loading)
      • Calls runtime! ftdetect/*.vim
        • Search for ftdetect/*.vim and sourcing them (second time loading)
  • It found the default share/vim/vim82/filetype.vim, cause variable did_load_filetypes is set, finish early

@sheerun
Copy link
Owner

sheerun commented Apr 24, 2021

They are sourced twice to support different loading scenarios on vim, nvim and different package managers for vim like vim pack, vim-plug, etc. second load takes 0ms because code is not executed

@sheerun
Copy link
Owner

sheerun commented Apr 24, 2021

You might want to read vim/vim#7056

Sourcing twice is actually for vim-polyglot to load faster, not slower, because it ensures it's loaded before vim-native ftdetect, preventing it from loading second time fully, and saving you 25ms of startup time. In fact it makes vim start faster than without any plugins.

@laggardkernel
Copy link
Author

I'm not talking about ftdetect/polyglot.vim, which just call call polyglot#init#init(). Cause the init() is redefined as a dummy function when it's first called. The next time when it's called, it does nothing and basically being skipped.

The reason I reported this issue is that I found my custom ftdetect files, ~/.config/nvim/ftdetect/zsh.vim and ftdetect/tmux.vim being sourced twice from the --startuptime log.

augroup filetypedetect
  runtime! filetype.vim
  runtime! ftdetect/*.vim
augroup END

From what I've digged, runtime! filetype.vim will source the vim builtin filetype.vim, within which runtime! ftdetect/*.vim is called. We may not need another runtime! ftdetect/*.vim in the above code.

But I've never thought about when loading vim-polyglot with different pkg managers. I really have no idea about this part.

If I wanna prevent my custom ftdetect being sourced twice, the best solution for now is use a custom flag like g:ftdetect_zsh_loaded and check it at the beginning of the file?

@sheerun
Copy link
Owner

sheerun commented Jun 9, 2021

I need to debug this.. no idea why it happens

@sheerun sheerun closed this as completed in 4899585 Jun 9, 2021
@sheerun sheerun reopened this Jun 9, 2021
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

No branches or pull requests

2 participants