Skip to content

Conversation

@larrybotha
Copy link
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()
		return vim.fn.executable("vectorcode") == 1
	end,
	event = { "BufReadPost", "BufNewFile" },
	config = function()
		require("vectorcode").setup(
			---@type VectorCode.Opts
			{
				cli_cmds = {
					vectorcode = "vectorcode",
				},
				-- options used when attaching to LSP, below
				---@type VectorCode.RegisterOpts
				async_opts = {
					debounce = 100, -- Debounce time in milliseconds to avoid excessive updates
					events = { "BufWritePost", "InsertLeave", "BufReadPost" }, -- Events that trigger automatic file indexing
					exclude_this = true, -- Exclude current buffer from automatic indexing
					n_query = 1, -- Number of queries to send for each indexing operation
					notify = true, -- Show notifications when indexing operations occur
					query_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 indexing
					run_on_register = false,
				},
				async_backend = "lsp", -- Backend to use for async operations (default or lsp)
				exclude_this = true, -- Exclude current buffer from operations
				n_query = 10, -- Number of queries to send for each operation
				notify = true, -- Show notifications for operations
				timeout_ms = 5000, -- Timeout in milliseconds for operations
				on_setup = {
					update = true, -- Update VectorCode on setup
					lsp = 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()
				local cacher = require("vectorcode.cacher")
				local bufnr = vim.api.nvim_get_current_buf()

				cacher.utils.async_check("config", function()
					cacher.lsp.register_buffer(bufnr, {
						-- provide rich context for LSP queries
						n_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

@larrybotha larrybotha changed the title Detach buffers only if the are attached Detach buffers only if they are attached Dec 16, 2025
@codecov
Copy link

codecov bot commented Dec 16, 2025

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.

@larrybotha larrybotha changed the title Detach buffers only if they are attached Neovim LSP integration: Detach buffers only if they are attached Dec 16, 2025
@larrybotha larrybotha changed the title Neovim LSP integration: Detach buffers only if they are attached Neovim LSP integration: Detach buffers only when attached Dec 16, 2025
@Davidyz Davidyz merged commit 3eacce5 into Davidyz:main Dec 26, 2025
15 checks passed
@Davidyz
Copy link
Owner

Davidyz commented Dec 26, 2025

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
Contributor Author

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

Sign up for free to 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