lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Class not recognized when not opening

Open DragoonKiller opened this issue 9 months ago • 6 comments

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

AttachNode is recognized as global and class.

Actual Behaviour

https://github.com/user-attachments/assets/d16ea267-6734-40a8-973c-13f9a6b46b32

Reproduction steps

I've no idea, maybe this is relative to my project.

Additional Notes

vscode settings are set to

    "Lua.workspace.maxPreload": 500000,
    "Lua.workspace.preloadFileSize": 500000,

Log File

file_s%3A_Work_Projects_Project-H.log

DragoonKiller avatar Apr 23 '25 17:04 DragoonKiller

From my own experience, this is usually due to the files being ignored😕 => the classes defined within that file will only be processed when it is opened, and will disappear after closing that file


  • The files in question are under the path Assets/Res/ship/build/*
  • Judging from the log file, I found that you have add ["build/"] = true, in your files.exclude vscode settings
  • By full searching LuaLS's code base, seems the server will utilize this settings to ignore those paths as well: https://github.com/LuaLS/lua-language-server/blob/606ea70d3608bf9cec8b007b7aa499dc94ffb02e/script/workspace/workspace.lua#L134-L146
  • I know that a build/ pattern should not match Assets/Res/ship/build/*, because the pattern isn't prefixed with a **/. But maybe the pattern matcher of LuaLS doesn't work this way, and it considers that a build/ will match Assets/Res/ship/build/* 🤦‍♂ 🐛

minimal reproduction env

  • create the following file structure
test/
├─ lib/
│  ├─ build/
│  │  ├─ mylib.lua
├─ .vscode/
│  ├─ settings.json
main.lua
  • .vscode/settings.json
{
    "files.exclude": {
        "build/": true
    }
}
  • lib/build/mylib.lua
---@class mylib
  • main.lua
---@type mylib # --> Undefined type or alias `mylib`.
  • only if I open lib/build/mylib.lua or remove the files.exclude settings, the mylib class type will be defined

tomlau10 avatar Apr 24 '25 01:04 tomlau10

I have no idea how this can be fixed ...

  • files.exclude uses glob pattern
  • while .gitignore uses gitignore pattern
  • one of the main differences is that a single build/ matches any folder prefix in gitignore style, while it only matches build/ under project root in glob style

Although LuaLS's glob library has both matcheres: https://github.com/LuaLS/lua-language-server/blob/606ea70d3608bf9cec8b007b7aa499dc94ffb02e/script/glob/init.lua#L1-L4

It only uses 1 of the matcher types to process both files.excludes and .gitignore ... https://github.com/LuaLS/lua-language-server/blob/606ea70d3608bf9cec8b007b7aa499dc94ffb02e/script/workspace/workspace.lua#L189-L196

☹ ☹ ☹

tomlau10 avatar Apr 24 '25 02:04 tomlau10

@tomlau10 then it is a design mistake... The correct design could be:

  • There's only one global matcher.
  • The global matcher contains two sub-matcher: the gitignore matcher, and vscode config matcher. Sub-matchers are created from its own pattern. (so build/ won't be in gitignore matcher)
  • for each input, test if it is excluded/included by the two sub-matcher.
  • compose the result and return. (rule can be: exclude when any exclude, include when any include, and default to include) So typically we need to wrap it into a higher level matcher.

DragoonKiller avatar Apr 24 '25 02:04 DragoonKiller

then it is a design mistake

You're right.

I am not maintainer of this project, just a contributor, and I am not very familiar with this part of the code base. Hopefully they can be aware of this issue, and have a better implementation in v4.0 🙂 (they are doing a big refactor currently)

tomlau10 avatar Apr 24 '25 03:04 tomlau10

@tomlau10 wow they are doing refactor? Then I should wait and hear though.

DragoonKiller avatar Apr 24 '25 03:04 DragoonKiller

yeah~ it's been a long time though 😂 related announcement: https://github.com/LuaLS/lua-language-server/discussions/2366

and there is a 4.0.0 branch under construction 👀 https://github.com/LuaLS/lua-language-server/tree/4.0.0

tomlau10 avatar Apr 24 '25 04:04 tomlau10