Skip to content

vim.lsp.buf.formatting_sync() fails with attempt to index a nil value #13232

@tyrion

Description

@tyrion

Executing :lua vim.lsp.buf.formatting_sync() sometimes fails with attempt to index a nil value
at

result = result[1].result

I am not a lua expert and I do not know how to reproduce this with nvim -u NORC. However, I tried to debug this by making a small lua file test.lua and then executing it in the current buffer with :luafile. This shows that sometimes the result variable is inside the 2 key, instead of 1.

-- dump copied from https://stackoverflow.com/a/27028488/641317
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

local params = require'vim.lsp.util'.make_formatting_params()
local result = vim.lsp.buf_request_sync(0, "textDocument/formatting", params)

print(dump(result))
-- shows: { [2] = { ["result"] = { } ,} ,}

This seems to happen only when I open two different files that uses two different language server. Then the problem happens on the second one.
Not sure what this index here is meant to represent. My guess is that it seems the index of the language server used.

  • nvim --version:

    NVIM v0.5.0-dev
    Build type: Release
    LuaJIT 2.1.0-beta3
    Compilation: 
    Compiled by nixbld
    Features: +acl +iconv +tui
    

    git rev: a061d53

  • language server name/version: typescript-language-server v0.4.0

  • Operating system/version: nixos

nvim -c ":checkhealth nvim nvim_lsp"

health#nvim#check

Configuration

  • WARNING: Missing user config file: /home/tyrion/.config/nvim/init.vim
    • ADVICE:
      • :help |init.vim|

Performance

  • OK: Build type: Release

Remote Plugins

  • OK: Up to date

terminal

  • INFO: key_backspace (kbs) terminfo entry: key_backspace=\177
  • INFO: key_dc (kdch1) terminfo entry: key_dc=\E[3~
  • INFO: $COLORTERM='truecolor'

health#nvim_lsp#check

Checking language server protocol configuration

  • INFO: tsserver: configuration checked.
  • INFO: svelte_lsp: configuration checked.
lsp.log

[ DEBUG ] 2020-11-05T20:39:58+0100 ] ...m-unwrapped-0.5.0-799/share/nvim/runtime/lua/vim/lsp.lua:666 ] "LSP[tsserver]" "client.request" 2 "textDocument/formatting" { options = { insertSpaces = true, tabSize = 4 }, textDocument = { uri = "file:///home/tyrion/src/kschema/src/main.ts" }} <function 1> 3
[ DEBUG ] 2020-11-05T20:39:58+0100 ] ...wrapped-0.5.0-799/share/nvim/runtime/lua/vim/lsp/rpc.lua:376 ] "rpc.send.payload" { id = 5, jsonrpc = "2.0", method = "textDocument/formatting", params = { options = { insertSpaces = true, tabSize = 4 }, textDocument = { uri = "file:///home/tyrion/src/kschema/src/main.ts" } }}
[ DEBUG ] 2020-11-05T20:39:58+0100 ] ...wrapped-0.5.0-799/share/nvim/runtime/lua/vim/lsp/rpc.lua:477 ] "decoded" { id = 5, jsonrpc = "2.0", result = {}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions