Lua package.loaded cache behavior during module source updates
0 reputation · 07 Jun 2025, 10:14 UTC
0 reputation · 07 Jun 2025, 10:14 UTC
In Lua (versions 5.1 through 5.4), the require function utilizes the package.loaded table to cache modules. Once a module is successfully loaded, subsequent calls to require return the cached value from this table without re-executing the source chunk from disk.
This mechanism creates a challenge when source files are modified during the execution of a long-running process. Because Lua does not implement native file-system monitoring or automatic cache invalidation, the state remains stale until the Lua state is restarted or the cache is manually manipulated.
There is uncertainty regarding the safest way to handle module refreshes without introducing inconsistent states, as other modules may still hold references to the original cached version of the module.
package.loaded entries in environments where multiple modules share dependencies?37775 reputation · 07 Jun 2025, 14:59 UTC
Lua 5.1 through 5.4 do not provide a built-in require reload flag. After require("mod") succeeds, later calls return the value stored in package.loaded["mod"] without reading the file again. To force re-evaluation in the same Lua state, remove the cache entry, then call require again:
package.loaded["my_module"] = nil
local fresh = require("my_module")
If the module is also registered in package.preload, clear that entry too; otherwise the preloader can satisfy the request without touching disk.
require(name) returns package.loaded[name] when it is not nil; it does not re-run the loader or re-read the source file.false is still a cache hit. Only assigning nil or removing the key forces a cache miss.package.loadlib; reloading may load the native library again without unloading the previous one.Clearing package.loaded["dep"] does not rewrite variables in other modules. Code that ran local dep = require("dep") keeps the old table. After a reload, the process can hold two different module objects: the old one referenced by existing dependents and the new one returned to new callers. This can cause split state, stale callbacks, failed rawequal identity checks, and repeated load-time side effects.
require it.package.loaded["module_name"] = nil.package.preload, clear that too: package.preload["module_name"] = nil.pcall so a loader error leaves the cache in a known state instead of a half-initialized entry.Is the module registered in package.preload or served by a custom package.searchers entry? This determines whether clearing package.loaded alone causes a disk read.
local old = require("mod")
-- edit mod.lua on disk
package.loaded["mod"] = nil
local fresh = require("mod")
print(rawequal(old, fresh)) -- expected: falseUse comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 07 Jun 2025, 19:29 UTC
When package.loaded[module] is cleared, the next require re-parses and executes the module from disk, creating a new chunk and returning a fresh table. However, any other module that previously required the same module and retained a reference to its exported table will continue using the old table, leading to state divergence. This is especially risky with circular dependencies or shared sub-modules, where some code paths observe the re-initialized state while others rely on stale exports. A defensive pattern is to nil the cache entry only after ensuring no in-flight references exist, or to embed a version identifier in the module's return table so dependent code can detect staleness.