Skip to content

Neovim LSP integration: Detach buffers only when attached - #303

Merged
Davidyz merged 1 commit into
Davidyz:mainfrom
larrybotha:fix/nvim-cacher-lsp-detaching-unattached-buffers
Dec 26, 2025
Merged

Neovim LSP integration: Detach buffers only when attached#303
Davidyz merged 1 commit into
Davidyz:mainfrom
larrybotha:fix/nvim-cacher-lsp-detaching-unattached-buffers

Conversation

@larrybotha

Copy link
Copy Markdown
Contributor

When switching to files that are not yet buffered I get the following error:

Buffer (id: [X]) is not attached to client (id: [Y]). Cannot detach.
"/path/to/file" 15L, 351B

This appears to be as a result of a race condition when detaching buffers - something in my config is potentially detaching buffers and then vectorcode attempts to do the same after they are already detached.

I tried to create a minimal neovim repro, but with the extent of my config it'd be a nightmare to minimise, so I've provided a screencast instead:

Recording.at.2025-12-16.22.53.40.Cropped.in.2025-12-16.22.54.37._compressed.webm

Reproduction Steps

  • add a decade's worth of vim configs to neovim
  • configure vectorcode lsp integration in neovim
  • open an unbuffered file
  • open a new unbuffered file in the same buffer

Expect: no error
Actual: error raised

Neovim Vectorcode Config

return {
"Davidyz/VectorCode",
version="*",
build="uv tool upgrade vectorcode",
dependencies= { "nvim-lua/plenary.nvim" },
cond=function()
returnvim.fn.executable("vectorcode") ==1end,
event= { "BufReadPost", "BufNewFile" },
config=function()
require("vectorcode").setup(
---@typeVectorCode.Opts
{
cli_cmds= {
vectorcode="vectorcode",
},
-- options used when attaching to LSP, below---@typeVectorCode.RegisterOptsasync_opts= {
debounce=100, -- Debounce time in milliseconds to avoid excessive updatesevents= { "BufWritePost", "InsertLeave", "BufReadPost" }, -- Events that trigger automatic file indexingexclude_this=true, -- Exclude current buffer from automatic indexingn_query=1, -- Number of queries to send for each indexing operationnotify=true, -- Show notifications when indexing operations occurquery_cb=require("vectorcode.utils").make_surrounding_lines_cb(-1), -- Use surrounding lines as context for better indexing-- Don't automatically index when files are manually registered-- Use `:VectorCode register` to register specific files for indexingrun_on_register=false,
},
async_backend="lsp", -- Backend to use for async operations (default or lsp)exclude_this=true, -- Exclude current buffer from operationsn_query=10, -- Number of queries to send for each operationnotify=true, -- Show notifications for operationstimeout_ms=5000, -- Timeout in milliseconds for operationson_setup= {
update=true, -- Update VectorCode on setuplsp=false, -- Disable automatic LSP startup during setup
},
sync_log_env_var=false, -- Don't sync environment variables for logging
}
)
vim.api.nvim_create_autocmd("LspAttach", {
callback=function()
localcacher=require("vectorcode.cacher")
localbufnr=vim.api.nvim_get_current_buf()
cacher.utils.async_check("config", function()
cacher.lsp.register_buffer(bufnr, {
-- provide rich context for LSP queriesn_query=10,
})
end, nil)
end,
desc="Register buffer for VectorCode",
})
end,
}

Solution

Before calling vim.lsp.buf_detach_client, check that the buffer is attached using vim.lsp.buf_is_attached

@larrybothalarrybotha changed the title Detach buffers only if the are attachedDetach buffers only if they are attachedDec 16, 2025
@codecov

codecovBot commented Dec 16, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.72%. Comparing base (56f21eb) to head (7e20d6a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #303 +/- ##
=======================================
Coverage 99.72% 99.72% =======================================
Files 25 25 Lines 1845 1845 =======================================
Hits 1840 1840 Misses 5 5 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@larrybothalarrybotha changed the title Detach buffers only if they are attachedNeovim LSP integration: Detach buffers only if they are attachedDec 16, 2025
@larrybothalarrybotha changed the title Neovim LSP integration: Detach buffers only if they are attachedNeovim LSP integration: Detach buffers only when attachedDec 16, 2025
@Davidyz
Davidyz merged commit 3eacce5 into Davidyz:mainDec 26, 2025
15 checks passed
@Davidyz

Copy link
Copy Markdown
Owner

Thanks for the contribution, and sorry for taking so long to process this. I was sick and couldn't take care of my Github.

@larrybotha

Copy link
Copy Markdown
ContributorAuthor

no worries - thanks for the hard work, much appreciated!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@larrybotha@Davidyz