LuaLS / LuaLS/lua-language-server

Better global recognizing for loadfile

Open
#143 2 comments 0 reactions 0 assignees View on GitHub
enhancement feat/LuaCats Annotations
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

In some cases you might need to make the plugin recognize a specific local variable as global, without declaring that variable as an actual global, one of these cases are using `load` and `loadfile` were you can pass an `env` variable that represents the chunk environment.

*mainFile.lua
```lua
local env = setmetatable(
{foo = function(a, b) return a == b end},
{__index = _G}
)

loadfile('./secondFile.lua', 't', env)
```

*secondFile.lua
```lua
foo(1, 2) -- Undefined global `foo` . Lua Diagnostics.(undefined-global)
```

So i would suggest better recognizing for `load`/`loadfile`/`loadstring` environment or/and a custom emmylua annotations `--- @global` that marks the local var as a global one.

Thanks in advance.

Contributor guide

Open the contributing guide

Research direction

Start by tracing how the language server reports the `undefined-global` diagnostic and how it handles `load`, `loadfile`, and `loadstring` environments. Compare that behavior with the `mainFile.lua` and `secondFile.lua` example, then determine whether environment recognition or an `@global` annotation is the intended scope. Done means the example no longer reports `foo` as undefined under the chosen behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.