How to suppress warning: undefined global `vim`?

As title.

2 Likes

Is that luacheck or sumnekko_lua or somehting else?

Yes, exactly. it’s sumnekko installed from a plug-in called nvim-lsp-installer. I want to do this because it triggers so many same warnings and looks ugly.

Pretty much what’s listed in the lspconfig docs: nvim-lspconfig/server_configurations.md at master · neovim/nvim-lspconfig · GitHub

But you just need to include vim within it’s diagnostics.global key settings:

require'lspconfig'.sumneko_lua.setup {
  settings = {
    Lua = {
      diagnostics = {
        -- Get the language server to recognize the `vim` global
        globals = {'vim'},
      },
    },
  },
}
5 Likes

Are these the default settings? it doesn’t work by default in my case. Now my issue become: where should I put this line? This is my current settings:

use {
  'neovim/nvim-lspconfig',
  config = function ()
    vim.diagnostic.config({
      virtual_text = false,
      signs = true,
      underline = true,
      update_in_insert = true
    })
  end
}

Nevermind, I’m using williamboman/nvim-lsp-installer, and the config that works is:

  use {
    -- WARNING: this plugin doesn't provide server update.
    'williamboman/nvim-lsp-installer',
    requires = {
      'neovim/nvim-lspconfig',
      'hrsh7th/nvim-cmp',
    },
    config = function()
      local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
      local on_attach = require'config.on_attach'
      require('nvim-lsp-installer').on_server_ready(
        function(server)
          local config = {
            on_attach = on_attach[server.name],
            capabilities = capabilities,
            autostart = true,
            settings = {
              Lua = {
                diagnostics = { globals = {'vim'} }
              }
            }
          }
          server:setup(config)
        end
      )
    end
  }

Hmmm, this will allow the vim global when editing any Lua file at all, right? Not just Lua files in e.g. my dotfiles repository or my neovim configuration?

Yes, all lua files. If you want only for dotfiles/nvim config then you’ll have to configure it on a folder/project basis with a .nvimrc file within the folder. But be careful with that as the guide mentions.

I was hoping to tackle that using per-project .luacheckrc files, like this one: .luacheckrc · main · Ron Waldon / dotfiles · GitLab

The problem, is that this works perfectly when running the linter manually from the command line, but for some reason the linter does not seem to use per-project .luacheckrc files when executed via LSP

The sumneko lua LSP and luacheck are two different tools. What was shown was for sumneko and not for luacheck.

If you also want to see luacheck diagnostics within nvim - instead of getting those messages from the command line - you can try out null-ls (GitHub - jose-elias-alvarez/null-ls.nvim: Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.) or efm-langserver (GitHub - mattn/efm-langserver: General purpose Language Server) which will give you luacheck diagnostics in addition to diagnostics provided from sumneko.

FWIW for anyone encountering this with lsp-zero and lua_ls the solution is listed here from their readme: https://github.com/VonHeikemen/lsp-zero.nvim/blob/v2.x/doc/md/api-reference.md#nvim_lua_lsopts

1 Like

I highly recommend everyone use this instead of simply adding vim to your globals. It has SO much more useful stuff, such as documentation for all lua functions, etc.

It’s leveled up my neovim config like 100x, and im surprised not to see it mentioned more

it fixes the global issue plus a lot more

4 Likes

Hi i had the same warning setting up LSP for lua, lua_ls, Undefined global ‘vim’.

i am using lua_ls
NVIM v0.10.0-dev

You could use one of the code actions that provide a few ways to avoid the warning

With lua_ls you can add global in configuration. If you are using lspconfig it would look like this:

require('lspconfig').lua_ls.setup({
  settings = {
    Lua = {
      diagnostics = {
        globals = {'vim'}
      }
    }
  }
})