How to suppress warning: undefined global `vim`?

As title.

1 Like

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'},
      },
    },
  },
}
3 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.