Starting order for TCP based LSP / Lua error attempt to index upvalue

Hi

I’m not sure if this is rather an LSP configuration problem or a Lua problem…

I’m trying to use PR#19916 for a language server that just uses a port that is available (DVT). So I can’t use connect until the server runs.

Instead of

vim.lsp.start({name="lsp", cmd=vim.lsp.rpc.connect(host, port)})

I try to do the following (simplified):

vim.lsp.start({
  name="lsp",
  cmd=function()
    start_lsp()
    port=get_lsp_port()
    return vim.lsp.rpc.connect(host, port)
  end
})

This fails with

Error executing lua callback: /usr/share/nvim/runtime/filetype.lua:20: Vim(append):Error executing lua callback: /usr/share/nvim/runtime/lua/vim/lsp.lua:1283: attempt to index upvalue 'rpc' (a function value)

It looks like something local should not be so, but even making all global did not help.
If I just remove the function and keep the vim.lsp.rpc.connect it works (as expected and proposed by the PR).
Can you give me some hint what is wrong with my Lua code?

Or: Is this the completely wrong attempt? How should I get the port after server start? When should the server be started?
I’m trying to have it all in the autocmd for VHDL files, using vim.lsp.start (to hopefully have some manager that only one server is started).

1 Like

Try

vim.lsp.start({
  name="lsp",
  cmd=function(...)
    start_lsp()
    port=get_lsp_port()
    return vim.lsp.rpc.connect(host, port)(...)
  end
})

cmd takes a function that returns the rpc client.
vim.lsp.rpc.connect returns such a function. If you wrap vim.lsp.rpc.connect in a function you must call it, otherwise you have a function that returns a function.

Hi

Thanks a lot for the hint! Problem solved.
There were too many new trees in that new forest, but your point is kind of obvious…

Best regards,
emanuel

1 Like