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

Global in one file affects diagnostics in another

Open andybak opened this issue 2 years ago • 5 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

Expected Behaviour

If I have 1.lua:

function Main()
    foobar = 1
end

and 2.lua:

function Main()
    print(foobar)
end

in the same workspace/directory.

I would expect 2.lua to show foobar as undefined (unless I explicitly require 1.lua)

Actual Behaviour

No error is shown.

Additional Notes

This might be a pecularity of how I'm using lua (each file defines a separate plugin) but I would be surprised if this is an uncommon usage pattern. At the very least there should be a way to configure the behaviour.

Also - I'd be curious to understand the thinking behind the existing behaviour?

andybak avatar Sep 20 '23 12:09 andybak

Opening each plugin in a separate workspace should allow you to divide the global scope between two scopes. If you have them open as two files in the same directory, they are treated as one workspace, I believe.

See "multiple workspace support".

carsakiller avatar Sep 24 '23 00:09 carsakiller

Ah OK. Obvious in hindsight.

However - this does result in a worse user experience and I'm trying to make the scripting workflow as approachable as possible.

Is this a good enough use-case to argue for the ability to disable workspace scope? LLS is making an assumption about how the lua runtime will load scripts that doesn't feel warranted. I can imagine other scenarios as well (the runtime loads each directory into a single scope but not subdirectories etc).

andybak avatar Sep 24 '23 09:09 andybak

For some reason - my workaround has now stopped working. If I open a single file then the definition file defined in workspace.library is no longer being applied.

So my choice is either:

  1. Open the whole folder as a workspace (and thus have inconsistent and unreliable warnings about undefined globals)
  2. Open each lua file separately and manually add the folder containing the definition file - every single time.

Surely I'm not the only person working in a situation where "VS Code workspace" does not imply "shared variable scope"?

andybak avatar Oct 04 '23 13:10 andybak

Surely I'm not the only person working in a situation where "VS Code workspace" does not imply "shared variable scope"?

Surely you are not. I'm looking for a way for every file to assume it's own defined globals. I'd like to have a way for any globals used without being declared to generate a diagnostic messages. I'd like to be able to declare typed globals outside of the .luarc.json or settings.json files that only pertain to the current file. I'd prefer files that don't require each other should not know about each other.

I think I may be wanting luals to operate very differently than it is because of assumptions carried over from luau type system

mgmchenry avatar May 11 '24 03:05 mgmchenry