Page 1 of 1

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

Posted: Wed Jan 04, 2023 3:43 pm
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.

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

Posted: Mon Jan 09, 2023 8:45 pm
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.

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

Posted: Mon Jan 09, 2023 8:55 pm
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.

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

Posted: Tue Jan 10, 2023 12:23 am
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.

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

Posted: Tue Jan 10, 2023 12:36 pm
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?

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

Posted: Tue Jan 10, 2023 1:28 pm
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.

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

Posted: Tue Jan 10, 2023 2:07 pm
by robot256
Understood, thanks for clarifying!