From 167d39b2bef24024be1a48267e14cc6c82146462 Mon Sep 17 00:00:00 2001 From: Jinfeng Guo Date: Mon, 15 Sep 2025 23:19:23 +0800 Subject: [PATCH] fix(clangd): rewrite the root_dir function (#6060) ## Description Nvim 0.11 had some breaking changes in lsp related parts. And one of them is that if the root_dir field of lsp config is a function, the bufnr and a function would be passed instead of the file name in nvim 0.10. As a result, the configurations of extra.lang.clangd broke. However, before the breaking changes of mason and mason-lspconfig, the configurations of clangd still worked. So maybe what really broke the configurations is mason-lspconfig, which uses a new way to enable lsp in v2. I rewrite the root_dir function and make it work both on 0.11 and 0.10. It would check whether the first argument is a string to decide whether to adapt the new interface. To test whether it works, you can just add a plugin/clangd.lua to you personal config with the content below: ```lua return { "neovim/nvim-lspconfig", opts = { servers = { clangd = { root_dir = function(bufnr, ondir) local root_directory = function(fname) return require("lspconfig.util").root_pattern( "Makefile", "configure.ac", "configure.in", "config.h.in", "meson.build", "meson_options.txt", "build.ninja" )(fname) or require("lspconfig.util").root_pattern("compile_commands.json", "compile_flags.txt")( fname ) or require("lspconfig.util").find_git_ancestor(fname) end if type(bufnr) == "string" then return root_directory(bufnr) else local fname = vim.api.nvim_buf_get_name(bufnr) ondir(root_directory(fname)) end end, }, }, }, } ``` ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines. --- lua/lazyvim/plugins/extras/lang/clangd.lua | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lua/lazyvim/plugins/extras/lang/clangd.lua b/lua/lazyvim/plugins/extras/lang/clangd.lua index 33dc4ed4..19f3d1c2 100644 --- a/lua/lazyvim/plugins/extras/lang/clangd.lua +++ b/lua/lazyvim/plugins/extras/lang/clangd.lua @@ -60,18 +60,27 @@ return { keys = { { "ch", "ClangdSwitchSourceHeader", desc = "Switch Source/Header (C/C++)" }, }, - root_dir = function(fname) - return require("lspconfig.util").root_pattern( - "Makefile", - "configure.ac", - "configure.in", - "config.h.in", - "meson.build", - "meson_options.txt", - "build.ninja" - )(fname) or require("lspconfig.util").root_pattern("compile_commands.json", "compile_flags.txt")( - fname - ) or require("lspconfig.util").find_git_ancestor(fname) + root_dir = function(bufnr, ondir) + local root_directory = function(fname) + return require("lspconfig.util").root_pattern( + "Makefile", + "configure.ac", + "configure.in", + "config.h.in", + "meson.build", + "meson_options.txt", + "build.ninja" + )(fname) or require("lspconfig.util").root_pattern( + "compile_commands.json", + "compile_flags.txt" + )(fname) or require("lspconfig.util").find_git_ancestor(fname) + end + if type(bufnr) == "string" then + return root_directory(bufnr) + else + local fname = vim.api.nvim_buf_get_name(bufnr) + ondir(root_directory(fname)) + end end, capabilities = { offsetEncoding = { "utf-16" },