hashivim / vim-terraform

basic vim/terraform integration
http://hashivim.github.io/vim-terraform
ISC License
1.07k stars 110 forks source link

.tf filetype detection seems broken in more recent Vim/NeoVim versions? #179

Closed smheidrich closed 1 year ago

smheidrich commented 1 year ago

I'm running NeoVim 0.7.2 with this plugin installed via vim-plug, but when I open a .tf file, the filetype is just empty. It works fine for .hcl files.

Curiously, if I comment out this line where you disable Vim's native .ft filetype detection, it works. But that seems to be because recent versions of Vim are able to recognize .tf files as Terraform, so with this line removed, Vim's native detection kicks in and sets the filetype:

:autocmd filetypedetect BufRead,BufNewFile *.tf
--- Autocommands ---
filetypedetect  BufReadPost
    *.tf      call dist#ft#FTtf()

So that can't be the underlying issue (although it would probably make sense to have a condition there to let Vim handle this for recent versions, because then users who want to be able to detect both file types denoted by .tf can do so).

Based on Vim's own autocmd being in group filetypedetect, I tried adding filetypedetect as the group for the four autocmds setup by vim-terraform, and that made it detect the filetype correctly as well. But there is probably a reason why you don't have filetypedetect as the group there, so let me know what you think.

dimbleby commented 1 year ago

filetype detection is executed from https://github.com/vim/vim/blob/be19d78c3d44221cbc38fbb5bac19302345c1def/runtime/filetype.vim#L2745, which is already inside a group: it is correct not to add another group in our own file.

https://github.com/vim/vim/blob/938ae280c79b8cdb0fca60336ec4c090ecd8bb5a/runtime/doc/filetype.txt#L198-L199

Note that there is no "augroup" command, this has already been done when sourcing your file.

133 was a bit similar, the reporter in that was using a plugin manager that messed with the runtime path and not following the associated instructions correctly.

Perhaps you're doing something similar?

smheidrich commented 1 year ago

I see, thanks for the explanation and the links!

I haven't figured out exactly what's causing this yet but as you explained this plugin is doing everything correctly, so I'll close the ticket.

smheidrich commented 1 year ago

All right, for anyone who has the same issue in the future, the source of the bug in my case was another plugin's (vimoutliner) ftdetect script leaving the augroup filetypedetect context at the end of its execution. I've opened an issue with them here: https://github.com/vimoutliner/vimoutliner/issues/185

I'm sure this is not the only plugin that does this, so again for anyone who has the same issue, it probably makes sense to go into your Vim plugin folder and run rg 'augroup' $(find . -path '*/ftdetect/*.vim') to see if you have any ftdetect scripts that (wrongly) close the augroup.

What pointed me towards this being a potential cause was this comment in Vim's filetype.vim, where it is noted that any of the ftdetect scripts it ran could have ended the augroup context.