Skip to content

[BUG] LSP module onAttach #3319

Closed
Closed
@monr3d

Description

@monr3d
Field Description
Module lsp
Nixpkgs unstable
Home Manager not used

I'm using a standalone flake

  • I have read the FAQ and my bug is not listed there.

Description

In the lsp module we have this:

autoCmd = [
{
event = "LspAttach";
group = "nixvim_lsp_on_attach";
callback = lib.nixvim.mkRaw ''
function(args)
do
-- client and bufnr are supplied to the builtin `on_attach` callback,
-- so make them available in scope for our global `onAttach` impl
local client = vim.lsp.get_client_by_id(args.data.client_id)
local bufnr = args.bufnr
${cfg.onAttach}
end
end
'';
desc = "Run LSP onAttach";
}
];
};

If I understand correctly, when using lsp.onAttach, we are allowed to refer to client and bufnr in the Lua code. However, I believe the assignment:

local bufnr = args.bufnr

is incorrect and should instead be:

local bufnr = args.buf

Reasoning

While testing a code that relied on bufnr, I encountered an error (sorry, I don't have the exact log anymore), which disappeared once I changed the code to use args.buf directly. The minimal reproducible example below reproduces the same error.
if you try using the minimal example below, you I get this error message:

Error Message

When using the current implementation:

Error detected while processing LspAttach Autocommands for "*":                                         
Error executing lua callback: /nix/store/mf79j9pggfgjacsfynplb1ls5lvvsg92-init.lua:690: attempt to concatenate local 'bufnr' (a nil value)

If I remove the bufnr usage and print args.buf instead, the output is correct:

args.buf number is: 2

This confirms that args.buf is correctly populated while bufnr is nil.

Unless I’m misunderstanding something (which is very possible), this seems like a small bug in the module.

Apologies if this report is missing anything or doesn't follow conventions. Reporting issues in other people's code is new to me. I'm happy to provide the original code I used if needed, but the minimal example below should be sufficient to reproduce the problem.

Minimal, Reproducible Example (MRE)

programs.nixvim = {
  lsp.onAttach = ''
    print("Buffer number is: " .. bufnr)
    print("args.buf number is: " .. args.buf)
  '';
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions