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: plugins lazy loaded with autocmd events don't always execute their events when loaded #858

Closed
3 tasks done
fent opened this issue Jun 6, 2023 · 3 comments · Fixed by #859
Closed
3 tasks done
Labels
bug Something isn't working

Comments

@fent
Copy link

fent commented Jun 6, 2023

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)

0.9.0

Operating system/version

MacOS 13.4

Describe the bug

When a plugin is lazy loaded using an autocmd event and also has the same type but broader event on plugin startup, the event does not run. Some plugins will not work properly when this happens since their startup code is not properly set up. An example is a plugin that is loaded by event = "BufReadPost *.md" but also has autocmd BufReadPost * on their startup code

I think this happens because in this line pattern = pattern is used to filter for any new autocmds that have been created from loading the plugin, but filtering by the specific pattern will ignore the more broader patterns

Steps To Reproduce

  1. have a plugin lazyload via event, ie: event = "BufReadPost *.md"
  2. add on autocmd on plugin config for same event but broader, ie:
vim.api.nvim.create_autocmd("BufReadPost", {
  pattern = "*", -- this pattern should match the pattern by which this plugin is loaded with
})
  1. observe that the autocmd does not run when the plugin loads

Expected Behavior

The plugin is expected run matching autocmds when it loads, but does not

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
  {
    "tomasiser/vim-code-dark", -- plugin doesn't matter, only used for the example
    event = "BufReadPost *.md",
    config = function()
      print("plugin 3 config")
      vim.api.nvim_create_autocmd("BufReadPost", {
        pattern = "*.md",
        callback = function()
          print("plugin *.md autocmd!") -- expect this to print (does print)
        end,
        group = vim.api.nvim_create_augroup("mygroup3.1", {}),
      })
      vim.api.nvim_create_autocmd("BufReadPost", {
        pattern = "*",
        callback = function()
          print("plugin * autocmd!") -- expect this to print (does NOT print)
          print(debug.traceback())
        end,
        group = vim.api.nvim_create_augroup("mygroup3.2", {}),
      })
      vim.api.nvim_create_autocmd("BufReadPost", {
        pattern = "*.ts",
        callback = function()
          print("plugin *.ts autocmd!") -- expect this to not print
        end,
        group = vim.api.nvim_create_augroup("mygroup3.3", {}),
      })
    end
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
@fent fent added the bug Something isn't working label Jun 6, 2023
@folke folke closed this as completed in bc89502 Jun 6, 2023
@folke
Copy link
Owner

folke commented Jun 6, 2023

You're right. It doesn't really make sense to only check autocmds with the same pattern. I just pushed an update that removes all the pattern handling in the event handler.

folke added a commit that referenced this issue Jun 6, 2023
@folke
Copy link
Owner

folke commented Jun 6, 2023

I had to revert that change since the patterns are needed for at least FileType autocmds.

Need to look into it a bit more for a proper fix.

@folke folke reopened this Jun 6, 2023
@folke folke closed this as completed in #859 Jun 9, 2023
@fent
Copy link
Author

fent commented Jul 15, 2023

Hi, this was closed by mistake by the above release, but the commit was reverted

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

Successfully merging a pull request may close this issue.

2 participants