LuaLS / LuaLS/lua-language-server

Unexpected repeated alias expanding 类型别名错误地重复展开

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

Nobody has claimed this yet.

bug
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?

Hover

### Expected Behaviour

`hover.expandAlias`为`false`时,如下标注
Set `hover.expandAlias` to `false`, and do annotations like this:
```lua
---while `hover.expandAlias` is `false`
---@alias T string | [T, T]
```
当鼠标置于`[T, T]`中的任意一个`T`时,应为`(alias) T 展开为 string | [T, T]`
When the mouse is placed on one `T` of `[T, T]`, it should be expanded to `(alias) T 展开为 string | [T, T]`

### Actual Behaviour

如图所示
As is seen:
![image](https://github.com/user-attachments/assets/3d977300-38e6-41cf-8b8b-76a32f80f201)

对于任何一个出现了多于一次**递归**引用的别名,都会这样
For any alias that has more than one recursive reference, this will be the case:
![image](https://github.com/user-attachments/assets/dbc54088-b1ec-4c8e-9395-81ceb547d39e)

单次递归引用,或者是简单得没有意义的别名,则不会发生
A single recursive reference, or a simply meaningless alias, will not be with glitches:
![image](https://github.com/user-attachments/assets/e0b0747a-cc0c-4980-b53a-0505ce9f8238)
![image](https://github.com/user-attachments/assets/c3215090-8471-46ec-b193-ef5848d3b77b)

此外,若`hover.expandAlias`为`true`,展开的效果非常惊悚
In addition, if `hover. extpandAlias` is set to `true`, the effect is very frightening:
![image](https://github.com/user-attachments/assets/7f93a3a5-d186-47e7-9bcb-0ff86880681f)

### Reproduction steps

```lua
---while `hover.expandAlias` is `true`
---@alias S string | [S] | [S, S]

---while `hover.expandAlias` is `false`
---@alias T string | [T, T]

---while `hover.expandAlias` is `false`
---@alias U string | {msg: U} | {err: U}

---while `hover.expandAlias` is `false`
---@alias V string | V[]

---while `hover.expandAlias` is `false`
---@alias W {[string]: W} | W[]
```

### Additional Notes

我查阅了一下代码,做出一种猜想:

在[`getInfer`](../blob/master/script/vm/infer.lua#L257)时的[`compileNode`](../blob/master/script/vm/compiler.lua#L2043)错误地使用了先前已缓存的节点(其并未标记这是一个不应该展开的别名),在[`_eraseAlias`](../blob/master/script/vm/infer.lua#L419)发挥效用前就展开了这个别名,并没有经过`hover.expandAlias`的判断,未在`_drop`集合中附上标注。这也是在单次使用递归时不会触发 bug 但两次使用递归就会触发的原因。

I looked up the code and made a guess:

When using [`getInfer`](../blob/master/script/vm/infer.lua#L257), the [`compileNode`](../blob/master/script/vm/compiler.lua#L2043) mistakenly used a previously cached node which was not marked as an alias that should not be expanded. Without going through the judgment of `hover.expandAlias`, it expanded the alias before [`_eraseAlias`](../blob/master/script/vm/infer.lua#L419) takes into effect, which failed in marking in `_drop` set. This is the reason why it will not cause glitch when using recursion once, but only when using recursion twice.

作为本仓库的贡献者之一,我非常乐意协助这个 bug 的修复。但这个 bug 似乎太底层,有点超出我力所能及的范围了。故我提交本次 Issue,抛砖引玉一下。

As one of the contributors to this repository, I am more than happy to assist in fixing this glitch. But this glitch seems too fundamental for me to fix. So I am here to submit this issue for further discussion.

### 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 getInfer and _eraseAlias in script/vm/infer.lua, then inspect compileNode in script/vm/compiler.lua, as identified in the report. Reproduce the hover behavior with the recursive aliases S, T, U, V, and W while toggling hover.expandAlias. Done means recursive aliases no longer expand repeatedly and hover output matches the expected single expansion.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.