LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.29k stars 305 forks source link

ERROR: script/core/hint.lua:320: attempt to index a nil value (local 'last') #2788

Closed tmillr closed 1 month ago

tmillr commented 1 month ago

How are you using the lua-language-server?

NeoVim 0.10.1 LuaLS 3.9.3

Which OS are you using?

MacOS

What is the issue affecting?

Other

Expected Behaviour

no error

Actual Behaviour

Almost every time I open a file in my editor, I get this error (multiple at a time):

[20:55:15.316][error][#0:script/core/hint.lua:320]: script/core/hint.lua:320: attempt to index a nil value (local 'last')
stack traceback:
    script/core/hint.lua:320: in local 'callback'
    script/parser/guide.lua:770: in function 'parser.guide.eachSourceTypes'
    script/core/hint.lua:288: in upvalue 'semicolonHint'
    script/core/hint.lua:335: in function 'core.hint'
    script/provider/provider.lua:1407: in function <script/provider/provider.lua:1395>
    [C]: in function 'xpcall'
    script/proto/proto.lua:200: in function <script/proto/proto.lua:175>

Reproduction steps

Open an existing Lua file in neovim.

Additional Notes

Config:

require('lspconfig').lua_ls.setup {
  capabilities = require('cmp_nvim_lsp').default_capabilities(),

  settings = {
    Lua = {
      completion = {
        enable = true,
        autoRequire = true,
        keywordSnippet = 'Replace',
        displayContext = 0,
        postfix = '.',
        requireSeparator = '.',
        showParams = true,
      },

      runtime = {
        path = {
          'lua/?.lua',
          'lua/?/init.lua',
        },
        pathStrict = true,
        version = 'LuaJIT',
      },

      diagnostics = {
        enable = true,
        unusedLocalExclude = { '_*' },
      },

      type = {},

      workspace = {
        library = { vim.env.VIMRUNTIME },
      },

      format = { enable = false, defaultConfig = {} },

      hint = {
        enable = true,
        arrayIndex = 'Enabled',
        paramName = 'All',
        paramType = true,
        semicolon = 'All',
        setType = true,
      },

      hover = {
        enable = true,
        enumsLimit = 100,
        expandAlias = true,
      },

      signatureHelp = { enable = true },

      spell = {},

      semantic = {
        enable = false,
        annotation = true,
        keyword = false,
        variable = false,
      },

      telemetry = {
        enable = false,
      },
    },
  },
}

Log File

No response

clason commented 1 month ago

LuaLS 3.9.3

Is this a typo? Latest version is 3.10.3. And please share a concrete (minimal) file to test with. (Just because the error occurred with every file you happened to test with doesn't mean it'll occur with every file. I for example cannot reproduce the error.)

It's also important to show how you

  1. installed lua-language-server
  2. configured it in Neovim.
tomlau10 commented 1 month ago

Insights to this issue

script/core/hint.lua:320: attempt to index a nil value (local 'last')

This logic is under the if case mode == 'All', where local mode = config.get(uri, 'Lua.hint.semicolon')

=> So this issue happens only when Lua.hint.semicolon is set to All

Reproduction

The default value for Lua.hint.semicolon is SameLine. Once I changed it to All in the .luarc.json (with hint.enable = true of course) of a new workspace, even for an empty file, this error starts to trigger. 🤔

Analysis

I guess when a block (global scope / if case / etc.) have no statement, the src[] is actually an empty array.

Proposed fix

I believe a simple fix like checking #src > 0 should be enough 🤔 Maybe @tmillr you can test about it and then open PR?

        if mode == 'All' and #src > 0 then
            local last = src[#src]
            ...
tmillr commented 1 month ago

LuaLS 3.9.3

Is this a typo?

No it's not. I use the latest version from nixpkgs unstable, which does seem to lag behind somewhat (because with nix multiple pkgs are updated at once, but only if/when all of them pass tests and don't error). Vim plugins and lua pkgs are usually 1-2 weeks behind.

And please share a concrete (minimal) file to test with. (Just because the error occurred with every file you happened to test with doesn't mean it'll occur with every file. I for example cannot reproduce the error.)

Sorry but I don't have one at this point, but I'll update my OP to include the config I'm using. The error actually appears at "random", but often enough to be annoying. From the error message (attempt to index nil), it appears to be an internal logic error.