kickstart.nvim icon indicating copy to clipboard operation
kickstart.nvim copied to clipboard

Undefined global `vim`

Open talentix-ch opened this issue 1 year ago • 3 comments

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

talentix-ch avatar Mar 30 '24 06:03 talentix-ch

I'm not seeing this. Also pressing K on vim.opt shows proper definition: image

Can you make sure you have the latest kickstart and update all the plugins to latest versions run:

  • :Lazy then press S
  • :Mason then press U

Also, does :checkhealth report any unusual error/warning? What is your nvim version, and OS type/version?

dam9000 avatar Mar 30 '24 08:03 dam9000

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 avatar Apr 01 '24 06:04 aski

@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):

image

Compared to when the file is in nvim config dir:

image

So, hover help is different but I don't get any diagnostic warnings.

dam9000 avatar Apr 01 '24 08:04 dam9000

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 avatar Apr 03 '24 08:04 aski

@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 lua dir 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 avatar Apr 03 '24 10:04 dam9000

@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

aski avatar Apr 04 '24 08:04 aski

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!

feoh avatar Apr 04 '24 16:04 feoh

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 avatar May 16 '24 16:05 wiraki

@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

dam9000 avatar May 16 '24 17:05 dam9000

vim.env.HOME

I thought that was a really elegant solution but it didn't fix it.

image

wiraki avatar May 16 '24 19:05 wiraki

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.

dam9000 avatar May 16 '24 21:05 dam9000

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:

wiraki avatar May 17 '24 08:05 wiraki