Module path separator

Hi all,

during the last weeks I’ve been writing my first Defold game as a hobby with my kids. So far everything went well and we tested the game on my(MacOS) dev machine. However, when trying to bundle the game for windows (or HTML5 etc.) I get errors for all Lua modules.

I have included the modules like this so far:
local m = require("/path/my_module")
This works on the dev machine just fine, but when building for Windows it gives:
the module '//path/my_module' can't be found

After some search I tried to replace the separator with “.”, as they should be replaced with the OS separator.
local m = require("path.my_module")
This way it bundles for Windows, however, it no longer runs one my dev machine and I get
the module 'path.my_module' can't be found

Any ideas what I am doing wrong and how to do it correctly? Thanks!

Correct. Always separate paths with .

Use the . as separator and pay attention to upper and lower case of filenames and paths!

Thanks for the reply!

Case is correct, there must be something else wrong. I’m also wondering about the error message - shouldn’t it have replaced the “.” with the correct separator there already?

What is the location of the lua file in your project?
I.e. what is your folder structure, and what is the exact require you’re using?

The structure is like this (both paths are directories in the project root):
/path1/my_module.lua
/path2/my_script.script
my_script is trying to access the lua module using the above mentioned
local m = require "/path1/my_module" (which works for building in the IDE, but not when bundling)
local m = require "path1.my_module" (which works for bundling, but not in the IDE)

I can’t seem to find a way that works for both cases.

here’s a quick example I created:

It works when both run from the IDE and after bundling.

And here’s the test project.
Require.zip (2.4 KB)

Documentation is here:

1 Like

Thanks for that! After playing around with your test project I found the cause of the problem. Defold does not like to mix separators, even in different require instructions. So if the same project contains some require statements using “/” as separator and some using “.” it will not work.

It looks like Defold takes the first require statement it finds during a build and only accepts the separators found in that one. I have enhanced your sample project to demonstrate this.

Require2.zip (6.8 KB)

Here’s the main.script:

local test2 = require("path1.test")
local test1 = require("/path1/test")

function init(self)
	print("main")
	test1.hello()
	test2.hello()
end

Defold will not accept the second require statement - it doesn’t matter which one is first.

I have now changed all my require statements to using “.” notation and that works, so my problem is resolved.

If my assumption is correct, that is a weird behaviour of Defold but explains my observations. Unless there is a reason behind this behaviour it might be a good idea to change how Defold handles mixed separators in future versions, though.

1 Like

That is indeed a bug yes. Probably has to do with some caching of the paths, and we drop the ball somewhere.

As @britzl suggested: “Correct. Always separate paths with .”

Yes, maybe. I tried the following using Lua 5.1.5:

local test2 = require("path1.test")
local test1 = require("/path1/test")

print("main")
test1.hello()
test2.hello()

for k,v in pairs(package.loaded) do
	print(k,v)
end

And the output:

[√] ~/Downloads/TMP > lua main.lua
main
Hello
Hello
string	table: 0x7fe7a2c098b0
debug	table: 0x7fe7a2c0aaa0
package	table: 0x7fe7a2c075d0
_G	table: 0x7fe7a2c05f60
io	table: 0x7fe7a2c08a20
os	table: 0x7fe7a2c09230
table	table: 0x7fe7a2c07d60
math	table: 0x7fe7a2c0a0e0
/path1/test	table: 0x7fe7a2c0c170
coroutine	table: 0x7fe7a2c071d0
path1.test	table: 0x7fe7a2c0bf80

As you can see in this case it treats them as two different loaded packages despite them referring to the same file.

When testing the same in Defold (on macOS) I get an error that module '/path1/test' not found. Let me try the same on Windows.

I get the same result on Windows.

What do you mean by this? When do you get an error?

I mean, I think the current behavior is weird, as it doesn’t give you a valid module to use, right?
If Lua 5.1 accepts it, then we should too?

I just meant the same error you are getting: module '/path1/test' not found for the second require statement (no matter if the one with “.” or “/” is first). I assume this error is always thrown when Defold encounters a path with separators that are different from the first require statement.

1 Like