Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In a specific case the type of a value in a table can't be obtained in pairs, but can in straight . dot notation #1467

Closed
muppet9010 opened this issue Aug 13, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@muppet9010
Copy link

muppet9010 commented Aug 13, 2022

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

That the type of the value of a table is obtained in both pairs and dot notation at all times.

Type of x and the type of xChunkObject.

local x = chunkDetailsByAxis.xChunks[1]

vs

for _, xChunkObject in pairs(chunkDetailsByAxis.xChunks) do
    ...
end

Actual Behaviour

The "value" field in the FOR loop is unable to get the type of the value in the table, but the dot notation can.

dot notation populating the variable x.
image

FOR loop failing to get the type to populate the variable xChunkObject.
image

Both "know" the type of the parent chunkDetailsByAxis at the time of the issue,
image

The actual warning by Sumneko is on the next line when we try to use a field of xChunkObject, as it's trying to use a field of an unknown type. But this xChunkObject is the start of the issues.

It only seems to occur for this 1 for loop. All my other code retains it's correct types with no warnings shown.

Reproduction steps

The usage of the class is in 1 file and the class is declared in another file. I shall refer to these as Usage and Declare below.

If I edit anything in the Usage file to force Sumneko to rescan then the issue goes away.
I can reproduce the issue in the Usage file by editing the Declare file so that it has an invalid Lua code syntax and then back to being valid again. i.e. removing a trailing ) from any function in the file and then putting it back and saving the file again.

The issue with reproduction is you'd need my mod (public github) and all of the libraries I add of class definitions for Factorio (public). So I can provide them if that's needed, but I appreciate it will be some messing around for you to have the same large environment.

Additional Notes

The type definitions of the these top levels of the class hierarchy are below. This should be the complete loop of self referencing classes. The other custom classes used like int and MapPosition are either simple self contained classes, aliases to equally simple classes or literals.

---@class Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas # Is effectively the XChunks class, as no parent object with extra meta data is needed.
---@field minXValue int # The lowest X chunk position value in the xChunks table.
---@field maxXValue int # The highest X chunk position value in the xChunks table.
---@field minYValueAcrossAllXValues int # The lowest Y chunk position value across all of the X chunk tables. Just to save iterating them all each time to find it.
---@field maxYValueAcrossAllXValues int # The highest Y chunk position value across all of the X chunk tables. Just to save iterating them all each time to find it.
---@field xChunks table<int, Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas_XChunkObject> # A table of X values that have included chunks within them. This can be a gappy list.

---@class Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas_XChunkObject
---@field minYValue int # The lowest Y chunk position value in the yChunks table.
---@field maxYValue int # The highest Y chunk position value in the yChunks table.
---@field yChunks table<int, Task_ScanAreasForActionsToComplete_ChunkDetails> # A table of Y values that have included chunks within them. This can be a gappy list.

---@class Task_ScanAreasForActionsToComplete_ChunkDetails
---@field chunkPosition ChunkPosition
---@field chunkPositionString string # A string of the chunk position. Used by other tasks making use of this data set.
---@field toBeDeconstructedEntityDetails table<uint, Task_ScanAreasForActionsToComplete_EntityDetails> # Keyed by the Id of the entity details. These Id keys list will be very gappy.
---@field toBeUpgradedTypes table<string, table<uint, Task_ScanAreasForActionsToComplete_EntityDetails>> # Grouped by the entity item used to upgrade it first and then keyed by the Id of the entity details. These Id keys list will be very gappy.
---@field toBeBuiltTypes table<string, table<uint, Task_ScanAreasForActionsToComplete_EntityDetails>> # Grouped by the entity item type used to build it first and then keyed by the Id of the entity details. These Id keys list will be very gappy.

---@class Task_ScanAreasForActionsToComplete_EntityDetails
---@field entityListKey uint # Key in the entitiesToBe[X] table.
---@field identifier string|uint # The entities unit_number or its name and position as a string
---@field entity LuaEntity
---@field entity_type string # The type of the entity in relation to the actionType. For deconstruction this is the entity to be removed, for build it is the new entity, for upgrades it is the new entity being upgraded too.
---@field entity_name string # The name of the entity in relation to the actionType. For deconstruction this is the entity to be removed, for build it is the new entity, for upgrades it is the new entity being upgraded too.
---@field position MapPosition
---@field chunkDetails Task_ScanAreasForActionsToComplete_ChunkDetails
---@field actionType Task_ScanAreasForActionsToComplete_ActionType
---@field builtByItemName? string # The name of the item type used to build it for the action type, or nil if no item is required for the action type.
---@field builtByItemCount? uint # The count of the item type used to build it for the action type, or nil if no item is required for the action type. Most are 1, but curved rails are more and some modded things could also be >1.

Log File

file_c%3A_Home_Projects_Factorio%20Modding_Factorio-A-Robot-Friend.log
service.log

In terms of file names of the Usage and Declare I referenced:
Declared: scan-areas-for-actions-to-complete.lua
full: file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-A-Robot-Friend/scripts/tasks/scan-areas-for-actions-to-complete.lua
Usage: deconstruct-entities-in-chunk-details.lua
full: file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-A-Robot-Friend/scripts/tasks/deconstruct-entities-in-chunk-details.lua

@muppet9010 muppet9010 changed the title In a specific case the type of a value in a table can't be obtained in pairs, but can in straight "." dot notation In a specific case the type of a value in a table can't be obtained in pairs, but can in straight . dot notation Aug 13, 2022
muppet9010 added a commit to muppet9010/Factorio-A-Robot-Friend that referenced this issue Aug 13, 2022
Just added a `local x` for the bug report.
LuaLS/lua-language-server#1467
@sumneko
Copy link
Collaborator

sumneko commented Aug 13, 2022

How did you declare int ?

@sumneko
Copy link
Collaborator

sumneko commented Aug 13, 2022

Can you provide an demo project? I can't reproduce this problem.

@sumneko sumneko added the Info Needed More information is required label Aug 13, 2022
@muppet9010
Copy link
Author

int is:

---@alias int integer

I will try and make a standalone complete copy of everything. As the project is loading a large number of the game's Lua code files which "may" be affecting it (I have no idea).

@muppet9010
Copy link
Author

muppet9010 commented Aug 13, 2022

I have put everything in 1 folder and it looks to still produce the issue for me.
Folder can be found here: https://www.dropbox.com/s/ij94q61l1l1o1o0/Factorio-A-Robot-Friend.zip?dl=0

I have put every "Lua" setting from my global settings in to the workspace settings file. So the environments should be the same.
I have included a plugin within the .vscode folder that we use. As without this you'll get lots of warnings about the global global variable. The issue occurs without the plugin, you just get a lot of "noise" errors about other types not being resolvable.
I have excluded the libraries folder and then manually referenced the contents of it within the settings. As this mirrors how my real environment is with the various library folders being outside of the workspace and referenced.

The 2 files are both under scripts > tasks.
Screen recording of issue to help show reproduction steps.
explorer_uVd8qNal2e

@muppet9010
Copy link
Author

hi, just flagging that this has the incorrect label, as I don't believe its awaiting any more information.

@muppet9010
Copy link
Author

Found that when I zipped up the folder it kept on removing the .vscode folder, so added it back in and updated the link in previous message.

@sumneko sumneko added bug Something isn't working and removed Info Needed More information is required labels Aug 24, 2022
@sumneko
Copy link
Collaborator

sumneko commented Aug 24, 2022

Sorry, I don’t have time to view this yet

@muppet9010
Copy link
Author

no worries, was more just so it was accurate for when you did get a chance :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants