Search code examples
luarequire

Lua require does not work but file is in the trace


I'm trying to require files in Lua, in one case it is working, but when I want to simplify the requirements in updating the LUA PATH the file is not found, but it is in the trace!

To reproduce my require problem I did the test with the package.searchpath function, which takes the required key and the Lua path in arguments.

So the code :

print('MY LUA PATH')
local myLuaPath = "?;?.lua;D:\\Projets\\wow-addon\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\src\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\test\\HeyThere\\?"
print(myLuaPath)
print('package search core.add-test')
print(package.searchpath('core.add-test', myLuaPath))
print('package search test.HeyThere.core.add-test')
print(package.searchpath('test.HeyThere.core.add-test', myLuaPath))

The result :

MY LUA PATH
?;?.lua;D:\Projets\wow-addon\HeyThere\?;D:\Projets\wow-addon\HeyThere\src\HeyThere\?;D:\Projets\wow-addon\HeyThere\test\HeyThere\?
package search core.add-test
nil     no file 'core\add-test'
        no file 'core\add-test.lua'
        no file 'D:\Projets\wow-addon\HeyThere\core\add-test'
        no file 'D:\Projets\wow-addon\HeyThere\src\HeyThere\core\add-test'
        no file 'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'
package search test.HeyThere.core.add-test
test\HeyThere\core\add-test.lua

So the first try with 'core.add-test' should work with the 'D:\Projets\wow-addon\HeyThere\test\HeyThere\?' value in the path but fails...

In the trace, there is the file I want!

no file 'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'

But with the same LUA PATH but starting in a parent folder the file is found... Second test with 'test.HeyThere.core.add-test' found from the 'D:\Projets\wow-addon\HeyThere\?'

-> test\HeyThere\core\add-test.lua

Can someone explains to me why it doesn't work the first time?

EDIT :

My current directory is D:\Projets\wow-addon\HeyThere

My lua.exe is in D:\Projets\wow-addon\HeyThere\bin\lua but is added to my PATH variable (I'm on Windows)

I set the LUA_PATH environment variable and execute

lua "test\test-suite.lua" -v

The code inside test-suite.lua is the test code described above


Solution

  • As @EgorSkriptunoff suggested, adding file extansion in the path resolve the problem...

    Ex:

    • Wrong path D:\Projets\wow-addon\HeyThere\?
    • Good path D:\Projets\wow-addon\HeyThere\?.lua

    The extension should be in the path variable because in the require the dot is replace and used as a folder separator.