So suppose the moduleX has a submodule test. And when I do require("moduleX.test") it basically needs to load the file -> "Structure Root/moduleX/src/moduleX/test.lua" I added the path in package.path as "Structure Root/?/src/?.lua". This of course does not work since the default searcher translates it to "Structure Root/moduleX/test/src/moduleX/test.lua". So I wrote this searcher and added it to searchers:
-- Searcher for nested lua modules
package.searchers[#package.searchers + 1] = function(mod)
-- Check if this is a multi hierarchy module
if mod:find(".",1,true) then
-- Get the top most name
local totErr = ""
local top = mod:sub(1,mod:find(".",1,true)-1)
local sep = package.config:match("(.-)%s")
local delim = package.config:match(".-%s+(.-)%s")
local subst = mod:gsub("%.",sep)
-- Now loop through all the lua module paths
for path in package.path:gmatch("(.-)"..delim) do
if path:sub(-5,-1) == "?.lua" then
path = path:sub(1,-6)..subst..".lua"
end
path = path:gsub("%?",top)
--print("Search at..."..path)
-- try loading this file
local f,err = loadfile(path)
if not f then
totErr = totErr.."no file '"..path.."'\n"
else
--print("FOUND")
return f
end
end
return nil,totErr
end
end
Now when I do require("moduleX.test") it is successfully able to load that module. But if I do require("moduleX.tst") I see no error message returned by my searcher. Can anybody please help me figure out what am I doing wrong here.