luaneovim

neovim error: when using vim.fs.root(): attempt to call field 'root' (a nil value)


I'm just trying to set up a language server for a language that is not included in the popular lspconfig repo. I'm not a Lua developer and am only superficially familiar with Lua.

I am looking at the example in the documentation. The first example uses the vim.fs.root() function. Looking at the documentation for vim.fs.root(), I see that I can use it to find the top level of my git repo:

-- Find the root of a git repository
vim.fs.root(0, '.git')

However, when I try that in my configuration, I get an error that says I can't use root() because it is nil.

local augroup_foo = vim.api.nvim_create_augroup('foo', { clear = true })
vim.api.nvim_create_autocmd({ 'FileType' }, {
    group = augroup_foo,
    pattern = { "cue" },
    callback = function(ev)
        vim.lsp.start({
            name = "cuepls",
            cmd = { "cuepls" },
            root_dir = vim.fs.root(ev.buf, '.git'), <--- I can't use root
        })
    end,
})

The full error:

Error detected while processing BufNewFile Autocommands for "*": Error executing lua callback: ...brew/Cellar/neovim/0.9.1/share/nvim/runtime/filetype.lua:21: Error executing lua: ...brew/Cellar/neovim/0.9.1/share/nvim/runtime/filetype.lua:22: BufNewFile Autocommands for "*"..FileType Autocommands for "cue": Vim(append):Error executing lua callback: /Users/me/.config/nvim/lua/me/lsp.lua:98: attempt to call field 'root' (a nil value)

The question is this

How do I use vim.fn.root() inside of a vim.lsp.start()?


Solution

  • According to the paths in your error, you're using neovim 0.9.1, but vim.fs.root is only in neovim 0.10.0 (specifically, commit 38b9c322c97b ("feat(fs): add vim.fs.root (#28477)")) and newer.