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

sumneko_lua doesn't work across multiple projects #2366

Closed
musjj opened this issue Dec 29, 2022 · 59 comments
Closed

sumneko_lua doesn't work across multiple projects #2366

musjj opened this issue Dec 29, 2022 · 59 comments
Labels
bug Something isn't working

Comments

@musjj
Copy link
Contributor

musjj commented Dec 29, 2022

Description

sumneko_lua simply doesn't work across multiple projects. The LSP is only working for the first project you opened, but it will stop for working for any subsequent projects.

Neovim version

NVIM v0.9.0-dev-484+gb5edea655
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compiled by runneradmin@fv-az368-865

Nvim-lspconfig version

4329350

Operating system and version

Windows 10

Affected language servers

sumneko_lua

Steps to reproduce

  1. nvim -nu minimal_init.lua
  2. Open a Lua project.
  3. Open a different Lua project from a different root directory, without closing the current nvim session.

Actual behavior

The first opened project works fine. But the subsequently opened projects are completely non-functional.
Trying to open the hover menu with K will give you Workspace loading: 0/0 permanently.
Even the require statement is considered to be undefined.

I'm guessing this is because of the new workspace folders feature added by the recent commits. But I did remember testing the workspace functionality when it was first introduced to lspconfig and it worked perfectly fine.
But after some time, it just stopped working. I suppose there was a recent commit that broke the feature, but I can't quite narrow it down.

Expected behavior

The subsequently opened projects should work just as fine as the first one.

Minimal config

local on_windows = vim.loop.os_uname().version:match 'Windows'

local function join_paths(...)
  local path_sep = on_windows and '\\' or '/'
  local result = table.concat({ ... }, path_sep)
  return result
end

vim.cmd [[set runtimepath=$VIMRUNTIME]]

local temp_dir = vim.loop.os_getenv 'TEMP' or '/tmp'

vim.cmd('set packpath=' .. join_paths(temp_dir, 'nvim', 'site'))

local package_root = join_paths(temp_dir, 'nvim', 'site', 'pack')
local lspconfig_path = join_paths(package_root, 'test', 'start', 'nvim-lspconfig')

if vim.fn.isdirectory(lspconfig_path) ~= 1 then
  vim.fn.system { 'git', 'clone', 'https://github.com/neovim/nvim-lspconfig', lspconfig_path }
end

vim.lsp.set_log_level 'trace'
require('vim.lsp.log').set_format_func(vim.inspect)
local nvim_lsp = require 'lspconfig'
local on_attach = function(_, bufnr)
  local function buf_set_option(...)
    vim.api.nvim_buf_set_option(bufnr, ...)
  end

  buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  local opts = { buffer = bufnr, noremap = true, silent = true }
  vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
  vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
  vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
  vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
  vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
  vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
  vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
  vim.keymap.set('n', '<space>wl', function()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, opts)
  vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
  vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
  vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
  vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
  vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
  vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
  vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
end

-- Add the server that troubles you here
local name = 'sumneko_lua'
local cmd = {'/path/to/sumneko_lua', '--stdio' }
if not name then
  print 'You have not defined a server name, please edit minimal_init.lua'
end
if not nvim_lsp[name].document_config.default_config.cmd and not cmd then
  print [[You have not defined a server default cmd for a server
    that requires it please edit minimal_init.lua]]
end

nvim_lsp[name].setup {
  cmd = cmd,
  on_attach = on_attach,
}

print [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]]

LSP log

https://gist.github.com/musjj/1bf79a2c43957ff4c11cd92e74815145

@musjj musjj added the bug Something isn't working label Dec 29, 2022
@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

you should define a handler for workspace/diagnostic

vim.lsp.handlers['workspace/diagnostic/refresh'] = function(_, _, ctx)
  local ns = vim.lsp.diagnostic.get_namespace(ctx.client_id)
  local bufnr = vim.api.nvim_get_current_buf()
  vim.diagnostic.reset(ns, bufnr)
  return true
end

@glepnir glepnir closed this as completed Dec 29, 2022
@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

That helped get rid of the undefined issue for require statements, but the hover menu still shows Workspace loading: 0/0.
Please do re-open the issue.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

can't reproduce that I used for some days it works fine for me .

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Have you tried the minimal_init.lua?
Try opening your lua config, then open a lua file from one of your installed plugins.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

your sumneko server workspace config ?

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

What do you mean? The minimal_init.lua I included above contains everything I used to produce the issue.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

please try to config the sumneko lua-language server workspace field.

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

If you're talking about workspace.library, that has nothing to do with my issue.
That is only useful for getting diagnosis information from project files that you haven't opened yet.
sumneko_lua should work perfectly fine even after opening multiple projects, without any configuration, and that has been my experience so far but something caused to stop it working recently.

In any case, please do re-open the issue so that it's visible to anyone else who is experiencing the same problem.

Also, can you tell me the neovim and sumneko_lua version you're using?

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

head version and latest version. can you try do i -> ESC -> K on require in you second file

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

i -> ESC -> K

Tried it, doesn't work. I also tried to edit the file and press K again, but it also doesn't work.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

wired it works for me .

images

@glepnir glepnir reopened this Dec 29, 2022
@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Can you try K on any of the variables in the second opened project?

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Also, can you check :LspInfo to see how many sumneko_lua clients is running?

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

image

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

images

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Alright, I found the offending commit: b0bd429
Doing git reset --hard b0bd429^ on the repository fixes the issue.
It seems to be related to: #2355
I'll try to investigate the exact cause later on.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

wired. how to reproduce ? my gif just show you reproduce step 🥲

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Never mind that. Workspace folders doesn't actually work properly before that commit.

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Alright, I found a commit where it actually works: 6eb24ef
In here, clients are properly re-used and diagnosis works across multiple projects.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

I don't think so .that commit not correct. it not send request to client when the workspace changed.

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Ah, you're right...
Require statements are not resolved correctly in the second opened project.
Do you think this issue has any hints:
LuaLS/lua-language-server#981
It seems that not responding to certain requests can cause the workspace to not load properly.

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

I still need a reproduce step on my local 😺 looks useful that issue.

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Reproduce step?

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

yes. your reproduce step works fine for me .

Open a Lua project.
Open a different Lua project from a different root directory, without closing the current nvim session.

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Yes, I'm not sure why it's not working on my machine...
I suspect that it's a Windows issue, but I'm not sure.
Btw, can you check the exact version of your sumneko? I'm using 3.6.4

@glepnir
Copy link
Member

glepnir commented Dec 29, 2022

lua language version is same . there still has a thing that I wondered is the server said the lua workspace not support dynamically.

Note: The server does not support dynamically adding or removing workspaces. If the workspaces change, the client should restart the server.

but how to restart. we only have stop and start function. stop will close this process. so the restart server mean what ? use new config start client with attatched buffers and new workspaceFolders value or something else ? btw I don't read the lua language server code someone told me that support dynamically workspace ..

https://github.com/sumneko/lua-language-server/wiki/Developing#multiple-workspace-support

@musjj
Copy link
Contributor Author

musjj commented Dec 29, 2022

Right, that note also caught my attention.
But it does work just fine on your machine, right? So I'm not sure if that's the cause.

@glepnir
Copy link
Member

glepnir commented Dec 31, 2022

looks like something wrong in there

