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

On nightly, the default config for lua_ls is broken #3052

Closed
abeldekat opened this issue Mar 4, 2024 · 5 comments
Closed

On nightly, the default config for lua_ls is broken #3052

abeldekat opened this issue Mar 4, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@abeldekat
Copy link

abeldekat commented Mar 4, 2024

Description

1709570613

On nightly, without a luarc.json, the suggested default config for lua_ls is broken.

When a .luarc.jsonc is present in the directory of the config, I can complete global "vim":

{
  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
  "runtime": {
    "version": "LuaJIT",
    // "path": [
    //   "?.lua",
    //   "?/init.lua"
    // ],
    // Important setting:
    "pathStrict": true
  },
  "workspace": {
    "library": [
      // No need to append /lua:
      "$VIMRUNTIME",
      //
      "${3rd}/luv/library",
      "${3rd}/busted/library"
    ],
    "checkThirdParty": false
  }
}

When I remove .luarc.jsonc, the code inside the condition in on_init kicks in. As a result, there is no completion for global "vim", and warnings appear:

Undefined global "vim"

Reproduce:

  1. Using the repro provided below, open init.lua, and observe the warnings.
  2. Type vim. and press <c-x><c-o>. Only text suggestions appear.

Repeat the steps, switch the handlers:

handlers = {
  lua_ls = lua_ls_handler, -- no warnings!
  -- lua_ls = lua_ls_handler_with_on_init, -- warnings...
},

Nightly version: NVIM v0.10.0-dev-2507+g3df1211eb

Using 0.9.5, everything works as expected.

Repro:

--[[
Use:
  mkdir ~/.config/repro
  cd ~/.config/repro

  git init .
  touch init.lua
  add the contents of this file to init.lua
  NVIM_APPNAME=repro nvim init.lua ( no reboot necessary )

Remove:
  rm -rf ~/.local/share/repro ~/.local/state/repro ~/.local/cache/repro
  rm -rf ~/.config/repro
--]]
local function clone(path_to_site)
  local mini_path = path_to_site .. "pack/deps/start/mini.deps"
  if not vim.loop.fs_stat(mini_path) then
    vim.cmd('echo "Installing `mini.deps`" | redraw')
    local clone_cmd =
      { "git", "clone", "--filter=blob:none", "https://github.com/echasnovski/mini.deps", mini_path }
    vim.fn.system(clone_cmd)
    vim.cmd("packadd mini.deps | helptags ALL")
    vim.cmd('echo "Installed `mini.deps`" | redraw')
  end
end

-- Setup lua_ls according to the documentation in nvim-lspconfig
-- NOTE: Do not use on_init... Using this method, "vim." completes correctly
local function lua_ls_handler()
  local settings = {
    Lua = {
      runtime = { version = "LuaJIT" },
      workspace = {
        checkThirdParty = false,
        library = {
          vim.env.VIMRUNTIME,
          -- Depending on the usage, you might want to add additional paths here.
          -- E.g.: For using `vim.*` functions, add vim.env.VIMRUNTIME/lua.
          -- "${3rd}/luv/library"
          -- "${3rd}/busted/library",
        },
      },
    },
  }
  require("lspconfig").lua_ls.setup({ settings = settings })
end

-- Setup lua_ls according to the documentation in nvim-lspconfig
-- without a luarc.json: "vim." does not complete
-- warnings: undefined global "vim"
local function lua_ls_handler_with_on_init()
  local settings = {
    Lua = {
      -- Tell the language server which version of Lua you're using
      -- (most likely LuaJIT in the case of Neovim)
      runtime = { version = "LuaJIT" },
      workspace = { -- Make the server aware of Neovim runtime files
        checkThirdParty = false,
        library = {
          vim.env.VIMRUNTIME,
          -- Depending on the usage, you might want to add additional paths here.
          -- E.g.: For using `vim.*` functions, add vim.env.VIMRUNTIME/lua.
          -- "${3rd}/luv/library"
          -- "${3rd}/busted/library",
        },
        -- or pull in all of 'runtimepath'. NOTE: this is a lot slower
        -- library = vim.api.nvim_get_runtime_file("", true)
      },
    },
  }
  require("lspconfig").lua_ls.setup({
    on_init = function(client)
      local path = client.workspace_folders[1].name
      if not vim.loop.fs_stat(path .. "/.luarc.json") and not vim.loop.fs_stat(path .. "/.luarc.jsonc") then
        client.config.settings = vim.tbl_deep_extend("force", client.config.settings, settings)
      end
      return true
    end,
  })
