Undefined global `vim`
The kickstart configuration (init.lua) warns next to each "vim.opt....." definition "Undefined global vim". OK, it is only a warning, but is there a way to satisfy the code checking on this issue?
TJ, Thank you for your work.
Uri
I'm not seeing this. Also pressing K on vim.opt shows proper definition:
Can you make sure you have the latest kickstart and update all the plugins to latest versions run:
-
:Lazythen pressS -
:Masonthen pressU
Also, does :checkhealth report any unusual error/warning?
What is your nvim version, and OS type/version?
I noticed this too when opening nvim config files outside of the default locations (~/.config/nvim, ~/.config/nvim-suffix). The neodev help file describes how to configure custom locations, see :help neodev.nvim-neodev.nvim-setup.
@talentix-ch are your files in a custom location?
@aski I just tried it and you are right, if the files are outside nvim config location the hover doc is not complete but I don't get any diagnostic warnings as the OP reported, do you get warnings too? Here is how it looks for me outside the nvim config (example /tmp):
Compared to when the file is in nvim config dir:
So, hover help is different but I don't get any diagnostic warnings.
Yep I get these warnings too after opening the file, making a change and leaving insert mode.
Looking at neodev that's all expected though. Neodev will only activate if the file is either in stdpath('config') or somewhere inside a directory named lua - see here.
Custom directories can be added by changing the neodev plugin definition to something like:
{
'folke/neodev.nvim',
opts = {
override = function(root_dir, library)
if root_dir:find('/your/path/here', 1, true) == 1 then
library.enabled = true
library.plugins = true
end
end,
},
},
@aski ok I see it now, thanks. All of the below conditions are needed to trigger the warnings:
- file is not in nvim config dir
- file parent dir name is not
lua - there is no
luadir in the same dir as the file - make a change or save the file
I was wondering how the various kickstart repos that I have laying around did not show this warning and the reason is that kickstart repo contains a lua dir and that makes neodev accept it. This is probably another case where cloning the whole repo works better than just copying the single init.lua file.
So to recap, a simple work-around for the OP reported warnings is to create a lua dir where the file is located, or move the file to a lua dir.
@dam9000
the warnings should disappear if ...
the edited file is inside a directory named lua:
-
.../lua/file.lua
or
-
.../lua/.../file.lua
or the file is somewhere inside of stdpath('config'), e.g. /home/username/.config/nvim/,
or the override function sets library.enabled=true for the directory in question - see :help neodev.nvim-neodev.nvim-setup or my previous comment
Based on @aski's excellent feedback it seems like this is expected behavior for neodev. Please re-open if that's not the case, and thanks everyone for the report!
I am sorry for picking this back up, I am just in the learning phase and I would like to make sure I understand.
Context, I have my kickstart init.lua in ~/.config/nvim-kick/init.lua. To start neovim with this config, I have the following alias:
> which kvim
kvim: aliased to NVIM_APPNAME=nvim-kick nvim
(this is to allow me to open neovim with different configs using aliases)
I was also seeing these warnings in the init.lua, so I tried to fix it as suggested here, like so:
{
"folke/neodev.nvim",
opts = {
override = function(root_dir, library)
if root_dir:find("~/.config/nvim-kick", 1, true) == 1 then
library.enabled = true
library.plugins = true
end
end,
},
},
However that did not seem to work. I was still having the warnings.
Creating an empty lua/ directory alongside the init.lua did work, but I was wondering why the above snippet did not ?
@wiraki probably the ~ does not expand in lua as it expands in shell.
You can try using vim.env.HOME, something like this:
if root_dir:find(vim.env.HOME .. "/.config/nvim-kick", 1, true) == 1 then
vim.env.HOME
I thought that was a really elegant solution but it didn't fix it.
You are right, that's not enough, in addition the lua_ls has its own root path checks, see:
https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/server_configurations/lua_ls.lua
It's there that the existence of a lua dir is checked, in addition from that file I saw another way to force it to recognize a root dir is to create a .stylua.toml in the folder where your init.lua file is located. But if you really want to configure it programmatically, then try adding this:
diff --git a/init.lua b/init.lua
index 88658ef..8617fda 100644
--- a/init.lua
+++ b/init.lua
@@ -582,6 +582,17 @@ require('lazy').setup({
-- cmd = {...},
-- filetypes = { ...},
-- capabilities = {},
+ root_dir = function(fname)
+ local root = require('lspconfig').lua_ls.document_config.default_config.root_dir(fname)
+ if root then
+ return root
+ end
+ local mypath = vim.env.HOME .. '/.config/nvim-kick'
+ if fname:find(mypath, 1, true) == 1 then
+ return mypath
+ end
+ return nil
+ end,
settings = {
Lua = {
completion = {
Note, the neodev changes that you already made are also required.
Thank you @dam9000 ! This works. It was not really a preference to settle this programmatically, but rather I thought it would be a good exercise in understanding lua neovim config. I can't say I fully grasp what happened though :see_no_evil: