Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tmillr opened this issue Aug 10, 2024 · 3 comments · Fixed by #2790
Closed

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

tmillr opened this issue Aug 10, 2024 · 3 comments · Fixed by #2790

Comments

@tmillr
Copy link
Contributor

tmillr commented Aug 10, 2024

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

@tmillr tmillr changed the title BUG: script/core/hint.lua:320: attempt to index a nil value (local 'last') ERROR: script/core/hint.lua:320: attempt to index a nil value (local 'last') Aug 10, 2024
@clason
Copy link

clason commented Aug 10, 2024

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
Copy link
Contributor

tomlau10 commented Aug 10, 2024

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. 🤔

  • .luarc.json
{
    "hint.enable": true,
    "hint.semicolon": "All"
}
  • then just create an empty test.lua, and observe the error when you open the file
  • I used vscode with LuaLS 3.10.3

Analysis

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

  • That's why in the case of empty file, this error already triggers.
    • If the file has at least 1 valid statement say local a => no errors
  • Moreover for an empty if block if true then end => error
    • if block with 1 statement if true then local a end => no error

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
Copy link
Contributor Author

tmillr commented Aug 11, 2024

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.

tmillr added a commit to tmillr/lua-language-server that referenced this issue Aug 11, 2024
tmillr added a commit to tmillr/lua-language-server that referenced this issue Aug 11, 2024
tmillr added a commit to tmillr/lua-language-server that referenced this issue Aug 13, 2024
sumneko pushed a commit to tmillr/lua-language-server that referenced this issue Aug 15, 2024
sumneko pushed a commit that referenced this issue Aug 15, 2024
skarph pushed a commit to skarph/luacats-docgen that referenced this issue Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants