Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LSP functionality detects incorrect root directory #212

Open
jolars opened this issue Sep 20, 2023 · 5 comments · May be fixed by #213
Open

LSP functionality detects incorrect root directory #212

jolars opened this issue Sep 20, 2023 · 5 comments · May be fixed by #213

Comments

@jolars
Copy link

jolars commented Sep 20, 2023

I use LazyVim, which includes several key mappings, for instance for searching in the root directory, that rely on the LSP client detecting the correct root directory. This is the function LazyVim uses: https://github.com/LazyVim/LazyVim/blob/a72a84972d85e5bbc6b9d60a0983b37efef21b8a/lua/lazyvim/util/init.lua#L56-L89

But it seems like the Copilot LSP server is reporting my home directory as the root directory for every file it attaches to. This is for instance the result of calling :LspInfo from a Makefile in one of my projects.

 Language client log: /home/<user>/.local/state/nvim/lsp.log
 Detected filetype:   make
 
 1 client(s) attached to this buffer: 
 
 Client: copilot (id: 1, bufnr: [26, 24])
 	filetypes:       
 	autostart:       false
 	root directory:  /home/<user>
 	cmd:             node /home/<user>/.local/share/nvim/lazy/copilot.lua/copilot/index.js

So when Copilot is the only LSP server attached to a file, I end up searching directly in my home directory instead of the project directory.

This is the default setup LazyVim uses for copilot, which I use: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/extras/coding/copilot.lua

If I disable the plugin, then everything works fine because lazyvim then just uses cwd.

Maybe it would be possible to expose a root_dir setting just like many of the servers in lspconfig do (https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md)?

A possible interface might be:

require('copilot').setup({
  server_opts_overrides = {
    root_dir = require("lspconfig.util").root_pattern(".git"),
  },
})

Or I guess the server could just not report the root directory, or default to vim.loop.cwd(). I'm not sure exactly if that would work.

@MunifTanjim
Copy link
Collaborator

default to vim.loop.cwd()

root_dir = vim.loop.cwd(),

@jolars
Copy link
Author

jolars commented Sep 20, 2023

default to vim.loop.cwd()

root_dir = vim.loop.cwd(),

Thanks, sorry, missed that.

But why is the server reporting home/<user> as root directory in that case? In the same buffer, running :lua vim.print(vim.loop.cwd()) gives me home/<user>/<project>.

@jolars
Copy link
Author

jolars commented Sep 20, 2023

I don't think it makes sense to set root_dir = vim.loop.cwd(). I suppose that setup() probably doesn't run for every new buffer, right? In which case it would just use the output of vim.loop.cwd() at whatever time it was called.

I'm looking at other configurations at lspconfig now, and I'm thinking it should probably be

root_dir = require("lspconfig.util").find_git_ancestor

I'll test this and submit a PR if it works.

@MunifTanjim
Copy link
Collaborator

But why is the server reporting home/ as root directory in that case? In the same buffer, running :lua vim.print(vim.loop.cwd()) gives me home//.

It's set when you open vim. Are you opening vim on your home directory and later changing the directory?

@jolars
Copy link
Author

jolars commented Sep 20, 2023

Yes, you're right. That's what's happening. If I run neovim from the project directory, it's set appropriately.

@jolars jolars linked a pull request Sep 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants