New Lua Script Analysis (Beta) marks all Global Variables as "W000: Unknown symbol"

With New Lua Script Analysis (Beta) Enabled, all Global Varibles that I define are underlined in red, and the Script Analysis window is showing that they are marked as “W000: Unknown symbol”. Turning off New Lua Script Analysis (Beta) resolves this issue, however, the code runs fine either way.

Repro steps:

  • Enable New Lua Script Analysis from the Beta Features menu
  • Restart studio
  • Open up a script and define some Global Variables

Screenshot reference:

2 Likes

Probably because they aren’t defined using the local keyword. It’s generally best practice to define them using local, just like you’ve done for your whitelist table.

Even if they’re “global” in the sense that they are used in all scopes within that script, they should still be defined using local.

The thing to bear in mind is it’s simply an analysis tool. It’s not saying your code won’t work, but if it doesn’t, you should look here.

Without explicitly defining your variables using local (which also has performance and readability benefits) how is the analysis tool to know that you intended to put that variable, and haven’t simply misspelled another name that you were trying to use.

This feature is incredibly useful for picking up typos in your variable names, as it highlights anything that hasn’t been properly defined using best practice. I agree it’s not invalid syntax but having it highlight it is useful far more often than it isn’t. On top of that it’s encouraging you to write more readable and more performant code which is always a bonus.

I don’t see how it’s bad practice to define Global Variables without the local keyword. The defining of such variables without the keyword is very widespread and you can find it being utilized often (and supported) throughout Lua documentation.

Whether or not you believe that it’s better to use the local keyword or no keyword to define Global Variables, the lack of using the keyword is widely used and actively supported throughout Lua, and has been supported through LuaU consistently. This is definitely a bug with the Script Analysis tool, even if you would consider it “bad practice”.

Sources

In the Lua 5.3 manual, on multiple occasions, variables are defined without using the local keyword.
Lua 5.3 Reference Manual

In the book “Programming in Lua (Fourth edition)” by Roberto Ierusalimschy, in the section “1.2 Some Lexical Conventions” on the last code block of page 6 we can see that global variables are defined without the local keyword, this practice is not specific to this section but actually widely used throughout the book with no note to it being referred to as bad practice.

3 Likes

I would argue it is definitively better to define them using local:

But practice aside, I’d probably just ask they use the word “undefined” instead of unknown. It’s still incredibly useful to know when I’ve misspelled variable names before running the code and to remove this “bug” would be to remove that feature.

A possible Feature Request could be to have the Script Analysis mark variables that you reference that have not yet been defined with a warning message such as “Reference to undefined variable: …”, but to mark warning on all Global Variables defined without the local keyword is simply wrong and should be addressed.

And if I’m not mistaken, it already does this:
Screen Shot 2020-06-26 at 1.35.15 PM

Script Analysis Tool (W001)

1 Like

Yeah, I had the same problem and I was very confused in why it marks global variables as not defined. Before it used to not do that but now it does which is not good so going back to where it was before would be great.

This is by design. See this page: https://roblox.github.io/luau/typecheck.html

Unknown symbols

You may see this error when using custom globals, and that’s by design even in nonstrict mode.

With BanTech here, this analysis should just be renamed and kept to be clearer. There’s just not really any good use cases for globals on Roblox Lua. If I could lead the uprising with pitchforks against globals I would.

Ultimately we aren’t saying it’s bad practice for no reason. This is observed in every single other programming language ever. Global variables over local variables is bad practice in all of them. The only reason people use them in Lua with little thought is because they seem to just work.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.