Closed dhruvmanila closed 1 year ago
Hmm, that's strange. I would expect fix
to always be present (except for very old Ruff versions) because serde should serialize None
if no fix is present:
Ok, so I found the problem.
tldr, It's got to do with how lua
interacts with nil
and how Neovim's LSP client decodes the JSON response from the server.
So, in lua
there's only one data structure called a table
which is used to represent everything (arrays, hashmaps, classes, etc.). Now, you can index into a table through []
or using attribute access. If the key doesn't exists, it'll return nil
and not raise any error.
Here, Neovim decodes the body with a specific parameter which says to convert JSON null
to lua nil
, basically eliminating the entry as they're the same in lua:
vim.json.decode('{"data": {"fix": null, "noqa_row": 8}}')
{
data = {
-- `vim.NIL` is a special object used to handle the very bug we're facing
fix = vim.NIL,
noqa_row = 8
}
}
vim.json.decode('{"data": {"fix": null, "noqa_row": 8}}', {luanil = {object = true}})
{
data = {
noqa_row = 8
}
}
So, now when the Neovim client sends the code action request, the diagnostic data doesn't contain the fix
key.
I think we should handle such cases appropriately.
So, I was testing this (https://github.com/charliermarsh/ruff/issues/4746) out and I've Neovim configured to show a lightbulb symbol if there are any code actions available. This is done by sending a
textDocument/codeAction
request wherever the cursor is present.Request:
Response:
Extracting the traceback:
This is happening because the diagnostic doesn't contain a fix but the server expects the
fix
key to be present (line 506):https://github.com/astral-sh/ruff-lsp/blob/8dca712c84fee384a477271127ab33b7da955877/ruff_lsp/server.py#L503-L507