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

docs(lua_ls) Fix broken example setup when workspace_folders are not used #3133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mikehaertl
Copy link
Contributor

Without this fix I get errors on startup and the config is not applied properly.

LSP[lua_ls]: Error ON_INIT_CALLBACK_ERROR: "[string ":lua"]:79: attempt to index field 'workspace_folders' (a nil value)"

@mikehaertl mikehaertl requested a review from glepnir as a code owner May 2, 2024 17:17
local path = client.workspace_folders[1].name
if vim.loop.fs_stat(path..'/.luarc.json') or vim.loop.fs_stat(path..'/.luarc.jsonc') then
return
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glepnir Actually I wonder if the current example configuration makes much sense anymore. From https://luals.github.io/wiki/configuration/:

The language server loads its settings from one of the following sources, in order of priority:

So the if at the beginning should not be required at all anymore.

And even on_init is probably not necessary: The whole config can just be passed in settings as shown here: https://luals.github.io/wiki/configuration/#neovim

What do you think?

Copy link
Member

@lewis6991 lewis6991 May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what I'm using: https://github.com/lewis6991/dotfiles/blob/main/config%2Fnvim%2Flua%2Flewis6991%2Flsp.lua#L137-L146

Basically if there's no .luarc.json which configures the - workspace then apply some default settings which include $VIMRUNTIME. You shouldn't include this unconditionally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't include this unconditionally.

But according to the lua_ls documentation that shouldn't be the case: If the server finds a .luarc.json it will not use the config sent from the LSP client (neovim).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure how to test it as I don't do much lua development and thus don't know what you would set in a .luarc.json. But why not trust the documentation of the LSP server in that regard?

In any case the fix as it is should already be fine as @telemachus pointed out. So if in doubt we could at least merge this change already.

Copy link

@telemachus telemachus Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did the following (minimal) test, and it seems to confirm what @mikehaertl got from the docs.

  1. Create a scratch directory with the following .luarc.json:
{
  "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
  "runtime.version": "Lua 5.4",
}
  1. Put the following in my init.lua file:
lspconfig.lua_ls.setup({
    settings = {
        Lua = {
            runtime = {
                version = "LuaJIT",
            },
            workspace = {
                library = { vim.env.VIMRUNTIME },
            },
        },
    },
})
  1. Edit a file in the scratch directory with the .luarc.json from 1. If I enter foo, bar = unpack({1, 2, }), I get a diagnostic for the use of unpack instead of table.unpack because the language server has read the .luarc.json file and thinks I am using Lua 5.4. If I edit the same content in a directory without the .luarc.json file, I get no warning since the server knows I'm using LuaJIT (i.e., Lua 5.1).
  2. If I enter local getcwd = vim.fn.cwd into a Lua file in a directory without any .luarc.json file, I get no warning because the server loads vim.env.VIMRUNTIME. But if I enter that same line in the scratch directory with the .luarc.json file, I get a warning for Undefined global 'vim' because the server isn't aware of vim.env.VIMRUNTIME (from my LSP settings).

This isn't ironclad confirmation, but it definitely suggests that Mike is right. If there is a .luarc.json file, the server ignores settings from the LSP client. The LSP client itself doesn't seem to need to check for the .luarc.json file again.

In case this matters, I'm using lua-language-server 3.7.4 on macOS, installed via MacPorts.

@glepnir glepnir requested a review from lewis6991 May 3, 2024 06:10
@telemachus
Copy link

I was just about to submit a PR with the same fix for the same reason. (Namely, I get the following error with the current suggested configuration if I open a Lua file in a scratch directory.)

LSP[lua_ls]: Error ON_INIT_CALLBACK_ERROR: "...machus/Documents/git-repos/dotfiles/config/nvim/init.lua:311: attempt to index field 'workspace_folders' (a nil value)"

I'm not sure that I follow the discussion in the comments, but I agree that the docs should suggest a less brittle default configuration. What is the next step here?

telemachus added a commit to telemachus/dotfiles that referenced this pull request Jun 28, 2024
In addition to a lot of smaller tweaks, here are a few more significant
changes.

1. Prevent LSPs from setting formatexpr.
2. Remove .luarc.json.
3. Simplify my LSP settings for Lua. See this issue for discussion.
   neovim/nvim-lspconfig#3133
4. Use vim.o instead of vim.opt.
5. Add completion via hrsh7th/nvim-cmp.
6. Add ysmb-wtsg/in-and-out.nvim for a simple way to jump out of paired
   character groups.
7. Switch to my own fork of mini.surround in order to get highlighting
   of surround actions.
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 this pull request may close these issues.

None yet

3 participants