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

inject-field diagnostics when defining LOVE callbacks.

Open MikuAuahDark opened this issue 2 years ago • 3 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?

Diagnostics/Syntax Checking, Libraries

Expected Behaviour

This should be clean.

local love = require("love")

function love.load() -- no diagnostics here
end

Actual Behaviour

local love = require("love")

function love.load() -- Fields cannot be injected into the reference of `love` for `load`. To do so, use `---@class` for `love`. (inject-field)
end

Reproduction steps

  1. Ensure to have LOVE Definition addon.
  2. Create new main.lua with content above.

Additional Notes

LOVE addon @class is not defined with (exact).

Log File

file_d%3A_Data_Development_love-11.3-win64_inochi2d.log

MikuAuahDark avatar Sep 08 '23 16:09 MikuAuahDark

See my response on the cross post

JJSax avatar Jul 15 '24 06:07 JJSax

If the global variable love and require("love") is exactly the same, why the different diagnostic result? Shouldn't it be same, either giving inject-field warning or no warning at all on both?

Aside that, IMO having require("love") makes it clear to user who reads the source code that in the specific file, LOVE is being used.

MikuAuahDark avatar Jul 15 '24 11:07 MikuAuahDark

If the global variable love and require("love") is exactly the same, why the different diagnostic result?

It seems that you have configured your project to use the love2d meta definition as library, i.e. in the .luarc.json you may have setup something like this:

    "workspace.library": [
        "${3rd}/love2d/library"
    ]

=> this will include the built-in definition files for love2d https://github.com/LuaCATS/love2d/blob/dad72a7eae31f35bf4c6529e5b81f6187b5b7377/library/love.lua

  • In the above meta definition file, it defined love = {} as a global variable with a ---@class love annotation, and returns it at the bottom
  • For what I know, inject-field diagnostic is disabled on global variable
  • But once you used local love = require "love", it becomes a local variable with ---@type love (as returned by aforementioned meta definition file) => inject-field diagnostic is triggered
  • The key difference here is local variable vs global variable. Hope this explains the behavior 🙂

IMO having require("love") makes it clear to user who reads the source code that in the specific file, LOVE is being used.

If you really wants to write require("love") in the file, I suggest writing it as:

love = love or require("love")

function love.load() -- no more warning now
end

=> this makes use of the fact that inject-field diagnostic is disabled on global variable 😄

tomlau10 avatar Jul 15 '24 12:07 tomlau10