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

Trying to create a local configuration gives error (Rust project) #42

Open
korken89 opened this issue May 16, 2023 · 7 comments
Open

Trying to create a local configuration gives error (Rust project) #42

korken89 opened this issue May 16, 2023 · 7 comments

Comments

@korken89
Copy link

Hi!

I'm trying to give this plugin a try, but I am getting the following error when running :LspSettings local rust_analyzer:

E5108: Error executing lua ...ig/lua/lspconfig/server_configurations/rust_analyzer.lua:14: assertion failed!
stack traceback:
        [C]: in function 'assert'
        ...ig/lua/lspconfig/server_configurations/rust_analyzer.lua:14: in function 'get_workspace_dir'
        ...ig/lua/lspconfig/server_configurations/rust_analyzer.lua:63: in function 'get_root_dir'
        ...art/nlsp-settings.nvim/lua/nlspsettings/command/init.lua:116: in function <...art/nlsp-settings.nvim/lua/nlspsettings/command/init.lua:107>
        ...art/nlsp-settings.nvim/lua/nlspsettings/command/init.lua:177: in function '_execute'
        ...art/nlsp-settings.nvim/lua/nlspsettings/command/init.lua:185: in function '_command'
        [string ":lua"]:1: in main chunk

My current config is (use default config):

require('nlspsettings').setup()

I'm not sure what's going wrong but it seems to be an internal error to the plugin.
Any assistance would be appreciated!

@korken89
Copy link
Author

It seems like the get_root_dir is not called like a co-routine.

@Qyriad
Copy link

Qyriad commented Jun 16, 2023

It seems like the get_root_dir is not called like a co-routine

@korken89 Indeed, though it seems that it's only a coroutine for rust-analyzer? (Or at least isn't a coroutine for a few other server configs). I'm not sure if there's some way to dynamically detect if that a lspconfig function is a coroutine or not.

This patch fixes the issue, it could be made conditional on server_name == "rust_analyzer":

diff --git a/lua/nlspsettings/command/init.lua b/lua/nlspsettings/command/init.lua
index 7454afe..1b21972 100644
--- a/lua/nlspsettings/command/init.lua
+++ b/lua/nlspsettings/command/init.lua
@@ -113,7 +113,11 @@ M.open_local_config = function(server_name)
   local conf = config.get()
   local root_dir
   if lspconfig[server_name] then
-    root_dir = lspconfig[server_name].get_root_dir(path.sanitize(start_path))
+	local sanitized_start_path = path.sanitize(start_path)
+	local coro = coroutine.wrap(function()
+		return lspconfig[server_name].get_root_dir(sanitized_start_path)
+	end)
+	root_dir = coro()
   end
 
   if not root_dir then

but I'm not sure if there's a reason why this plugin calls lspconfig[server_name].get_root_dir() in the first place instead of vim.lsp.buf.list_workspace_folders() and using the first item? Is there a semantic difference between those?

Alternatively there's also vim.lsp.get_client_by_id(client_id).config.root_dir, which gets the root directory the LSP was started with (which should be set by lspconfig).

@korken89
Copy link
Author

korken89 commented Jun 18, 2023

Hi @Qyriad, thank you for the input!
My LUA knowledge is rock-bottom-bad, but I really want this plugin to work.
I do a lot of embedded and backend work in Rust, and those need different configuration of the LSP to make it work.

With that said I tried getting the original command to work in a fork: 72921a7
I got the async_run from Neovim's lspconfig, and wrapped the entire call in it.
I was able to make it run without error, however it never loaded the LSP config. At that point I paused.

Your idea on using list_workspace_folders() is intriguing, I'll try to wrap my head around how I can use that in stead.

If you have any feedback I'd love to hear it!

Edit: I tested your fix here: https://github.com/korken89/nlsp-settings.nvim/tree/fix2

@korken89
Copy link
Author

korken89 commented Jun 18, 2023

Or, I should say it seem to be semi working?

The issue I am seeing is that if I set the global config to the same config as I set in the local file, I see different results.
What happens if you configure the embedded LSP wrong, an rustc process gets stuck at 100% for a few minutes.

If I set the correct config in the global config, this does not happen.
If I set the correct config in the local config, it does happen.

I also the the print saying that the LSP configuration has been updated if I do update the local LSP config [Lsp Settings] [rust-analyzer] Success to update the settings., so it seems to be working?
Maybe I'm hitting some other but than one in nlsp-settings?

@korken89
Copy link
Author

korken89 commented Jun 18, 2023

I can also see what is going wrong, with the following config:

{
  "rust-analyzer.checkOnSave.allTargets": false,
  "rust-analyzer.checkOnSave.extraArgs": ["--bin", "app"]
}

cargo check is still called with the --all-tagets flag.
So it seems like the settings don't stick?

@korken89
Copy link
Author

Alright, I have gotten close to the root of the issue and I can get the local config to work with a caveat.
Here are my steps:

  1. If I open a project with a local config, the local config is not used - the global config is used.
  2. If I open the local config and save it, everything starts working.

This looks like a race to me between my global config and my local config.
What does anyone else think?

Here is my global config for reference: https://github.com/korken89/nvim-config/blob/master/after/plugin/lspconfig.lua

@Captainfl4me
Copy link

Captainfl4me commented Feb 8, 2024

Alright, I have gotten close to the root of the issue and I can get the local config to work with a caveat. Here are my steps:

  1. If I open a project with a local config, the local config is not used - the global config is used.
  2. If I open the local config and save it, everything starts working.

This looks like a race to me between my global config and my local config. What does anyone else think?

Here is my global config for reference: https://github.com/korken89/nvim-config/blob/master/after/plugin/lspconfig.lua

Hi ! I facing the same issue, any update on it?
PS: Also, what do you mean by "open the local config and save it"?

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

No branches or pull requests

3 participants