end

 -- package manager
local path_to_site = vim.fn.stdpath("data") .. "/site/"
clone(path_to_site)
local MiniDeps = require("mini.deps")
MiniDeps.setup({ path = { package = path_to_site } })
local add, now = MiniDeps.add, MiniDeps.now

now(function() -- load plugins
  add("folke/tokyonight.nvim")
  vim.cmd.colorscheme("tokyonight")

  add("nvim-treesitter/nvim-treesitter")
  require("nvim-treesitter.configs").setup({ ensure_installed = { "lua" } })

  add("williamboman/mason.nvim")
  require("mason-registry"):on("package:install:success", function()
    --- stylua: ignore
    vim.defer_fn(function() vim.cmd("LspStart") end, 100)
  end)
  require("mason").setup()

  add({ source = "neovim/nvim-lspconfig", depends = { "williamboman/mason-lspconfig.nvim" } })
  require("mason-lspconfig").setup({
    ensure_installed = { "lua_ls" },
    handlers = {
      -- lua_ls = lua_ls_handler,           -- no warnings.
      lua_ls = lua_ls_handler_with_on_init, -- warnings!
    },
  })
end)

Related:

1 Neovim issue, closed
2 PR luarc
3 nvim-lspconf, undefined variable vim

@glepnir
Copy link
Member

glepnir commented Mar 5, 2024

library = {
          vim.env.VIMRUNTIME ..'/lua'

@glepnir glepnir closed this as completed Mar 5, 2024
@abeldekat
Copy link
Author

Hello @glepnir,

I am familiar with the vim.env.VIMRUNTIME .. '/lua' line.

In the provided .luarc.jsonc I added:

// No need to append /lua:
"$VIMRUNTIME",

Also, this issue contains a link to the issue where that line is discussed: #2948
However, adding that line does not fix the problem on the current nightly.

I do think that this issue is Neovim related, because the same code using the same version of nvim-lspconfig behaves differently in 0.9.5 and nightly.
However, my issue in the Neovim repo was closed:

Please report that at nvim-lspconfig.

Could you run the repro code I provided?
If you can reproduce the problem, please consider reopening this issue.

Best regards!

@glepnir
Copy link
Member

glepnir commented Mar 5, 2024

@abeldekat
Copy link
Author

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 = 'pyright'
-- local cmd = { 'pyright-langserver', '--stdio' } -- needed for elixirls, lua_ls, omnisharp
-- 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

local settings = {
  Lua = {
    -- Tell the language server which version of Lua you're using
    -- (most likely LuaJIT in the case of Neovim)
    runtime = { version = "LuaJIT" },
    workspace = { -- Make the server aware of Neovim runtime files
      checkThirdParty = false,
      library = {
        vim.env.VIMRUNTIME,
        -- vim.env.VIMRUNTIME .. "/lua", -- does not solve the problem
        -- Depending on the usage, you might want to add additional paths here.
        -- E.g.: For using `vim.*` functions, add vim.env.VIMRUNTIME/lua.
        -- "${3rd}/luv/library"
        -- "${3rd}/busted/library",
      },
      -- or pull in all of 'runtimepath'. NOTE: this is a lot slower
      -- library = vim.api.nvim_get_runtime_file("", true)
    },
  },
}
nvim_lsp["lua_ls"].setup({
  -- cmd = cmd,
  -- on_attach = on_attach,
  on_init = function(client)
    local path = client.workspace_folders[1].name
    if not vim.loop.fs_stat(path .. "/.luarc.json") and not vim.loop.fs_stat(path .. "/.luarc.jsonc") then
      client.config.settings = vim.tbl_deep_extend("force", client.config.settings, settings)
    end
    return true
  end,
})

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.]]
)

Is the code above usable for you?
Note: All the vim.keymaps are irrelevant, treesitter is not provided and the lua_ls lsp is not installed automatically.

@abeldekat
Copy link
Author

See this PR

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

No branches or pull requests

2 participants