AstroNvim / AstroNvim

AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins
https://AstroNvim.com
GNU General Public License v3.0
12.64k stars 921 forks source link

clangd: -32001: Invalid AST Repeated Error #1838

Closed half-potato closed 1 year ago

half-potato commented 1 year ago

Checklist

Operating system/version

ubuntu 20, Manjaro

Terminal/GUI

kitty

AstroNvim Health

astronvim: require("astronvim.health").check()

AstroNvim ~

Describe the bug

Clangd generates endless invalid AST errors when attached to a buffer that contains C++ code but does not have a typical C++ extension.

clangd version 15.0.7

[ERROR][2023-04-27 12:46:06] .../vim/lsp/rpc.lua:733    "rpc"    "clangd"    "stderr"    "I[12:46:06.856] <-- textDocument/documentHighlight(78)\n"
[ERROR][2023-04-27 12:46:06] .../vim/lsp/rpc.lua:733    "rpc"    "clangd"    "stderr"    "I[12:46:06.857] --> reply:textDocument/documentHighlight(78) 0 ms, error: invalid AST\n"
[ERROR][2023-04-27 12:30:08] .../vim/lsp/rpc.lua:733    "rpc"    "clangd"    "stderr"    "I[12:30:08.795] Failed to find compilation database for /home/amai/ParticleNeRF/include/particles/particle_encoding.h\n"
[ERROR][2023-04-27 12:30:08] .../vim/lsp/rpc.lua:733    "rpc"    "clangd"    "stderr"    "I[12:30:08.795] ASTWorker building file /home/amai/ParticleNeRF/include/particles/particle_encoding.h version 0 with command clangd fallback\n[/home/amai/ParticleNeRF/include/particles]\n/usr/bin/clang -xobjective-c++-header -resource-dir=/home/amai/.local/share/nvim/mason/packages/clangd/clangd/lib/clang/15.0.6 -- /home/amai/ParticleNeRF/include/particles/particle_encoding.h\n"
 Language client log: /home/amai/.local/state/nvim/lsp.log
 Detected filetype:   cuda

 2 client(s) attached to this buffer: 

 Client: clangd (id: 1, bufnr: [11, 22, 34, 58])
     filetypes:       c, cpp, objc, objcpp, cuda, proto
     autostart:       true
     root directory:  /home/amai/ParticleNeRF
     cmd:             clangd

 Client: null-ls (id: 2, bufnr: [11, 22, 34, 58])
     filetypes:       python, java, cs, cuda, proto, cpp, c
     autostart:       false
     root directory:  /home/amai/ParticleNeRF
     cmd:             <function>

 Configured servers list: pyright, pyre, clangd, jedi_language_server
NVIM v0.8.2
Build type: Release
LuaJIT 2.1.0-beta3

Steps to Reproduce

  1. Install clangd LSP client
  2. Open a file like "test.cuh" or "test.inl" and write some C++ code
  3. IDK, close and reopen it
  4. Move cursor around.

Expected behavior

No repeated errors.

Screenshots

No response

Additional Context

No response

mehalter commented 1 year ago

Looks like a bug in clangd language server improperly handling document highlighting requests. This is not something AstroNvim can fix or handle at all, but you can disable the automatic document highlighting in your user configuration for the clangd server until they fix their bugs. Something like this is an example user/init.lua file:

return {
  lsp = {
    config = {
      clangd = {
        on_attach = function(client, bufnr)
          local cmds_found, cmds =
            pcall(vim.api.nvim_get_autocmds, { group = "lsp_document_highlight", buffer = bufnr })
          if cmds_found then vim.tbl_map(function(cmd) vim.api.nvim_del_autocmd(cmd.id) end, cmds) end
        end,
      },
    },
  },
}

Or if you have a split up user configuration, you can just put this in the file user/lsp/config/clangd.lua:

return {
  on_attach = function(client, bufnr)
    local cmds_found, cmds = pcall(vim.api.nvim_get_autocmds, { group = "lsp_document_highlight", buffer = bufnr })
    if cmds_found then vim.tbl_map(function(cmd) vim.api.nvim_del_autocmd(cmd.id) end, cmds) end
  end,
}