[1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Bugs which we just recently fixed in our development version and will reach you in the next release.
Post Reply
robot256
Filter Inserter
Filter Inserter
Posts: 536
Joined: Sun Mar 17, 2019 1:52 am
Contact:

[1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by robot256 »

I received a report against Cargo Ships 0.1.19 for a broken require() inside a migration script when opening a tutorial scenario from within another game (with a fairly large mod set). Log file is attached. It appears to affect only tutorials that are loaded from the Tips and Tricks library. Loading save files and tutorial scenarios from the menu applies the migration correctly, even when loading a 0.18 savegame. This behavior is consistent on Linux and WIndows.

In the file "__cargo-ships__/migrations/migrations-0-1-11.lua", there is a line requiring a file from a relative path:

Code: Select all

require("logic.oil_placement")
When loading a save from the main menu, this migration correctly calls the file "__cargo-ships__/logic/oil_placement.lua". When loading a Tips & Tricks scenario, it searches for "__cargo-ships__/migrations/logic/oil_placement.lua", which does not exist. Changing the migration script code to an absolute path fixes the issue:

Code: Select all

require("__cargo-ships__/logic/oil_placement")
This is an issue with a relative path being resolved incorrectly, similar to 67032 and viewtopic.php?p=556401 , but I wanted to document it separately because it happens within the same mod and apparently only in Tips and Tricks scenarios.
The earlier thread about cross-mod compatibility gives a good reason to avoid relative paths anyways, so I am going to update everything to use absolute paths in the future anyways.
Attachments
log of relative path failure.txt
(55.84 KiB) Downloaded 11 times

robot256
Filter Inserter
Filter Inserter
Posts: 536
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by robot256 »

Small update: PI-C was investigating another tutorial migration crash and discovered that the tutorial scenario script calls the migration scripts within the Lua environment of the scenario itself, not within mod sandboxes. This is the root cause for why relative path resolutions are affected, but it also means that migrations involving the global variable table do not operate correctly. I'm not sure if this is a bug or if it was intentional.

PI-C's suggestion was to check the Lua environment variable and skip some or all of the migration script if it is not in a mod-specific sandbox. But we don't know if that will have other implications. The main game tutorial levels are not affected, seemingly because they load from the main menu like a normal save with a Lua sandbox for every mod. Can a dev explain why migration scripts are run this way in tips & tricks tutorial scenarios? Is there a different way to handle the change of Lua environment for them? How much mod code is actually utilized in tips & tricks tutorial scenarios?

Thanks in advance.

Rseding91
Factorio Staff
Factorio Staff
Posts: 12510
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by Rseding91 »

Checking the code: mod-specific scripts are not run at all in tips and tricks scenarios. As far as I can tell the only thing run is the lua defined by the simulation and migration.lua files.

Most likely the issue is that second part: mod migration.lua files were never intended to run in simulations.
If you want to get ahold of me I'm almost always on Discord.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1411
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: [1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by Pi-C »

Rseding91 wrote:
Mon Jan 09, 2023 8:55 pm
Checking the code: mod-specific scripts are not run at all in tips and tricks scenarios. As far as I can tell the only thing run is the lua defined by the simulation and migration.lua files.

Most likely the issue is that second part: mod migration.lua files were never intended to run in simulations.
OK, I've made a simple mod (mig-test_0.0.1.zip), started a new game with that, ran the stack-transfers tutorial, and saved the game:
factorio-previous.log

I then updated the mod (mig-test_0.0.2.zip) and resumed the game:
factorio-current.log, part 1
As you can see, both migration scripts show the global table I've set up in on_init. Also, the global variable CHECK_X which I have defined in migrations/test_1.lua still has the same value in migrations/test_2.lua. You can also see that script.mod_name is the name of my mod.

I then replayed the tutorial:
factorio-current.log, part 2
My migration scripts are run again, but this time, script.mod_name is "level" and the table "global" is empty. What's even more interesting is that my definition of the global variable CHECK_X from migrations/test_1.lua has been invalidated in migrations/test_2.lua.
Attachments
factorio-current.log
(8.68 KiB) Downloaded 16 times
mig-test_0.0.2.zip
(1.51 KiB) Downloaded 12 times
factorio-previous.log
(10.18 KiB) Downloaded 10 times
mig-test_0.0.1.zip
(776 Bytes) Downloaded 10 times
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

robot256
Filter Inserter
Filter Inserter
Posts: 536
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: [1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by robot256 »

Rseding91 wrote:
Mon Jan 09, 2023 8:55 pm
Most likely the issue is that second part: mod migration.lua files were never intended to run in simulations.
To clarify:

Do you mean that it was not intended for the simulation script to call mod migration.lua files, and the simulation script needs to be fixed by you to not call them anymore?

Or do you mean that the mod migration.lua files were written without the intention they would be run in a simulation, and we need to fix them to account for this situation?

Rseding91
Factorio Staff
Factorio Staff
Posts: 12510
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [1.1.74] Require() with relative path in migration script behaves differently in Tip & Tricks scenario

Post by Rseding91 »

robot256 wrote:
Tue Jan 10, 2023 12:36 pm
Do you mean that it was not intended for the simulation script to call mod migration.lua files, and the simulation script needs to be fixed by you to not call them anymore?
This one.
If you want to get ahold of me I'm almost always on Discord.


Post Reply

Return to “Resolved for the next release”