From ec78ef6b0f4dc866a803b4e6d23ad16db12934d6 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 12 Jul 2019 17:38:00 -0300 Subject: which: search in package.path and cpath as well Also, add some tests. --- src/luarocks/cmd/which.lua | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/luarocks/cmd/which.lua b/src/luarocks/cmd/which.lua index 89e6db56..56db5254 100644 --- a/src/luarocks/cmd/which.lua +++ b/src/luarocks/cmd/which.lua @@ -6,6 +6,7 @@ local which_cmd = {} local loader = require("luarocks.loader") local cfg = require("luarocks.core.cfg") local util = require("luarocks.util") +local fs = require("luarocks.fs") which_cmd.help_summary = "Tell which file corresponds to a given module name." which_cmd.help_arguments = "" @@ -21,12 +22,26 @@ function which_cmd.command(_, modname) return nil, "Missing module name. " .. util.see_help("which") end local pathname, rock_name, rock_version = loader.which(modname) - if not pathname then - return nil, "Module '" .. modname .. "' not found by luarocks.loader." + + if pathname then + util.printout(pathname) + util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") + return true + end + + local modpath = modname:gsub("%.", "/") + for _, v in ipairs({"path", "cpath"}) do + for p in package[v]:gmatch("([^;]+)") do + local pathname = p:gsub("%?", modpath) + if fs.exists(pathname) then + util.printout(pathname) + util.printout("(found directly via package." .. v .. " -- not installed as a rock?)") + return true + end + end end - util.printout(pathname) - util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") - return true + + return nil, "Module '" .. modname .. "' not found." end return which_cmd -- cgit v1.2.3-55-g6feb