From 65c417e0ecda55f44c691df032163a8c08f0b52a Mon Sep 17 00:00:00 2001
From: Hisham Muhammad <hisham@gobolinux.org>
Date: Sun, 1 Jul 2018 14:50:59 -0300
Subject: path, loader: handle coexisting foo.bar and foo.bar.init modules

LuaRocks used to conflate in its manifest two modules whose names only
differed by ".init" (e.g. "foo.bar" and "foo.bar.init"). With this change,
`path.path_to_modules` treats them as distinct modules, and `luarocks.loader`
handles them correctly (given `require("foo.bar")`, it looks for
`foo.bar` first and then `foo.bar.init` next).
---
 src/luarocks/core/path.lua |  6 +-----
 src/luarocks/loader.lua    | 35 +++++++++++++++++++++++------------
 2 files changed, 24 insertions(+), 17 deletions(-)

(limited to 'src')

diff --git a/src/luarocks/core/path.lua b/src/luarocks/core/path.lua
index adebaa23..179d3aaf 100644
--- a/src/luarocks/core/path.lua
+++ b/src/luarocks/core/path.lua
@@ -36,7 +36,7 @@ end
 
 --- Convert a pathname to a module identifier.
 -- In Unix, for example, a path "foo/bar/baz.lua" is converted to
--- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo".
+-- "foo.bar.baz"; "bla/init.lua" returns "bla.init"; "foo.so" returns "foo".
 -- @param file string: Pathname of module
 -- @return string: The module identifier, or nil if given path is
 -- not a conformant module path (the function does not check if the
@@ -47,10 +47,6 @@ function path.path_to_module(file)
    local name = file:match("(.*)%."..cfg.lua_extension.."$")
    if name then
       name = name:gsub("/", ".")
-      local init = name:match("(.*)%.init$")
-      if init then
-         name = init
-      end
    else
       name = file:match("(.*)%."..cfg.lib_extension.."$")
       if name then
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua
index 537adf94..a8a9cfdf 100644
--- a/src/luarocks/loader.lua
+++ b/src/luarocks/loader.lua
@@ -159,6 +159,22 @@ local function call_other_loaders(module, name, version, module_name)
    return "Failed loading module "..module.." in LuaRocks rock "..name.." "..version
 end
 
+local function add_providers(providers, entries, tree, module, filter_file_name)
+   for i, entry in ipairs(entries) do
+      local name, version = entry:match("^([^/]*)/(.*)$")
+      local file_name = tree.manifest.repository[name][version][1].modules[module]
+      if type(file_name) ~= "string" then
+         error("Invalid data in manifest file for module "..tostring(module).." (invalid data for "..tostring(name).." "..tostring(version)..")")
+      end
+      file_name = filter_file_name(file_name, name, version, tree.tree, i)
+      if loader.context[name] == version then
+         return name, version, file_name
+      end
+      version = vers.parse_version(version)
+      table.insert(providers, {name = name, version = version, module_name = file_name, tree = tree})
+   end
+end
+
 --- Search for a module in the rocks trees
 -- @param module string: module name (eg. "socket.core")
 -- @param filter_file_name function(string, string, string, string, number):
@@ -180,21 +196,16 @@ local function select_module(module, filter_file_name)
    end
 
    local providers = {}
+   local initmodule
    for _, tree in ipairs(loader.rocks_trees) do
       local entries = tree.manifest.modules[module]
       if entries then
-         for i, entry in ipairs(entries) do
-            local name, version = entry:match("^([^/]*)/(.*)$")
-            local file_name = tree.manifest.repository[name][version][1].modules[module]
-            if type(file_name) ~= "string" then
-               error("Invalid data in manifest file for module "..tostring(module).." (invalid data for "..tostring(name).." "..tostring(version)..")")
-            end
-            file_name = filter_file_name(file_name, name, version, tree.tree, i)
-            if loader.context[name] == version then
-               return name, version, file_name
-            end
-            version = vers.parse_version(version)
-            table.insert(providers, {name = name, version = version, module_name = file_name, tree = tree})
+         add_providers(providers, entries, tree, module, filter_file_name)
+      else
+         initmodule = initmodule or module .. ".init"
+         entries = tree.manifest.modules[initmodule]
+         if entries then
+            add_providers(providers, entries, tree, initmodule, filter_file_name)
          end
       end
    end
-- 
cgit v1.2.3-55-g6feb