8000 bug: nvim_exec_autocmds re-fire missing data and buffer validation after lazy-load · Issue #9 · zuqini/zpack.nvim · GitHub
[go: up one dir, main page]

Skip to content

bug: nvim_exec_autocmds re-fire missing data and buffer validation after lazy-load #9

Closed

Description

@MomePP

Description

After commit 6b351e5 ("fix: lazy-loaded plugins not fully activating"), event.lua re-fires the original autocmd via nvim_exec_autocmds once the plugin is loaded. However, the re-fired event does not forward the original ev.data and does not check whether the buffer is still valid.

This causes two classes of errors:

  1. attempt to index field 'data' (a nil value) — Any autocmd callback that reads event.data (e.g., LspAttach expects event.data.client_id) crashes because data is not forwarded.
  2. Invalid buffer id: N — If the buffer that triggered the original event has been wiped before the re-fire, nvim_exec_autocmds errors on the stale buffer id.

Current code (event.lua:84-88)

if #other_events > 0 then
  util.autocmd(other_events, function(ev)
    loader.process_spec(pack_spec)
    vim.api.nvim_exec_autocmds(ev.event, { buffer = ev.buf, modeline = false })
  end, { group = state.lazy_group, once = true, pattern = normalized_event.pattern })
end

Suggested fix

if #other_events > 0 then
  util.autocmd(other_events, function(ev)
    loader.process_spec(pack_spec)
    if vim.api.nvim_buf_is_valid(ev.buf) then
      vim.api.nvim_exec_autocmds(ev.event, { buffer = ev.buf, data = ev.data, modeline = false })
    end
  end, { group = state.lazy_group, once = true, pattern = normalized_event.pattern })
end

Two changes:

  • Forward data = ev.data so downstream callbacks receive the original event data.
  • Guard with nvim_buf_is_valid(ev.buf) to skip re-fire for wiped buffers.

Reproduction

Minimal init.lua:

vim.pack.add({ 'https://github.com/zuqini/zpack.nvim' })

require('zpack').setup({
  -- Any plugin that lazy-loads on LspAttach
  {
    'https://github.com/someone/example-plugin',
    event = 'LspAttach',
  },
})

-- Callback that depends on event.data
vim.api.nvim_create_autocmd('LspAttach', {
  callback = function(event)
    -- This crashes: event.data is nil when re-fired by zpack
    local client_id = event.data.client_id
    print('attached client:', client_id)
  end,
})

Steps:

  1. Open a file with an LSP server configured (e.g., nvim test.lua).
  2. Wait for the LSP to attach — this triggers the lazy-load of the plugin.
  3. zpack loads the plugin, then re-fires LspAttach without data.

Errors observed:

Error  Lua callback: .../lsp-config.lua:129: attempt to index field 'data' (a nil value)
Error  .../zpack/lazy_trigger/event.lua:87: Invalid buffer id: 1

Environment

Activity

  1. added 2 commits that reference this issue on Apr 9, 2026
    1cc0a2f
    2351956
  2. zuqini commented on Apr 9, 2026

    @zuqini
    Owner

    Thank you for reporting the issue. #10 should address this along with some other edge-case behaviors with event-triggers. Please let me know if you're still seeing issues

  3. MomePP commented on Apr 9, 2026

    @MomePP
    Author

    Thanks for the quick fix, I've tested, no longer get any errors on startup. 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0