fix(file_refresh): guard against invalid buffer in TermClose autocmd
Problem
When exiting Neovim (or closing a terminal buffer), a TermClose autocmd in
file_refresh.lua calls nvim_buf_get_name(args.buf) at line 104. By the
time the event fires the buffer may already have been deleted, causing:
Error in TermClose Autocommands for "*":
Lua callback: ...claude-code.nvim/lua/claude-code/file_refresh.lua:104: Invalid buffer id: 2
stack traceback:
[C]: in function 'nvim_buf_get_name'
...claude-code.nvim/lua/claude-code/file_refresh.lua:104: in function <...>
Fix
Add a nvim_buf_is_valid guard before the name lookup so the callback exits
early when the buffer is no longer valid:
if not vim.api.nvim_buf_is_valid(args.buf) then return end
This is the same defensive pattern applied to terminal.lua in #106 /
commit 1952ba2.
Testing
Reproduced by opening Claude Code inside Neovim, closing the terminal split, then quitting Neovim β the error no longer appears after this change.
Summary by CodeRabbit
- Bug Fixes
- Fixed terminal buffer cleanup logic to properly validate buffer state before performing cleanup operations, preventing potential errors when closing terminals.
No actionable comments were generated in the recent review. π
βΉοΈ Recent review info
βοΈ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a1ea3ac8-cd8c-45c1-bec5-22321ffd58dc
π₯ Commits
Reviewing files that changed from the base of the PR and between 55c0cb59828fbc3bec744288286a46f5d5750b83 and 497f4302e9c500ada734923776d7b95d2e915009.
π Files selected for processing (1)
lua/claude-code/file_refresh.lua
π Walkthrough
Walkthrough
A single guard line is added to the TermClose autocommand callback in file_refresh.lua. Before executing any cleanup logic, the callback now calls vim.api.nvim_buf_is_valid(args.buf) and returns early if the buffer is no longer valid.
Changes
TermClose Buffer Validity Guard
| Layer / File(s) | Summary |
|---|---|
Early return for invalid buffer in TermClose lua/claude-code/file_refresh.lua |
TermClose callback now calls vim.api.nvim_buf_is_valid(args.buf) and returns early, preventing updatetime restore, instance cleanup, and window close/delete from running on an invalid buffer. |
Estimated code review effort
π― 1 (Trivial) | β±οΈ ~2 minutes
Possibly related issues
- greggh/claude-code.nvim#124: This PR directly implements the
vim.api.nvim_buf_is_valid(args.buf)check identified as the fix for the "Invalid buffer id" crash occurring when closing terminals with Ctrl+D.
Possibly related PRs
- greggh/claude-code.nvim#106: Refactored the same
TermCloseautocommand infile_refresh.luato useargs.buffor cleanup; this PR adds a validity guard on top of that same code path.
Poem
π A buffer may vanish, gone without a trace, But now we check first before we give chase.
nvim_buf_is_validβ a tiny little test, Keeps crashes at bay and lets Neovim rest. One line of caution, a guard standing tall, No invalid buffer shall cause us to fall! πΏ
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title accurately describes the main change: adding a buffer validity check to guard against invalid buffers in the TermClose autocmd. |
| Docstring Coverage | β Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
| Linked Issues check | β Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | β Passed | Check skipped because no linked issues were found for this pull request. |
βοΈ Tip: You can configure your own custom pre-merge checks in the settings.
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.