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

`duplicate-set-field` is reading from CoreLibs even though `diagnostics.libraryFiles` is set to 'Disable'

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

Formatting

Expected Behaviour

I do not see a warning for duplicate-set-field in my project.

Actual Behaviour

Hi

Thanks for this plugin. I am getting warning for duplicate-set-field on this line:

function playdate.update()

and the duplicate is in the CoreLibs folder here:

...\PlaydateSDK\CoreLibs\__stub.lua

I have set the CoreLibs folder to be ignored by the code checker:

"Lua.diagnostics.libraryFiles": "Disable",

Is this the expected behaviour?

Reproduction steps

  1. Install Lua language server plugin
  2. Set "Lua.diagnostics.libraryFiles": "Disable",
  3. Create main.lua
  4. Add the line function playdate.update() to main.lua
  5. Observe warning:
	"resource": "/e:/dev/miner/Source/main.lua",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "duplicate-set-field",
	"severity": 4,
	"message": "Duplicate field `update`.",
	"source": "Lua Diagnostics.",
	"startLineNumber": 72,
	"startColumn": 10,
	"endLineNumber": 72,
	"endColumn": 25,
	"relatedInformation": [
		{
			"startLineNumber": 5,
			"startColumn": 10,
			"endLineNumber": 5,
			"endColumn": 25,
			"message": ".twopolefilter.s",
			"resource": "/e:/devtools/PlaydateSDK/CoreLibs/__stub.lua"
		}
	]
}]

Additional Notes

No response

Log File

No response

edzillion avatar Mar 27 '23 06:03 edzillion

Add ---@diagnostic disable-next-line: duplicate-set-field before your function.

sumneko avatar Mar 27 '23 08:03 sumneko

Thanks I will do that for the moment, but it seems like this is not the optimal behaviour if I have to do that on every hook. If I disable checking on the libraryFiles it should probably not use those files as a comparison in duplicate-set-field

edzillion avatar Mar 27 '23 08:03 edzillion

Hi @sumneko and @edzillion,

I ran into this issue as well. Here is a simplified repo which demonstrates the issue:

https://github.com/notpeter/luals-2035

Usage

PlaydateSDK and Love2D SDK use a pattern where function callbacks are created in their global object (e.g. playdate.BButtonDown() or love.gamepadpressed()). See PlaydateSDK docs and Love2D Callbacks

Expected behavior:

Functions defined in a file that begins with ---@meta are ignored for the purposes duplicate-set-field.

Actual behavior:

Assuming a namespace.callback defined in a library stub.

LuaLS complains with duplicate-set-field if you directly name a function namespace.callback.

LuaLS does not complain if you assign a function namespace.callback.

image

Workaround

Instead of having to annotate each function with ---@diagnostic disable-next-line: duplicate-set-field you can call the function something else and then explicitly bind the function to namespace.callback = .

notpeter avatar Sep 28 '23 17:09 notpeter

I'm looking into fixing this issue on the LuaCats love2d definitions file and this is impacting progress.

It can be confusing for users implementing love.conf() to encounter a duplicate field message without clear guidance. This message might leave them confused about why it's indicating that a field is already defined.

I have attempted to use the workaround others have posted and it appears that only works when the function has no parameters. Even with functions that have no parameters, VSCode shows both the local implementation and full implementation in the box that pops up.

image

This image shows both the double function names, as well as a big problem with doing this.

The IntelliSense defaults to the any type that's defined in the actual implementation, in my case the user's love.conf where they can't be expected to know exactly why they're getting duplicate set field warnings, nor expected to implement the notation for some 50 classes and fields needed for proper IntelliSense completion options.

JJSax avatar Jul 15 '24 06:07 JJSax

fyi - everything I saw said to fix "Lua.diagnostics.disable": ["duplicate-set-field"]. I tried that and I was still having the issue.

I noticed that intellisense showed an option for duplicate-doc-field. so I changed my settings.json to include the line "Lua.diagnostics.disable": ["duplicate-doc-field"] and it fixed the issue.

I doubt this is a permanent fix but it fixed my problem and it might help someone else.

kurtzilla avatar May 13 '25 00:05 kurtzilla

