[0.17.6] require may behave unexpectedly when loading files from other mods
Posted: Tue Mar 05, 2019 9:16 pm
Say we have mod A with the files config.lua and api.lua and mod B which also contains a config.lua file. Mod A's api.lua file contains something like:
If mod B then does it will get the content of mod A's config.lua, instead of it's own.
This works as it should from the perspective of require and package.loaded, but the programmer is likely to not expect this behavior, which could easily lead to some hard to find bugs.
I don't really know how this should be solved properly, but one (hopefully feasible) idea is to switch the package.loaded table depending on which mod the current file belongs to.
Code: Select all
local config = require 'config'
-- Some code
Code: Select all
local config = require 'config'
This works as it should from the perspective of require and package.loaded, but the programmer is likely to not expect this behavior, which could easily lead to some hard to find bugs.
I don't really know how this should be solved properly, but one (hopefully feasible) idea is to switch the package.loaded table depending on which mod the current file belongs to.