Duplicate "go to definition" results when using table assignment syntax in a module
How are you using the lua-language-server?
Kakoune
Which OS are you using?
MacOS
What is the issue affecting?
Other
Expected Behaviour
When a module is written by assigning functions to table members with the following syntax:
local lib={}
lib.func = function()
-- ...
end
return lib
Performing a "Go to definition" on this function from another file that requires the module should go to the function defintion.
Actual Behaviour
Instead when doing a "Go to defintion", there are 2 results, and the user must choose one. They both point to the same line, one points to the table member's name and the other to the start of the function declaration.
Reproduction steps
- Create a project with two files,
lib.luaandother.lua, and populate them as follows:
lib.lua:
local lib={}
lib.func = function() end
return lib
other.lua:
local lib = require('lib')
lib.func()
-
From
other.lua, perform a "Go to definition" onfunc. Note that it has two results that must be chosen, one will take you to the table member's name, and the other will take you to the start of the function declaration. -
Change
lib.luato:
local lib={}
function lib.func() end
return lib
- From
other.lua, perform a "Go to definition" onfunc. It takes you directly to the start of the function declaration (which is also the table member's name).
Additional Notes
I know that the function lib.func() syntax is preferred, but this is an issue for me when using libraries that use the lib.func = function() syntax. I wasn't able to find any discussions of this issue, but it seems likely that others have run into it, so I am wondering if there is a solution that I just haven't found.
Log File
Yes, I'm struggling with this issue too.
I found an even smaller reproduction case that shows the same behavior on Neovim:
local func = function() end
func()
Performing "Go to definition" on func results in two locations showing up: one pointing to the variable assignment (local func) and the other to the function() part.
min.lua|1 col 7-11| local func = function() end
min.lua|1 col 14-28| local func = function() end
This minimal example shows the problem occurs even without any table or module structure, which suggests it's related to how the lua-language-server handles function definitions assigned to variables.
In contrast, I tested a smaller reproduction case in Python with pyright:
func = lambda: None
func()
Using "Go to definition" on func jumps directly to the assignment without multiple choices. I would appreciate it if the lua-language-server could behave similarly—showing a single, clear definition location like Pyright does—making navigation simpler and more intuitive.