Allow loadfile() when require() would be allowed

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
i142857
Inserter
Inserter
Posts: 22
Joined: Mon Nov 04, 2024 9:56 am
Contact:

Allow loadfile() when require() would be allowed

Post by i142857 »

I'd like the Lua function loadfile to work, with the same restrictions as require and load (starting at mod root, not in event handlers, etc., only in text mode, only with .lua extension, etc.). I would be OK with an extra restriction that prevents loadfile from loading files in other mods, if you're concerned, but really the basic cross-mod require can already do a lot of stuff, so I'm not sure this is much worse.

For our own files, this is equivalent to changing the target .lua module to return a large string of all the Lua code, then calling require and passing that to load, so it doesn't let us do anything new, but I really don't want to write the code in a large multi-line string.

Why:
I have a test framework in my mod for running tests. I can run tests both outside Factorio, in normal lua5.2, and inside Factorio. It's nice to be able to run the tests in the real runtime. But some of my tests need to control Factorio globals, such as "game" or "script", and I do so by using loadfile with a custom environment. I could try to mess with _G but that's a lot riskier, and not really a good Lua practice, at least in 5.2. Those tests currently can't run inside Factorio and I'd like to fix that.

Thank you for your consideration :any-quality:
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3889
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Allow loadfile() when require() would be allowed

Post by boskid »

What exactly makes `require()` unusable in your case?
i142857
Inserter
Inserter
Posts: 22
Joined: Mon Nov 04, 2024 9:56 am
Contact:

Re: Allow loadfile() when require() would be allowed

Post by i142857 »

require doesn't take an environment, it always loads the module with the current environment/globals. In my tests I need to control the environment, so that I can replace things like "game" and "script". loadfile (and load) have an optional third parameter that is the environment to load the module with.
curiosity
Filter Inserter
Filter Inserter
Posts: 637
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Allow loadfile() when require() would be allowed

Post by curiosity »

i142857 wrote: Fri May 09, 2025 1:12 pm Why:
I have a test framework in my mod for running tests. I can run tests both outside Factorio, in normal lua5.2, and inside Factorio. It's nice to be able to run the tests in the real runtime. But some of my tests need to control Factorio globals, such as "game" or "script", and I do so by using loadfile with a custom environment. I could try to mess with _G but that's a lot riskier, and not really a good Lua practice, at least in 5.2. Those tests currently can't run inside Factorio and I'd like to fix that.
Have you tried making an instrument mod? Sure, it doesn't let you test multiplayer, but I don't think multiplayer-specific tests would need this.
i142857
Inserter
Inserter
Posts: 22
Joined: Mon Nov 04, 2024 9:56 am
Contact:

Re: Allow loadfile() when require() would be allowed

Post by i142857 »

Yeah, there are several elaborate workarounds like that that I can do, but I asked because they're kind of unreasonably complicated, and it's just kind of weird to allow load() (which also can override the environment) and not loadfile(). To be honest if load() hadn't been allowed I wouldn't have asked :)
Post Reply

Return to “Modding interface requests”