Setting properties using the environment table from getfenv(0) does not work until certain expressions are used (in Luau)

When pre-existing global variables are redefined in Luau by assigning properties to the environment table (getfenv(0)), optimizations for global variable lookups (i.e. require) do not get disabled until one of the following expressions are used:

print()
warn()
setfenv(0, getfenv(0)) -- Argument #2 can /likely/ be anything. Haven't verified.

To get a better idea of what I’m talking about, these work:

print("1")
getfenv(0).require = print
require("2")

-- Output:
-- 1
-- 2
getfenv(0).require = print
warn("1")
require("2")

-- Output:
-- 1
-- 2

and this does not:

getfenv(0).require = print
require("this should work")

-- Error:
-- 23:44:41.804 - Attempted to call require with invalid argument(s).
-- 23:44:41.804 - Stack Begin
-- 23:44:41.805 - Script 'ServerScriptService.does not work', Line 2
-- 23:44:41.805 - Stack End

Repro file: repro.rbxl (14.9 KB)

1 Like

Thanks! This is a bug with our deoptimization handling that is specific to getfenv(0) (technically also happens for something odd like getfenv(getfenv)). This will be fixed in ~1.5 weeks.

5 Likes

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