Page 1 of 1

[0.17.6] require may behave unexpectedly when loading files from other mods

Posted: Tue Mar 05, 2019 9:16 pm
by theRustyKnife
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:

Code: Select all

local config = require 'config'
-- Some code
If mod B then does

Code: Select all

local config = require 'config'
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.

Re: [0.17.6] require may behave unexpectedly when loading files from other mods

Posted: Tue Mar 05, 2019 9:24 pm
by Rseding91
Thanks for the report. There's no solution to this that doesn't break every mod out there.

In your own code you can solve it a few ways: change the name or better use the absolute path for the require.

Re: [0.17.6] require may behave unexpectedly when loading files from other mods

Posted: Tue Mar 05, 2019 9:53 pm
by Klonan
oof this seems nasty, especiallly as every mod out there has its own flavour of 'util'...