Skip to content

[Bug]: LSP explanations do not include the buffer #2639

@mhedgpeth

Description

@mhedgpeth

Pre-submission checklist

  • I have read the documentation
  • I have updated the plugin and all dependencies to the latest versions
  • I have searched for existing issues and discussions
  • My issue is not a minor or cosmetic quirk (e.g., formatting, spacing, or other non-functional details)

Neovim version (nvim -v)

0.11.5

Operating system/version

macOS 26.2

Adapter and model

anthropic

Describe the bug

When using the "Explain LSP Diagnostics" action from the action palette, the chat receives diagnostic metadata (line number, buffer ID, severity, message) but not the actual source code where the diagnostics occur.

This makes the AI response unhelpful since it can't see what code is causing the issue.

Steps to reproduce

Steps to Reproduce:

  1. Open a file with LSP diagnostics (errors/warnings)
  2. aa → "Explain LSP Diagnostics"
  3. Observe the chat context

I used a file called test_lsp.lua and had the Lua LSP installed locally:

-- Test file for reproducing LSP diagnostics issue
-- This file has intentional errors for lua_ls to catch

-- ERROR 1: Undefined global variable
local x = undefined_variable

-- ERROR 2: Wrong type assignment
local num = "this should be a number"
local result = num + 5

-- ERROR 3: Missing parameter
local function greet(name)
	return "Hello, " .. name
end
greet() -- missing argument

-- ERROR 4: Unused variable
local unused = "this variable is never used"

-- To test:
-- 1. Open this file: nvim --clean -u minimal.lua test_lsp.lua
-- 2. Wait for lua_ls to load (check :LspInfo)
-- 3. Press <leader>aa to open CodeCompanion Actions
-- 4. Select "Explain LSP Diagnostics"
-- 5. Check if the chat shows the ACTUAL CODE or just diagnostic metadata

I then ran: nvim --clean -u minimal.lua test_lsp.lua, highlighted the error, hit leader a, a, enter and observed the settings

Expected behavior

Chat should include both the diagnostic info AND the relevant source code lines.

Actual:
Only diagnostic metadata is included. Example:
Line: 5, Buffer: 1, Severity: Error, Message: "undefined variable"
Missing: the actual code on line 5.

Screenshots or recordings (optional)

No response

minimal.lua file

---@diagnostic disable: missing-fields
-- Run with: nvim --clean -u ~/.config/nvim-debug/minimal.lua
-- Or from this directory: nvim --clean -u minimal.lua

-- ============================================================================
-- BASIC VIM SETTINGS (make it usable)
-- ============================================================================
vim.g.mapleader = " "
vim.g.maplocalleader = " "
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.signcolumn = "yes"
vim.opt.termguicolors = true

-- Copilot token path
vim.env["CODECOMPANION_TOKEN_PATH"] = vim.fn.expand("~/.config")

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

-- ============================================================================
-- PLUGINS
-- ============================================================================
local plugins = {
	-- LSP for testing diagnostics (lua_ls)
	{
		"neovim/nvim-lspconfig",
		config = function()
			require("lspconfig").lua_ls.setup({
				settings = {
					Lua = {
						diagnostics = {
							globals = { "vim" },
						},
					},
				},
			})
		end,
	},

	-- CodeCompanion
	{
		"olimorris/codecompanion.nvim",
		version = "^18.0.0",
		dependencies = {
			{ "nvim-lua/plenary.nvim" },
			{
				"nvim-treesitter/nvim-treesitter",
				lazy = false,
				build = ":TSUpdate",
			},
			{
				"saghen/blink.cmp",
				lazy = false,
				version = "*",
				opts = {
					keymap = {
						preset = "enter",
						["<S-Tab>"] = { "select_prev", "fallback" },
						["<Tab>"] = { "select_next", "fallback" },
					},
					cmdline = { sources = { "cmdline" } },
					sources = {
						default = { "lsp", "path", "buffer", "codecompanion" },
					},
				},
			},
		},
		opts = {
			strategies = {
				chat = { adapter = "anthropic" },
				inline = { adapter = "anthropic" },
			},
			opts = {
				log_level = "DEBUG",
			},
		},
		keys = {
			{ "<leader>aa", "<cmd>CodeCompanionActions<cr>", mode = { "n", "v" }, desc = "Actions" },
			{ "<leader>ac", "<cmd>CodeCompanionChat Toggle<cr>", mode = { "n", "v" }, desc = "Chat" },
			{ "<leader>ai", "<cmd>CodeCompanion<cr>", mode = { "n", "v" }, desc = "Inline" },
		},
	},
}

require("lazy.minit").repro({ spec = plugins })

-- ============================================================================
-- SETUP TREE-SITTER PARSERS (runs after plugins load)
-- ============================================================================
vim.api.nvim_create_autocmd("User", {
	pattern = "VeryLazy",
	callback = function()
		local ok, ts = pcall(require, "nvim-treesitter")
		if ok then
			ts.install({ "lua", "markdown", "markdown_inline" }, { summary = true }):wait(60000)
		end
	end,
})

Log output (optional)

No response

Minimal reproduction confirmation

  • Yes, I have tested and provided a minimal.lua file that reproduces the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreviewed-by-AIThe CodeCompanion agent reviewed this PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions