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

[Issue] sumneko_lua LSP client is defined but cannot attach to buffer #20

Closed
pidgeon777 opened this issue Jul 24, 2021 · 11 comments
Closed
Labels
invalid This doesn't seem right question Further information is requested

Comments

@pidgeon777
Copy link

Neovim version: V0.6 nightly
OS: Windows 10 x64 Pro

:LspInfo:

Configured servers: ahk_lsp, sumneko_lua, ghdl_ls, vhdl_ls, vhdl_tool, hdl_checker
Neovim logs at: C:\Users\MyUsername\AppData\Local\Temp\nvim/lsp.log

0 client(s) attached to this buffer: 

0 active client(s): 

Clients that match the filetype lua:
  
  Config: sumneko_lua
  	cmd:               lua-language-server
  	cmd is executable: True
  	identified root:   C:\Work\MEGA\Portable\Neovim\config
  	custom handlers:   

lsp.log:

[ START ] 2021-07-25T00:10:23+0200 ] LSP logging initiated
[ ERROR ] 2021-07-25T00:10:26+0200 ] ...A\Portable\Neovim\share\nvim\runtime\lua\vim\lsp\rpc.lua:462 ]	"rpc"	"lua-language-server"	"stderr"	"lua-language-server:\0stdin:1: syntax error near '-'\0"

lua-dev setup file:

local luadev = require("lua-dev").setup({
    lspconfig = {
        filetypes = {"lua"},
        cmd = {"lua-language-server"},
        sumneko_root_path = "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server",
        sumneko_binary    = "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server/bin/Windows/lua-language-server",
    },
})

local lspconfig = require('lspconfig')
lspconfig.sumneko_lua.setup(luadev)
@jose-elias-alvarez
Copy link

Not sure if this is the cause, but I think the root path and path to the binary need to be included in the command, as per the lspconfig wiki.

@pidgeon777
Copy link
Author

pidgeon777 commented Aug 4, 2021

It now seems to attach to the buffer.

My config for the LSP (based on LunarVim):

    lsp = {
      provider = "sumneko_lua",
      setup = {
        -- filetypes = {},
        cmd = {
          -- DATA_PATH .. "/lspinstall/lua/sumneko-lua-language-server", "-E", DATA_PATH .. "/lspinstall/lua/main.lua",
          "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server".."\\bin\\".."Windows".."\\lua-language-server",
          "-E",
          "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server" .. "/main.lua"
        },
        capabilities = common_capabilities,
        on_attach = on_attach_sumneko_lua,
        on_init = common_on_init,
        settings = {
          Lua = {
            runtime = {
              -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
              version = "LuaJIT",
              -- Setup your lua path
              path = vim.split(package.path, ";"),
            },
            diagnostics = {
              -- Get the language server to recognize the `vim` global
              globals = { "vim", "lvim", "MA", "MAA" },
            },
            workspace = {
              -- Make the server aware of Neovim runtime files
              library = {
	        -- WARN: IS THIS SETUP OK?
                ["C:/Work/MEGA/Portable/Neovim/config/LunarVim/lua"] = true,
                ["C:/Work/MEGA/Portable/Neovim/config/MyLunarVim/lua"] = true,
                ["C:/Work/MEGA/Portable/Neovim/config/mine/lua"] = true,
                [vim.fn.expand "$VIMRUNTIME/lua"] = true,
                [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true,
              },
              maxPreload = 100000,
              preloadFileSize = 1000,
            },
          },
        },
      },
    },
    -- lsp = nil,
    -- lsp = {},
  },

Still there seem to be two main problems:

  1. When I try to jump to files of require('module'), if that require is contained in one of the Neovim runtime files or plugins, then it works. But not when those requires are coded inside my custom configuration.
  2. I'm not sure autocomplete is fully enabled, for everything. How could I check that?

More in general, I'm not sure of the above config, and if it could be fixed/optimized somehow.

@folke
Copy link
Owner

folke commented Aug 5, 2021

You shouldn't set the workspace library yourself. That's what this plugin does

@folke folke added the question Further information is requested label Aug 5, 2021
@pidgeon777
Copy link
Author

pidgeon777 commented Aug 5, 2021

I removed the library item and now this is my config:

    lsp = {
      provider = "sumneko_lua",
      setup = {
        -- filetypes = {},
        cmd = {
          -- DATA_PATH .. "/lspinstall/lua/sumneko-lua-language-server", "-E", DATA_PATH .. "/lspinstall/lua/main.lua",
          "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server".."\\bin\\".."Windows".."\\lua-language-server",
          "-E",
          "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server" .. "/main.lua"
        },
        capabilities = common_capabilities,
        on_attach = on_attach_sumneko_lua,
        on_init = common_on_init,
        settings = {
          Lua = {
            runtime = {
              -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
              version = "LuaJIT",
              -- Setup your lua path
              path = vim.split(package.path, ";"),
            },
            diagnostics = {
              -- Get the language server to recognize the `vim` global
              globals = { "vim", "lvim", "MA", "MAA" },
            },
            workspace = {
              maxPreload = 100000,
              preloadFileSize = 1000,
            },
          },
        },
      },
    },
    -- lsp = nil,
    -- lsp = {},
  },

Let consider this file:

C:\Work\MEGA\Portable\Neovim\config\MyLunarVim\config.lua:

if MAA.plugins.tagbar.active then
    require("my-tagbar")
end

The issue is that when looking for the definition of my-tagbar module, the associated file is not opened.

It is located at:

C:\Work\MEGA\Portable\Neovim\config\mine\lua\my-tagbar\init.lua

But, when looking for the definition of another module like at the following require:

    {
        "folke/todo-comments.nvim",
        requires = "nvim-lua/plenary.nvim",
        config = function()
            require("todo-comments").setup { }
        end,
        disable = not MAA.plugins.todo_comments.active,
    },

then the proper file is subsequently opened:

C:\Users\MyUsername\AppData\Local\nvim-data\site\pack\packer\start\todo-comments.nvim\lua\todo-comments\init.lua

@folke
Copy link
Owner

folke commented Aug 5, 2021

You're not using standard directories.

What is your neovim config directory here? vim.fn.stdpath('config')?

Inside that directory, all your lua config should be under lua. Any other structure is non-standard and is not supported by sumneko, unless you manually add in the correct folders to the workspace library.

@folke folke added the invalid This doesn't seem right label Aug 5, 2021
@pidgeon777
Copy link
Author

You're not using standard directories.

You're right. I'm using a portable setup.

What is your neovim config directory here? vim.fn.stdpath('config')?

C:\Users\MyUsername\AppData\Local\nvim

But that path only contains:

plugin\packer_compiled.lua

Content of my sysinit.vim:

if has('win32')
  " -----------------------------
  " LunarVim
  " -----------------------------
  let $VIMCONFIG = fnamemodify(expand("$VIM"), ':h:h') . '\config' " Get the path equivalent to $VIM\config
  let $MYVIMRC = $VIMCONFIG . '\MyLunarVim\init.lua'               " Set $MYVIMRC to point to the init.vim file
  
  let &rtp .= ',' . $VIMCONFIG . '\MyLunarVim' " Add custom runtime path
  let &rtp .= ',' . $VIMCONFIG . '\LunarVim'   " Add custom runtime path
  let &rtp .= ',' . $VIMCONFIG . '\mine'       " Add custom runtime path
  
  exec 'luafile ' . $MYVIMRC
endif

Also:

$VIM = C:\Work\MEGA\Portable\Neovim\share\nvim

thus:

$VIMCONFIG = C:\Work\MEGA\Portable\Neovim\config
$MYVIMRC = C:\Work\MEGA\Portable\Neovim\config\MyLunarVim\init.lua

I would be very interested in finding a possible solution to fix the require navigation issue.

@folke
Copy link
Owner

folke commented Aug 5, 2021

As I mentioned before, this setup is not supported by lua-dev, but you can manually add in the correct paths:

luadev.settings.Lua.workspace.library["C:\\Work\\MEGA\\Portable\\Neovim\\config\\MyLunarVim\\"] = true
luadev.settings.Lua.workspace.library["C:\\Work\\MEGA\\Portable\\Neovim\\config\\mine"] = true

I haven't tested this, so not sure it will work.

I'll close this issue since this is not supported.

@folke folke closed this as completed Aug 5, 2021
@pidgeon777
Copy link
Author

I'm not sure about the place where I should put those extra settings. Any hint?

Currently, my LUA LSP settings are defined here:

https://github.com/LunarVim/LunarVim/blob/rolling/lua/default-config.lua#L690

@folke
Copy link
Owner

folke commented Aug 5, 2021

I don't get it. You are not using lua-dev?

@folke
Copy link
Owner

folke commented Aug 5, 2021

Also, since you;re probably using lspinstall, you need to change sumneko_lua below to lua (from your issue description)

local lspconfig = require('lspconfig')
lspconfig.sumneko_lua.setup(luadev)

@pidgeon777
Copy link
Author

I finally solved it. Here is the new config:

MyLunarVim\config.lua:

if MAA.plugins.lua_dev.active then
    require("my-lua-dev")
end

mine\lua\my-lua-dev\init.lua:

local lspconfig = require 'lspconfig'
local configs = require 'lspconfig/configs'

-- Check if lua_dev server already defined.
if not lspconfig.lua_dev then configs['lua_dev'] = {default_config = {}} end

-- https://github.com/sumneko/lua-language-server/wiki/Build-and-Run-(Standalone)
USER = vim.fn.expand('$USER')

local system_name
if vim.fn.has("mac") == 1 then
    system_name = "macOS"
elseif vim.fn.has("unix") == 1 then
    system_name = "Linux"
elseif vim.fn.has('win32') == 1 or vim.fn.has('win64') == 1 then
    system_name = "Windows"
else
    print("Unsupported system for sumneko")
end

local sumneko_root_path = ""
local sumneko_binary = ""

if vim.fn.has("mac") == 1 then
    sumneko_root_path = "/Users/" .. USER .. "/.config/nvim/lua-language-server"
    sumneko_binary = "/Users/" .. USER .. "/.config/nvim/lua-language-server/bin/macOS/lua-language-server"
elseif vim.fn.has("unix") == 1 then
    sumneko_root_path = "/home/" .. USER .. "/.config/nvim/lua-language-server"
    sumneko_binary = "/home/" .. USER .. "/.config/nvim/lua-language-server/bin/Linux/lua-language-server"
elseif vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 then
    sumneko_root_path = "C:/Work/Projects/Repositories/GitHub/sumneko/lua-language-server"
    sumneko_binary = sumneko_root_path.."\\bin\\"..system_name.."\\lua-language-server"
else
    print("Unsupported system for sumneko")
end

local luadev = require("lua-dev").setup({
    lspconfig = {
        filetypes = {"lua"},
        cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},

        root_dir = require('lspconfig/util').root_pattern(".nv_root"),
        -- WARN: By doing so, two main roots are defined, according to the active buffer path:
        -- LV Config   : C:\Work\MEGA\Portable\Neovim\config
        -- Packer.nvim : C:\Users\MyUsername\AppData\Local\nvim-data
    },
})

-- lspconfig.sumneko_lua.setup(luadev) -- WARN: Conflict with sumneko_lua setups defined elsewhere
lspconfig.lua_dev.setup(luadev)

The trick was indeed to define a new LUA LSP config (lua_dev) for lua-dev.nvim:

lspconfig.lua_dev.setup(luadev)

Otherwise, if defined as sumneko_lua, it would allegedly enter in conflict with other LSP LUA definitions coded elsewhere, leading to issues as in my case.

The strange thing is that some definitions and references seem to be found twice:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants