LuaLS / LuaLS/lua-language-server

Defining optional globals doesn't trigger nil check warning

Open
#2,450 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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?

Type Checking

Expected Behaviour

I have some code that runs in different environments that have different global variables available.

For example, environment1 may have a global table C_GameRules available with a function in it. environment2 does not have the global available.

I'm trying to write a meta file that makes this optional nature of this global available:

---@meta

---@type table?
C_GameRules = nil

---@return boolean
function C_GameRules.IsHardcoreActive() end

I've also tried to define a class, no change in behavior

Actual Behaviour

C_GameRules.IsHardcoreActive() does not trigger a need-nil-check warning, but the type of C_GameRules gets shown as table?

Reproduction steps

see above

Additional Notes

i don't want the inner field as nilable, environment 1 has "C_GameRules" as global available and the function is guaranteed to be in there, environment 2 has the global set to nil

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

Reproduce the report in the Visual Studio Code Extension using the shown Lua meta-file declaration and inspect the type-checking diagnostics for C_GameRules.IsHardcoreActive(). Done means an optional global produces a nil-check warning while its inner function remains non-nilable.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.