You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the Luau Language Server (johnymorgan) with VSCode, I cannot use the "Go to Declaration" functionality for functions defined on tables with metatables and type annotations. This occurs in cases where functions are defined for a table using a type that employs setmetatable for object-like behavior.
Reproduction
Here’s an example:
type AccountImpl = {
__index: AccountImpl,
new: (name: string, balance: number) -> AccountType,
deposit: (self: AccountType, credit: number) -> (),
withdraw: (self: AccountType, debit: number) -> (),
}
type AccountType = typeof(setmetatable({} :: { name: string, balance: number }, {} :: AccountImpl))
local MyAccount: AccountImpl = {} :: AccountImpl
MyAccount.__index = MyAccount
function MyAccount.new(name: string, balance: number): AccountType
local self = {}
self.name = name
self.balance = balance
return setmetatable(self, MyAccount)
end
function MyAccount:deposit(credit: number)
self.balance += credit
end
function MyAccount:withdraw(debit: number)
self.balance -= debit
end
local account = MyAccount.new("Alexander", 500)
account:withdraw(100) -- cannot go to declaration
Expected Behavior
When right-clicking account:withdraw(100) or MyAccount.new(...) and selecting "Go to Declaration," the LSP should navigate to the respective function definitions (MyAccount:withdraw or MyAccount.new).
Observed Behavior
"Go to Declaration" does not work for either the method (withdraw) or the function (new).
Additional Information
Luau Language Server version: 1.32.4
VSCode version: 1.93.1
Reproduction environment: Window
P/S
I have tried adding explicit type annotations, but the issue persists.
If I use Nightrains's Roblox-LSP then the issue no longer persist
Update
I also cannot find "references" & "go to definitions".
So basically I cannot find "references" or "go to definition" if tables assigned to any custom type
The code below is working
local Sample = {}
Sample.__index = Sample
function Sample.HelloWorld(): ()
print("LOL")
end
Sample.HelloWorld() -- Can go to the definition
return Sample
While the other is not
local Sample = {} :: ACustomType
Sample.__index = Sample
function Sample.HelloWorld(): ()
print("LOL")
end
Sample.HelloWorld() -- Can't go to the definition
return Sample
The text was updated successfully, but these errors were encountered:
We rely on Luau's type tracking for function definitions and go to declaration.
When you apply a type assertion, then the Luau type checker doesn't ever care / track your definition of Sample.HelloWorld anymore when you call the function. It is going to try and find the definition of HelloWorld inside of ACustomType.
I don't think there is much we can do here to solve this, because really it is working as intended
JohnnyMorganz
changed the title
Issue: Cannot "Go to Declaration" for functions on tables of a type using metatables
"Go to Declaration" does not work when the type was asserted to a different type
Dec 28, 2024
Problem
When using the Luau Language Server (johnymorgan) with VSCode, I cannot use the "Go to Declaration" functionality for functions defined on tables with metatables and type annotations. This occurs in cases where functions are defined for a table using a type that employs setmetatable for object-like behavior.
Reproduction
Here’s an example:
Expected Behavior
When right-clicking
account:withdraw(100)
orMyAccount.new(...)
and selecting "Go to Declaration," the LSP should navigate to the respective function definitions (MyAccount:withdraw
orMyAccount.new
).Observed Behavior
"Go to Declaration" does not work for either the method (withdraw) or the function (new).
Additional Information
Luau Language Server version: 1.32.4
VSCode version: 1.93.1
Reproduction environment: Window
P/S
Update
Update #2
So basically I cannot find "references" or "go to definition" if tables assigned to any custom type
The code below is working
While the other is not
The text was updated successfully, but these errors were encountered: