fix(api): nvim_get_option_value FileType autocmd handling #37414

Problem:
nvim_get_option_value with "filetype" set silently returns incorrect
defaults if autocommands are blocked, like when they're already running.

Solution:
Allow its FileType autocommands to nest: `do_filetype_autocmd(force=true)`.
Also error if executing them fails, rather than silently return wrong defaults.

Endless nesting from misbehaving scripts should be prevented by the recursion
limit in apply_autocmds_group, which is 10.
This commit is contained in:
Sean Dewar
2026-03-19 00:11:59 +00:00
committed by GitHub
parent 19715e6e8a
commit 6f12663de5
3 changed files with 46 additions and 6 deletions

View File

@@ -1969,9 +1969,38 @@ describe('API', function()
end
end
command 'au FileType lua setlocal commentstring=NEW\\ %s'
command 'au FileType lua ++once setlocal commentstring=NEW\\ %s'
eq('NEW %s', api.nvim_get_option_value('commentstring', { filetype = 'lua' }))
-- Works from within a FileType autocommand fired from setting the &filetype.
exec [[
au FileType * ++once let g:value = nvim_get_option_value('commentstring', #{filetype: 'vim'})
set commentstring= ft=lua
]]
eq('"%s', eval('g:value'))
-- Check it didn't somehow mess up the &commentstring from setting the &filetype.
eq('-- %s', eval('&commentstring'))
-- Not possible to recurse endlessly, of course.
exec [[
au FileType foobar call nvim_get_option_value('commentstring', #{filetype: 'foobar'})
]]
matches( -- Watch out - this error is large!
[[E5555: API call: Vim:E218: Autocommand nesting too deep$]],
pcall_err(command, 'set ft=foobar')
)
command('au! FileType foobar')
eq(
[[Vim(call):E5555: API call: Could not execute FileType autocommands]],
pcall_err(command, "noautocmd call nvim_get_option_value('tagfunc', #{filetype: 'man'})")
)
-- No error if executed with no FileType autocommands defined.
-- Returning the copied global value will continue to suffice, I guess.
command([[filetype plugin off | setglobal commentstring=<><\ %s\ ><>]])
eq({}, api.nvim_get_autocmds { event = 'FileType' })
eq('<>< %s ><>', api.nvim_get_option_value('commentstring', { filetype = 'lua' }))
end)
it('errors for bad FileType autocmds', function()