Page 1 of 1

[Rseding91] [1.1.100] Lua: `require()` absolute and relative path checks are not working

Posted: Sun Sep 01, 2024 3:55 pm
by Hares
I was discussing how import statements work with @justrandomgeek on Discord (link), and he noticed that the relative imports I use in my code should not work.

We digged into testing, and I wrote this simple script which I smashed into my `data.lua`:

Code: Select all

----
--- @param module ModuleID
local function test_require(module)
    local ok, err = pcall(require, module)
    if (not ok)
    then print(("[TRACE]: test-imports: require(%q) -> %s"):format(module, err:gsub("no such file", "\n - ")))
    else print(("[ERROR]: test-imports: require(%q) -> OK"):format(module))
    end
end

test_require('D:\\git\\factorio\\combinators-bridge\\meta\\info.lua')
test_require('/d/git/factorio/combinators-bridge/meta/info.lua')
test_require('module')
test_require('thismod.module')
test_require('.relative')
test_require('./relative')
test_require('__combinators-bridge__.module')
test_require('__compaktcircuit__.module')
test_require('__inactive__.module')

error("Stop loading")

Note that "D:\\git\\factorio\\combinators-bridge\\meta\\info.lua" is a valid file within the same mod.
Even though the statements 1,2,5,6 should return an "Explicit relative paths are not supported" error message, in practice only #2 did:

Code: Select all

[TRACE]: test-imports: require("thismod.module") -> module thismod.module not found;  
 -  __combinators-bridge__/local/thismod/module.lua 
 -  __combinators-bridge__/thismod/module.lua 
 -  __core__/lualib/thismod/module.lua
[TRACE]: test-imports: require(".relative") -> module .relative not found;  
 -  __combinators-bridge__/local/relative.lua 
 -  __combinators-bridge__/relative.lua 
 -  __core__/lualib/relative.lua
[TRACE]: test-imports: require("./relative") -> module ./relative not found;  
 -  __combinators-bridge__/local/./relative.lua 
 -  __combinators-bridge__/./relative.lua 
 -  __core__/lualib/./relative.lua
[TRACE]: test-imports: require("__combinators-bridge__.module") -> module __combinators-bridge__.module not found;  
 -  __combinators-bridge__/module.lua
[TRACE]: test-imports: require("__compaktcircuit__.module") -> module __compaktcircuit__.module not found;  
 -  __compaktcircuit__/module.lua
[TRACE]: test-imports: require("__inactive__.module") -> Path __inactive__/module.lua does not match any enabled mod.
   1.163 Error ModManager.cpp:1625: Failed to load mod "combinators-bridge": __combinators-bridge__/local/test-requires.lua:21: Stop loading
Even more, the #1 successfully loaded even though this should not be possible.

Re: Lua: `require()` absolute and relative path checks are not working

Posted: Sun Sep 01, 2024 4:17 pm
by Hares
However, if you ask me, I think that imports with form of "require('./local')" or "require('.local')" should be viable and look up only in the current directory, ignoring mod root or "__core__.lualib".

Also, there's another linked problem related to the imports, causing wrong mod lookup if you import other mod files. (That was the problem I initially started digging.)

Code: Select all

mymod.module:
require('__othermod__.package.stuff')

othermod.package.stuff:
require('misc.more-stuff')

othermod.misc.more-stuff: (exists)
Results in the wrong lookup:

Code: Select all

Original message:
Failed to load mods: __compaktcircuit__/data/combinators.lua:1: module scripts.commons not found;  no such file __compaktcircuit__/data/scripts/commons.lua no such file __combinators-bridge__/scripts/commons.lua no such file __core__/lualib/scripts/commons.lua

In the terminology declared above:
- __othermod__.package.misc.stuff [OK]
- __mymod__.misc.stuff            [Wrong!] Should be __othermod__.misc.stuff
- __core__.lualib.misc.stuff      [OK]

Re: [Rseding91] [1.1.100] Lua: `require()` absolute and relative path checks are not working

Posted: Thu Oct 10, 2024 9:17 pm
by Rseding91
Thanks for the report. This is now fixed for 2.0.