"diagnostics.libraryFiles": "Disable" is not for disabling warning in your main files. It's just for silencing warnings in library files. But for some reason it's not even working properly according to this: https://github.com/LuaLS/lua-language-server/discussions/3163#discussioncomment-12917733


The duplicate-set-field comes from that: you are redefining a defined field of a class:

---@class module
local module = {}
funciton module.test() return true end

---@class module
local mod = {}

function mod.test() return false end --> warning: duplicate set field
  • here both module and mod are declared to be the definition of @class module
  • and both of them wants to define the .test() function
  • => duplicate set field

PlaydateSDK and Love2D SDK use a pattern where function callbacks are created in their global object (e.g. playdate.BButtonDown() or love.gamepadpressed()).

Although I am unfamiliar with PlaydateSDK or Love2D SDK, I understand the usage here is to define the user callback functions (using the same function name). In one of my PRs #2859, I supported to allowing an instance variable to override any of its class function/method, which should silence the duplicate-set-field warnings:

---@class module
local module = {}
function module.test() return true end

---@type module # note the `@type` here instead of `@class`
local mod

function mod.test() return false end --> no warning
  • So I believe the original issue mentioned by OP should be fixed by now 🤔

I noticed that intellisense showed an option for duplicate-doc-field. so I changed my settings.json to include the line "Lua.diagnostics.disable": ["duplicate-doc-field"] and it fixed the issue.

I am unsure about the usage of this 😕 Can you give a more concrete example? @kurtzilla I believe this should only be triggered if you really defined the @field twice ? (which is probably incorrect)

---@class module
---@field test integer
local module = {}

---@class module
---@field test string --> warning: duplicate-doc-field
local mod

tomlau10 avatar May 13 '25 01:05 tomlau10

    "Lua.diagnostics.libraryFiles": "Disable",
    "Lua.diagnostics.disable": ["duplicate-doc-field"]
}

I have probably opened a whole new can of worms by doing this and it might actually mask errors, but it did remove the warnings in vscode, whereas disabling duplicate-set-field did not

kurtzilla avatar May 13 '25 04:05 kurtzilla

I know there is a Lua.diagnostics.disable config key for masking different kinds of errors. But what I would like to know is your code to reproduce this error 😂 .

Or are you saying that you are having different kinds of errors in your library files, but once you add duplicate-doc-field to the diagnostics.disable, then the "Lua.diagnostics.libraryFiles": "Disable" config starts working correctly and can mute errors from library files? 🤯

tomlau10 avatar May 13 '25 11:05 tomlau10

I didn't do full on investigation so I don't know. I am playing around with using copilot in vscode (chatGPT-4.1) - working on a mod for factorio and have just started yesterday. If you want to look at the code it is here: https://github.com/KzFactorios/FavoriteTeleport/tree/master

What I do notice is that I have to reload the window quite often. Some of the duplicate definition errors seem to stem from the work in progress files that the agent uses. Then vscode can't figure out what files to hold on to or maybe it's a caching thing, like a file appears to have been deleted but it is still in memory somewhere.

Another thing I notice, is that files I have deleted tend to come back into the solution by the agent? The files are empty but I don't understand why they keep showing up.

I am new to this whole agent coding thing and kicking the tires, so to speak. Definitely some weirdness going on. I guess they call it vibe-coding but it's more like ghost-coding for me.

kurtzilla avatar May 13 '25 17:05 kurtzilla

What I do notice is that I have to reload the window quite often. ... Then vscode can't figure out what files to hold on to or maybe it's a caching thing, like a file appears to have been deleted but it is still in memory somewhere.

Ah~ I see, this is indeed another known issue of LuaLS. I am experiencing this randomly after doing each git checkout as well. Somehow LuaLS is still referencing the old files 😕 , and I will also do a reload vscode window in this situation.

  • https://github.com/LuaLS/lua-language-server/issues/1974
  • https://github.com/LuaLS/lua-language-server/issues/2625

tomlau10 avatar May 14 '25 01:05 tomlau10

Prrobably unrelated, but my project started loading very slowly and the entire vscode was running VERY slow. I finally figure out that it was SteelSeries CG (that I don't ewven use anymore) that was causing the issue. Uninstalled it and voila, everything is back to normal again

kurtzilla avatar May 17 '25 21:05 kurtzilla