[0.17.41] Relative paths with require

Things that has been reported already before.
Post Reply
User avatar
Cooldude2606
Long Handed Inserter
Long Handed Inserter
Posts: 86
Joined: Sat Sep 16, 2017 9:04 pm
Contact:

[0.17.41] Relative paths with require

Post by Cooldude2606 »

I am unsure if this is a bug, it may be how lua works however you also altered the require function which is why I am unsure.

While requiring files using relative paths I came accross a bug in my code however I found the cause to be because of lua's package cache; you may already know but require stores required files so that when the file is required again it returns the same table. The error I found was that with relative paths it treated each one as its own package (seen below using log). If this is just something that lua does then im sorry to bother you with this.

Example:

Code: Select all

File Structure
> Mod Root
-> expcore
->-> gui.lua
->-> gui
->->-> left.lua (this file is for left frames)
->->-> toolbar.lua (this file is for top button flow)

Code: Select all

Requires
#gui.lua
require './gui/toolbar'
require './gui/left'
#left.lua
require './toolbar'
#toolbar.lua
log('added button')
The toolbar.lua file had a log function inside which made me notice how it treated them as two different packages; below is log output:
Image

Again just want to say this might not be a bug and may just be how lua works but due to the altered require function I cant be certain that it is just a lua thing.
--- Developer for Explosive Gaming factorio community. Find our code on GitHub. Please contact me via our Discord. ---

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

Re: [0.17.41] Relative paths with require

Post by Rseding91 »

Duplicate of viewtopic.php?f=48&t=67032

Essentially: don't use relative paths with "require".

If I started over I wouldn't support relative paths and would force every file to be absolute including the mod name: "__mod-name__/.../.../..." because that is the only way you will get fully correct behavior with everything.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Duplicates”