[TRACE][2022-12-29 13:13:44] .../lua/vim/lsp.lua:1070	"server_request: found handler for"	"workspace/configuration"
[TRACE][2022-12-29 13:13:44] ...lsp/handlers.lua:593	"default_handler"	"workspace/configuration"	{
  ctx = '{\n  client_id = 1,\n  method = "workspace/configuration"\n}',
  result = {
    items = { {
        scopeUri = "file:///c%3A/Users/Username/AppData/Local/nvim-data/lazy/bufferline.nvim",
        section = "Lua"
      }, {
        scopeUri = "file:///c%3A/Users/Username/AppData/Local/nvim-data/lazy/bufferline.nvim",
        section = "files.associations"
      }, {
        scopeUri = "file:///c%3A/Users/Username/AppData/Local/nvim-data/lazy/bufferline.nvim",
        section = "files.exclude"
      }, {
        scopeUri = "file:///c%3A/Users/Username/AppData/Local/nvim-data/lazy/bufferline.nvim",
        section = "editor.semanticHighlighting.enabled"
      }, {
        scopeUri = "file:///c%3A/Users/Username/AppData/Local/nvim-data/lazy/bufferline.nvim",
        section = "editor.acceptSuggestionOnEnter"
      } }
  }
}
[DEBUG][2022-12-29 13:13:44] .../vim/lsp/rpc.lua:403	"server_request: callback result"	{
  result = { {
      telemetry = {
        enable = false
      }
    }, vim.NIL, vim.NIL, vim.NIL, vim.NIL },
  status = true
}
[DEBUG][2022-12-29 13:13:44] .../vim/lsp/rpc.lua:284	"rpc.send"	{
  id = 13,
  jsonrpc = "2.0",
  result = { {
      telemetry = {
        enable = false
      }
    }, vim.NIL, vim.NIL, vim.NIL, vim.NIL }
}
[TRACE][2022-12-29 13:13:44] .../lua/vim/lsp.lua:1053	"notification"	"$/status/report"	{
  text = "😺Lua",
  tooltip = "Workspace   : C:\\Users\\Username\\projects\\dotfiles\\nvim\nWorkspace   : C:\\Users\\Username\\AppData\\Local\\nvim-data\\lazy\\bufferline.nvim\nCached files: 61/61\nMemory usage: 15M"
}
[DEBUG][2022-12-29 13:13:44] .../vim/lsp/rpc.lua:387	"rpc.receive"	{
  id = 14,
  jsonrpc = "2.0",
  method = "workspace/configuration",
  params = {
    items = { {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "Lua"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "files.associations"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "files.exclude"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "editor.semanticHighlighting.enabled"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "editor.acceptSuggestionOnEnter"
      } }
  }
}
[TRACE][2022-12-29 13:13:44] .../lua/vim/lsp.lua:1067	"server_request"	"workspace/configuration"	{
  items = { {
      scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
      section = "Lua"
    }, {
      scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
      section = "files.associations"
    }, {
      scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
      section = "files.exclude"
    }, {
      scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
      section = "editor.semanticHighlighting.enabled"
    }, {
      scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
      section = "editor.acceptSuggestionOnEnter"
    } }
}
[TRACE][2022-12-29 13:13:44] .../lua/vim/lsp.lua:1070	"server_request: found handler for"	"workspace/configuration"
[TRACE][2022-12-29 13:13:44] ...lsp/handlers.lua:593	"default_handler"	"workspace/configuration"	{
  ctx = '{\n  client_id = 1,\n  method = "workspace/configuration"\n}',
  result = {
    items = { {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "Lua"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "files.associations"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "files.exclude"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "editor.semanticHighlighting.enabled"
      }, {
        scopeUri = "file:///c%3A/Users/Username/projects/dotfiles/nvim",
        section = "editor.acceptSuggestionOnEnter"
      } }
  }
}
[DEBUG][2022-12-29 13:13:44] .../vim/lsp/rpc.lua:403	"server_request: callback result"	{
  result = { {
      telemetry = {
        enable = false
      }
    }, vim.NIL, vim.NIL, vim.NIL, vim.NIL },
  status = true
}
[DEBUG][2022-12-29 13:13:44] .../vim/lsp/rpc.lua:284	"rpc.send"	{
  id = 14,
  jsonrpc = "2.0",
  result = { {
      telemetry = {
        enable = false
      }
    }, vim.NIL, vim.NIL, vim.NIL, vim.NIL }
}

@musjj
Copy link
Contributor Author

musjj commented Dec 31, 2022

Is it the vim.NILs in server_request: callback result? What is it supposed to look like normally?

@LostInTheLogs
Copy link

LostInTheLogs commented Dec 31, 2022

I have that issue on linux, go to definition across files works only in the project that was opened first, if I open a different project in the same nvim instance it doesn't work there, but continues to work in the first one.
I think the issue is that now there's one instance of sumneko_lua for two projects and only the root dir is changing and sumneko doesn't seem to like it. iirc it used to be that there were separate sumneko_lua instances per project

There's a possibility that it's a fault of some other lunarvim plugin, I need to test it with a minimal init.lua.

something else is at fault here, it does work with the minimal init.lua

You can ignore everything I just said, lspconfig in lunarvim is outdated, it works with a newer version

@musjj
Copy link
Contributor Author

musjj commented Jan 6, 2023

Tried out Helix and I can't reproduce this issue there. They seem to have multi-workspace support by default (only one instance of lua-language-server is launched for multiple projects), but hover works just fine across all opened projects.

So I guess this is a nvim-specific issue. Still can't tell why it only happens on Windows though.

@musjj
Copy link
Contributor Author

musjj commented Jan 6, 2023

Hmmm, never mind, Helix isn't even emitting any didChangeWorkspaceFolders requests. Not sure how it handles multiple projects with one LSP instance.

@musjj
Copy link
Contributor Author

musjj commented Jan 6, 2023

Never mind, requires are not being resolved in the subsequently opened projects with Helix...

@musjj
Copy link
Contributor Author

musjj commented Jan 7, 2023

Ok, I tried out VSCode now and it seems that what they do is that they restart the server from a scratch every time a new folder is added to the workspace. At least in the case of lua-language-server, this is what they do.
I wonder if this would be the more reliable way of handling multi workspaces. Even on Linux, currently you would get a flash of undefined globals for a moment, which is kinda ugly.

@glepnir
Copy link
Member

glepnir commented Jan 7, 2023

as

lua language version is same . there still has a thing that I wondered is the server said the lua workspace not support dynamically.

Note: The server does not support dynamically adding or removing workspaces. If the workspaces change, the client should restart the server.

but how to restart. we only have stop and start function. stop will close this process. so the restart server mean what ? use new config start client with attatched buffers and new workspaceFolders value or something else ? btw I don't read the lua language server code someone told me that support dynamically workspace ..

https://github.com/sumneko/lua-language-server/wiki/Developing#multiple-workspace-support

like i said in there .doc said it need restart. but for other server what should we do ? both restart or only restart lua server?

@musjj
Copy link
Contributor Author

musjj commented Jan 7, 2023

Yeah, I'm not sure how other LSP extensions handle it either. I've only tried sumneko on VSCode.
Any other LSPs worth testing that comes to mind? I'll try to fiddle with it on VSCode later.

@glepnir
Copy link
Member

glepnir commented Jan 7, 2023

restart it easy just copy the old attached_buffers and new workspaceFolders into config and start it. but there no need do client.rpc.notify for lua server. for other servers which support dynamically add workspace it still need .so write a check code for lua server ?

@musjj
Copy link
Contributor Author

musjj commented Jan 7, 2023

Right, I suppose we can just setup a manual rule/exception list for workspace restart behaviors.
Though I still wonder why sumneko have a handler for didChangeWorkspaceFolders if they don't support dynamically changing workspaces. I don't see it being used in the official extension at all, so I guess it just wasn't a well-tested feature?

@glepnir
Copy link
Member

glepnir commented Jan 7, 2023

yup I saw that code .but don't know how does it works. maybe for future or next version ? create an issue on sumnkeo lua for that would be better. in there currently add a field in sumneko lua config maybe called change_workspace_restart or something else if matched we restart the server.

@musjj
Copy link
Contributor Author

musjj commented Jan 7, 2023

Yeah, sounds like a good idea.
Maybe make it to an enum, in case there are more exceptions in the future:
on_new_folder = "restart" | "notify"

@glepnir
Copy link
Member

glepnir commented Jan 7, 2023

restart | notify does not make sense. because we don't use these value . boolean would be better currently
new_folder_restart = boolean

@musjj
Copy link
Contributor Author

musjj commented Jan 7, 2023

What do you mean by not using these values?
Well, I don't really mind either way, just a suggestion.

@glepnir
Copy link
Member

glepnir commented Jan 7, 2023

check #2383

@justinmk
Copy link
Member

justinmk commented Jan 7, 2023

Though I still wonder why sumneko have a handler for didChangeWorkspaceFolders if they don't support dynamically changing workspaces. I don't see it being used in the official extension at all, so I guess it just wasn't a well-tested feature?

did anyone file an issue with sumneko...?

@asmodeus812
Copy link

asmodeus812 commented Feb 20, 2023

@glepnir I am having the same issue, if i open two different projects, both different .git repos, i get sumneko attached to the first project root, if i then open a file from the other project, sumneko attaches to that buffer/file but the root_dir of the instance stays the same, the same behaviour i can observe for yamlls as well. I would expect that either sumneko and yamls is notified when i change contexts / projects or two instances should be run, one for each project. My setup is just calling lspconfig["yamlls"].setup({}) same for lua_ls (sumneko)

@glepnir
Copy link
Member

glepnir commented Feb 20, 2023

can't reproduce and this issue already solved by lua-ls recently version closed.

lsp

@glepnir glepnir closed this as completed Feb 20, 2023
@asmodeus812
Copy link

Yes, i just tested it it does work okay with lua_ls, but my question about yamlls remains, should i open a new ticket ?

@musjj
Copy link
Contributor Author

musjj commented Feb 20, 2023

I think a part of this issue have re-surfaced again and the handler doesn't even help. I'm getting undefined globals when I open a new project root.
Looking at the log, the workspace/diagnostic/refresh was never requested so the handler doesn't even do anything. You can put in a print statement to verify it.
Might be an upstream bug?

@asmodeus812
Copy link

asmodeus812 commented Feb 24, 2023

I don't know what's going on, i just tested it a few days ago, and it was working just fine with lua_ls, now it is broken again, for reference, seems like the root dir is changing but, could be async, as i am using bufenter to read the client's root dir from the config, to set the cwd for nvim. Just to make sure i am also testing bashls to make sure i am not insane, and bashls for example works just fine when you jump between files from different working dirs. But maybe that is not a reliable way

@glepnir
Copy link
Member

glepnir commented Feb 24, 2023

luals attach function client.capabilities. workspace.diagnostics.supportRefresh = true

@seblyng
Copy link

seblyng commented Feb 24, 2023

luals attach function client.capabilities. workspace.diagnostics.supportRefresh = true

What do you mean? capabilites is not something that is in client. There is server_capabilities but nothings happens when that is set. I also couldn't find anything for supportRefresh in lua-language-server codebase.

I am also having the same problem as others are describing. The root directory is correct but I get a lot of undefined globals and the handler for refresh doesn't do anything for me as well. I am trying to call error("here") inside it just to see if it's called, but nothing is happening

@seblyng
Copy link

seblyng commented Feb 24, 2023

Here is a video:

Screencast.from.24.feb.2023.kl.13.07.+0100.webm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants