arduino-language-server icon indicating copy to clipboard operation
arduino-language-server copied to clipboard

neovim LSP <-> arduino-language-server crashes on textDocument/definition method.

Open amrlsayed opened this issue 1 year ago • 0 comments

Describe the problem

"nvim" LSP client detach from arduino-language-server with error Unresolved .ino path on textDocument/definition method, when the definition is located on a "not tracked" file. I learned from the source code that the "tracked files" are those which are saved from textDocument/didOpen methods. and to send that from "nvim", I just open the file containing the definition once.

To reproduce

  1. Create a sketch using arduino-cli
arduino-cli sketch new test_server
  1. Create another file(callme.ino) along with the main sketch file(test_server.ino)
  2. Add the following to callme.ino
void callme(void)
{
  Serial.println("Hello World!");
}
  1. Add the following to test_server.ino

void setup() {
  callme();
}
void loop() {
}
  1. Using neovim/nvim-lspconfig configure LSP client by adding the following to your init.lua
require'lspconfig'.arduino_language_server.setup{
  filetypes = {"arduino", "cpp"},
  cmd = {"arduino-language-server",
         "-cli-config",
  "<PATH-TO-CONFIG/arduino-cli.yaml>",
         "-log",
         "-logpath",
 "<PATH-TO-LOG-FOLDER>"}
}
  1. Open nvim
nvim test_server.ino
  1. Move the cursor to callme function call and run
:lua vim.lsp.buf.definition()

Expected behavior

From the text editor point of view, the callme.ino file should get opened and the cursor should be on the function callme.

Arduino Language Server version

0.7.4 and latest main branch commit 6c64232f29f8e61

Arduino CLI version

0.27.1

Operating system

Linux

Operating system version

Ubuntu 22

Additional context

Logged Error.

    !!! Unresolved .ino path: <PATH-TO-/callme.ino>
    !!! Known doc paths are:
    !!! <PATH-To-/test_server/test_server.ino>

I fixed the problem suppressing the error by changing the idePathToIdeURI() function to

func (ls *INOLanguageServer) idePathToIdeURI(logger jsonrpc.FunctionLogger, inoPath string) (lsp.DocumentURI, error) {
	if inoPath == sourcemapper.NotIno.File {
		return sourcemapper.NotInoURI, nil
	}
	doc, ok := ls.trackedIdeDocs[inoPath]
	if !ok {
		logger.Logf("    !!! Unresolved .ino path: %s", inoPath)
		logger.Logf("    !!! Known doc paths are:")
		for p := range ls.trackedIdeDocs {
			logger.Logf("    !!! > %s", p)
		}
		uri := lsp.NewDocumentURI(inoPath)
		return uri, nil //&UnknownURIError{uri} <-- EDIT HERE -->
	}
	return doc.URI, nil
}

But I do not think this is a good solution and would introduce other problems.

Other tries I made

I tried to open the project with several commands but all gave the same behavior.

nvim test_server.ino
nvim .
nvim test_server.ino callme.ino

The only workaround that leads to opening the definition is by opening the callme.ino file :e callme.ino explicitly once before trying to call textDocument/definition.

Issue checklist

  • [X] I searched for previous reports in the issue tracker
  • [X] I verified the problem still occurs when using the latest version
  • [X] My report contains all necessary details

amrlsayed avatar May 26 '23 14:05 amrlsayed