From 74960d73342da4408aa59373cc3dcd898c7d1eb1 Mon Sep 17 00:00:00 2001
From: Hisham Muhammad <hisham@gobolinux.org>
Date: Tue, 14 Jan 2020 18:05:46 -0300
Subject: cmd: fallback to actual PATH search

---
 src/luarocks/cmd.lua  | 49 ++++++++++++++++++++++++++++++++++---------------
 src/luarocks/util.lua |  8 ++++----
 2 files changed, 38 insertions(+), 19 deletions(-)

(limited to 'src')

diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua
index b43faaba..7866cd3c 100644
--- a/src/luarocks/cmd.lua
+++ b/src/luarocks/cmd.lua
@@ -172,6 +172,30 @@ local function die(message, exitcode)
    os.exit(exitcode or cmd.errorcodes.UNSPECIFIED)
 end
 
+local function search_lua_in_path(lua_version, verbose)
+   local path_sep = (package.config:sub(1, 1) == "\\" and ";" or ":")
+   local all_tried = {}
+   for bindir in os.getenv("PATH"):gmatch("[^"..path_sep.."]+") do
+      local parentdir = bindir:gsub("[\\/][^\\/]+[\\/]?$", "")
+      local detected, tried = util.find_lua(dir.path(parentdir), lua_version)
+      if detected then
+         return detected
+      else
+         table.insert(all_tried, tried)
+      end
+      detected = util.find_lua(bindir, lua_version)
+      if detected then
+         return detected
+      else
+         table.insert(all_tried, tried)
+      end
+   end
+   return nil, "Could not find " ..
+               (lua_version and "Lua " .. lua_version or "Lua") ..
+               " in PATH." ..
+               (verbose and " Tried:\n" .. table.concat(all_tried, "\n") or "")
+end
+
 local init_config
 do
    local detect_config_via_args
@@ -244,17 +268,9 @@ do
          end
       
          if lua_version then
-            local path_sep = (package.config:sub(1, 1) == "\\" and ";" or ":")
-            for bindir in os.getenv("PATH"):gmatch("[^"..path_sep.."]+") do
-               local parentdir = bindir:gsub("[\\/][^\\/]+[\\/]?$", "")
-               local detected = util.find_lua(dir.path(parentdir), lua_version)
-               if detected then
-                  return detected
-               end
-               detected = util.find_lua(bindir, lua_version)
-               if detected then
-                  return detected
-               end
+            local detected = search_lua_in_path(lua_version)
+            if detected then
+               return detected
             end
             return {
                lua_version = lua_version,
@@ -535,15 +551,18 @@ function cmd.run_command(description, commands, external_namespace, ...)
 
    -- if the Lua interpreter wasn't explicitly found before cfg.init,
    -- try again now.
+   local tried
    if not lua_found then
       if cfg.variables.LUA_DIR then
-         lua_found = util.find_lua(cfg.variables.LUA_DIR, cfg.lua_version)
+         lua_found, tried = util.find_lua(cfg.variables.LUA_DIR, cfg.lua_version, args.verbose)
+      else
+         lua_found, tried = search_lua_in_path(cfg.lua_version, args.verbose)
       end
    end
 
-   if not lua_found then
-      util.warning("Could not find a Lua " .. cfg.lua_version .. " interpreter in your PATH. " ..
-                   "Modules may not install with the correct configurations. " ..
+   if not lua_found and args.command ~= "config" and args.command ~= "help" then
+      util.warning(tried ..
+                   "\nModules may not install with the correct configurations. " ..
                    "You may want to specify the path prefix to your build " ..
                    "of Lua " .. cfg.lua_version .. " using --lua-dir")
    end
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua
index ae198daf..8eccace9 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util.lua
@@ -481,7 +481,7 @@ do
          end
       end
 
-      find_lua_bindir = function(prefix, luaver)
+      find_lua_bindir = function(prefix, luaver, verbose)
          local names = {}
          if luaver then
             insert_lua_variants(names, luaver)
@@ -517,13 +517,13 @@ do
                         and ("Lua " .. luaver .. " interpreter")
                         or  "Lua interpreter"
          return nil, interp .. " not found at " .. prefix .. "\n" ..
-                     "Tried:\t" .. table.concat(tried, "\n\t")
+                     (verbose and "Tried:\t" .. table.concat(tried, "\n\t") or "")
       end
    end
 
-   function util.find_lua(prefix, luaver)
+   function util.find_lua(prefix, luaver, verbose)
       local lua_interpreter, bindir
-      lua_interpreter, bindir, luaver = find_lua_bindir(prefix, luaver)
+      lua_interpreter, bindir, luaver = find_lua_bindir(prefix, luaver, verbose)
       if not lua_interpreter then
          return nil, bindir
       end
-- 
cgit v1.2.3-55-g6feb