-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Conversation
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 |
There was a problem hiding this comment.
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:
- The file specified by --config-path.
- A .luarc.json file in the workspace.
- The configuration file sent from the LSP client (like settings.json from VS Code).
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you confirm that?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
- 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",
}
- Put the following in my
init.lua
file:
lspconfig.lua_ls.setup({
settings = {
Lua = {
runtime = {
version = "LuaJIT",
},
workspace = {
library = { vim.env.VIMRUNTIME },
},
},
},
})
- Edit a file in the scratch directory with the
.luarc.json
from 1. If I enterfoo, bar = unpack({1, 2, })
, I get a diagnostic for the use ofunpack
instead oftable.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). - 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 loadsvim.env.VIMRUNTIME
. But if I enter that same line in the scratch directory with the.luarc.json
file, I get a warning forUndefined global 'vim'
because the server isn't aware ofvim.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.
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.)
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? |
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.
Without this fix I get errors on startup and the config is not applied properly.