neovim / neovim

Vim-fork focused on extensibility and usability
https://neovim.io
Other
82.2k stars 5.62k forks source link

LSP: "response id must be a number" error, but spec allows string #30638

Open nickjer opened 11 hours ago

nickjer commented 11 hours ago

Problem

After upgrading steep to 1.8.0 I am consistently seeing:

Error executing luv callback:                                                                                                                                                                             
...nt_nvimwcukvL/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:462: response id must be a number                                                                                                             
stack traceback:                                                                                                                                                                                          
        [C]: in function 'assert'                                                                                                                                                                         
        ...nt_nvimwcukvL/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:462: in function 'handle_body'                                                                                                        
        ...nt_nvimwcukvL/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:765: in function 'handle_body'                                                                                                        
        ...nt_nvimwcukvL/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:267: in function <...nt_nvimwcukvL/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:251>

I opened up an issue in the steep repo here: https://github.com/soutaro/steep/issues/1246

The author mentions that the spec does allow Strings for response id's:

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage

Any guidance on possibly tracking down this issue would help.

Steps to reproduce using "nvim -u minimal_init.lua"

Take everything below with a grain of salt as I am only replacing the top few variables. I am unfamiliar with the rest.

--- CHANGE THESE
local pattern = 'ruby'
local cmd = {'steep', 'langserver'}
-- Add files/folders here that indicate the root of a project
local root_markers = {'.git', 'Steepfile'}
-- Change to table with settings if required
local settings = vim.empty_dict()

vim.api.nvim_create_autocmd('FileType', {
  pattern = pattern,
  callback = function(args)
    local match = vim.fs.find(root_markers, { path = args.file, upward = true })[1]
    local root_dir = match and vim.fn.fnamemodify(match, ':p:h') or nil
    vim.lsp.start({
      name = 'bugged-ls',
      cmd = cmd,
      root_dir = root_dir,
      settings = settings
    })
  end
})

Expected behavior

Not raise the error.

Nvim version (nvim -v)

v0.10.2

Language server name/version

steep 1.8.0

Operating system/version

Pop!_OS 22.04 LTS

Log file

https://gist.github.com/nickjer/4b64c1e11d423cfa941321a251200944

justinmk commented 11 hours ago

The error comes from here: https://github.com/neovim/neovim/blob/d3b4772ddcd4c890dc5bc38e1f1b36a08aed0c04/runtime/lua/vim/lsp/rpc.lua#L461-L462

If tonumber() is failing, that suggests we got something different than we sent. tonumber('42') returns 42 , so that suggests that "steep" is not returning the response id that we sent (which would be a bug in "steep").

As a UX improvement, we could at least print the invalid result in the assert.

nickjer commented 11 hours ago

Interesting, I'll follow up with the author on this. In the meantime is there any way to find this errant response in the logs? Or does it crash before the log message is generated?

justinmk commented 10 hours ago

In the meantime is there any way to find this errant response in the logs?

one way is to just change that line to:

local result_id = assert(tonumber(decoded.id), 'response id must be a number: ' .. vim.inspect(decoded))

then restart Nvim. what does it print?

nickjer commented 10 hours ago

Oh nice...

Error executing luv callback:                                                                                                                                                                             
...squashfs-root/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:461: response id must be a number: {                                                                                                          
  id = "cabb3a89-ead8-4aa1-bc03-82873562c130",                                                                                                                                                            
  jsonrpc = "2.0",                                                                                                                                                                                        
  result = {                                                                                                                                                                                              
    completed = true,                                                                                                                                                                                     
    duration = 0,                                                                                                                                                                                         
    finished_at = "2024-10-03T08:13:24-04:00",                                                                                                                                                            
    guid = "cabb3a89-ead8-4aa1-bc03-82873562c130",                                                                                                                                                        
    started_at = "2024-10-03T08:13:23-04:00"                                                                                                                                                              
  }                                                                                                                                                                                                       
}                                                                                                                                                                                                         
stack traceback:                                                                                                                                                                                          
        [C]: in function 'assert'                                                                                                                                                                         
        ...squashfs-root/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:461: in function 'handle_body'                                                                                                        
        ...squashfs-root/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:762: in function 'handle_body'                                                                                                        
        ...squashfs-root/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:267: in function <...squashfs-root/usr/share/nvim/runtime/lua/vim/lsp/rpc.lua:251> 

I think I saw something similar in the logs that I attached to this issue. Where it looks like the token is used as the id in this one response that returns completed = true.

But one thing I unsure of is what was the original id sent to the LSP that would trigger this mismatch. I only ask because all the messages before this have no id in the log.