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: Autocommand (defined in autocommand) not executed #1184

Open
3 tasks done
scajanus opened this issue Nov 14, 2023 · 4 comments
Open
3 tasks done

bug: Autocommand (defined in autocommand) not executed #1184

scajanus opened this issue Nov 14, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@scajanus
Copy link

Did you check docs and existing issues?

  • I have read all the lazy.nvim docs
  • I have searched the existing issues of lazy.nvim
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

NVIM v0.9.4

Operating system/version

MacOS Ventura 13.6

Describe the bug

Autocommand like:

autocmd BufRead * echom "hello" | au FileType <buffer> ++once echom "there"

does not trigger the FileType autocmd when opening Neovim.

The actual autocmd I was trying to use was the one from :h restore-cursor:

    autocmd BufRead * autocmd FileType <buffer> ++once
      \ if &ft !~# 'commit\|rebase' && line("'\"") > 1 && line("'\"") <= line("$") | exe 'normal! g`"' | endif

Steps To Reproduce

  1. Start Neovim with nvim -u repro.lua repro.lua

Expected Behavior

Both autocommands get executed.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
vim.cmd([[autocmd BufRead * echom "hello" | au FileType <buffer> ++once echom "there" ]])
@scajanus scajanus added the bug Something isn't working label Nov 14, 2023
@scajanus
Copy link
Author

Related: #858

@ericvw
Copy link

ericvw commented Dec 24, 2023

I can also confirm the issue exists. I ran into this when attempting to port to lazy from packer.

@ericvw
Copy link

ericvw commented Mar 11, 2024

I figured out why the snippet in :h restore-cursor doesn't work with lazy, in some cases 😉.

What

    autocmd BufRead * autocmd FileType <buffer> ++once
      \ if &ft !~# 'commit\|rebase' && line("'\"") > 1 && line("'\"") <= line("$") | exe 'normal! g`"' | endif

does is create a one-time executed autocommand when the FileType event triggers.

However, lazy is invoking preemptively

M.source(vim.env.VIMRUNTIME .. "/filetype.lua")
when loaded (initially added in ffcd0ab). Thus, if the restore cursor autocommand is defined after lazy is loaded, the FileType event has already been triggered.

I'm unsure if lazy should load Neovim's filetype.lua runtime preemptively. I understand it was to address #35; however, it seems strange for lazy to compensate for an issue that is rooted with Neovim and its handling of filetype. While still entirely up to the maintainers of lazy, reverting this change and letting Neovim and the plugin authors address how to approach this seems like a good idea. The generic workaround for end users is to invoke the equivalent of :filetype plugin indent on at the top of their configuration to ensure that filetype is set before any other configuration is defined — a recommendation by the Neovim maintainers I've seen across a couple of issues.

For those who are impacted by this, you can update the :h restore-cursor by removing autocmd FileType <buffer> ++once to trigger the Vimscript on BufRead because ft will be set due to lazy invoking filetype.lua. Alternatively, reordering when lazy is loaded and the autocommand is defined also works.

For the history of why the restore cursor snippet has diverged from vim, see neovim/neovim#15536 (comment).

ericvw added a commit to ericvw/dotfiles that referenced this issue Mar 11, 2024
Plugin managers may call the equivalent of `:runtime! filetype.lua`,
which emits the FileType event. If a specified autocommand on
FileType with `++once` is defined after filetype detection is enabled,
then the autocommand won't be executed.

Moving plugin loading and configuration to the end of `init.lua` aligns
more closely to the initialization order specified in
`:h initializaiton`.

I ran into folke/lazy.nvim#1184 when
attempting to port plugin management to `lazy`.
@scajanus
Copy link
Author

Thanks for looking into it! My workaround for this specific case was to keep using BufReadPost without ++once, which I think was the documented version in Vim: https://vimhelp.org/usr_05.txt.html#restore-cursor.

This means the autocmd is firing more often than necessary, but I can live with that for now as it doesn't break anything or prevent this from working. I imagine there are autocommands where this would be an issue, and of course it would be nice if the documented one for Neovim would work :)

ericvw added a commit to ericvw/dotfiles that referenced this issue Mar 12, 2024
Plugin managers may call the equivalent of `:runtime! filetype.lua`,
which emits the FileType event. If a specified autocommand on
FileType with `++once` is defined after filetype detection is enabled,
then the autocommand won't be executed.

Moving plugin loading and configuration to the end of `init.lua` aligns
more closely to the initialization order specified in
`:h initializaiton`.

I ran into folke/lazy.nvim#1184 when
attempting to port plugin management to `lazy`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants