Skip to content

bug: TermRequest autocmd crashes with 'Invalid window' on nvim 0.12 #235

@sunnyagain

Description

@sunnyagain

Did you check docs and existing issues?

  • I am using the latest plugin version
  • I read the plugin docs
  • I read the opencode docs
  • I searched the existing plugin issues
  • I searched the existing opencode issues

:checkhealth opencode output

opencode.nvim ~
- OS: Darwin 25.4.0 (x86_64)
- `nvim` version: `0.12.1`.
- `opencode.nvim` git commit hash: `b3cdd746a5ca321b6884badefa0ba22c86c3d45e`.
- `vim.g.opencode_opts`: `nil`

opencode.nvim [binaries] ~
- ✅ OK `opencode` available with version `1.4.0`.
- ✅ OK `curl` available.
- ✅ OK `pgrep` available.
- ✅ OK `lsof` available.

opencode.nvim [snacks] ~
- ⚠️ WARNING `snacks.input` is disabled: `ask()` will not be enhanced.
- ✅ OK `snacks.picker` is enabled: `select()` will be enhanced.

Describe the bug

nvim_set_current_win(winid) at terminal.lua:66 inside the TermRequest autocmd callback errors with Invalid 'window' when winid becomes invalid before the callback fires.

Error in TermRequest Autocommands for "<buffer=5>":
Lua callback: ...te/pack/core/opt/opencode.nvim/lua/opencode/terminal.lua:66: Invalid 'window': Expected Lua number
stack traceback:
        [C]: in function 'nvim_set_current_win'
        ...te/pack/core/opt/opencode.nvim/lua/opencode/terminal.lua:66: in function <...te/pack/core/opt/opencode.nvim/lua/opencode/terminal.lua:63>

The callback at line 63-70 doesn't guard against winid being invalid:

auid = vim.api.nvim_create_autocmd("TermRequest", {
    buffer = bufnr,
    callback = function(ev)
      if ev.data.cursor[1] > 1 then
        vim.api.nvim_del_autocmd(auid)
        vim.api.nvim_set_current_win(winid)  -- winid can be invalid here
        vim.cmd([[startinsert | call feedkeys("\<C-\>\<C-n>\<C-w>p", "n")]])
      end
    end,
  })

Adding if vim.api.nvim_win_is_valid(winid) then before line 66 would fix it.

Steps To Reproduce

  1. Open nvim (0.12.1)
  2. Toggle opencode terminal via require("opencode").toggle()
  3. Error fires on initial terminal render

Expected Behavior

Terminal opens without error. The TermRequest callback should validate winid before calling nvim_set_current_win.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions