LuaLS / LuaLS/lua-language-server

Inconsistent Go To Definition Behavior

Open
#2,839 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

### Which OS are you using?

Windows

### What is the issue affecting?

Type Checking

### Expected Behaviour

Consider the following code:

```lua
---@class A
---@field t table
---@field s string

---@class B
---@field a A

---@type B
local b = {
a = {
t = { x = 1 },
s = 'hi',
}
}
```

When you ctrl+click (or Go To Definition) on `a` inside the declaration of variable `b`, the cursor moves to `---@field a A` in the type definition of class `B`. That's useful behavior. Similarly, when you do the same on `s` inside the nested `a` declaration, it goes to `--@field s string` in class `A` as expected.

When you click on `t` however (in `t = { x = 1 }`, it takes you to the `builtin.lua` meta file and places the cursor on the definition for tables `@class table: { [K]: V }`. Not only is that behavior not consistent with the above, but it's also completely useless.

Another example (and probably different issue) is if you move the variable declaration to another file, like this:

File 1:
```lua
---@class A
---@field t table
---@field s string

---@class B
---@field a A
```

File 2:
```lua
---@type B
local b = {
a = {
t = { x = 1 },
s = 'hi',
}
}
```

Now going to the definition of `t` still takes you to `builtin.lua`, but going to the definition of `a`, or `s` does nothing at all. I assume it's because it's treating their "definition" as the same line where they are declared in File 2.

Ideally I would like Go to Definition to do something useful inside table declarations and take me to the type annotation. But if that's not possible it should at least be consistent. In my opinion it should never take you to `builtin.lua` if there's another more suitable definition.

I think this worked fine in a previous version. I only started to see this behavior in the last few months.

### Actual Behaviour

See above.

### Reproduction steps

See above.

### Additional Notes

_No response_

### Log File

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Lua snippets in the issue and reproduce Go To Definition in the Visual Studio Code extension on Windows, comparing inline and cross-file declarations. Trace why the `t` field resolves to `builtin.lua` while `a` and `s` resolve differently. Done means table-field navigation consistently reaches the relevant type annotation or avoids the builtin table definition, with regression coverage for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.