From faa7c7560645fde443ad022214c0bb1fbea6953f Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 19 Feb 2024 18:10:34 -0300 Subject: improve and simplify Lua interpreter search * do not proceed with commands if interpreter is not found * begin retiring LUA_DIR and LUA_BINDIR, and promote LUA as the main way to setup the interpreter location (from which we derive the rest) --- spec/cmd_spec.lua | 2 +- src/luarocks/cmd.lua | 74 ++++++++++++++++++++++++++--------------------- src/luarocks/cmd/init.lua | 4 --- src/luarocks/core/cfg.lua | 2 +- src/luarocks/deps.lua | 5 ++-- src/luarocks/util.lua | 4 +++ 6 files changed, 50 insertions(+), 41 deletions(-) diff --git a/spec/cmd_spec.lua b/spec/cmd_spec.lua index fe27ab1b..5d6e729b 100644 --- a/spec/cmd_spec.lua +++ b/spec/cmd_spec.lua @@ -60,7 +60,7 @@ describe("LuaRocks command line #integration", function() it("warns but continues if given an invalid version", function() local output = run.luarocks("--lua-version=1.0") - assert.match("Warning: Lua 1.0 interpreter not found", output, 1, true) + assert.match("LUA *: %(not found%)", output) assert.match("Version%s*:%s*1.0", output) end) diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 3067f4ce..30293d6b 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -340,14 +340,18 @@ Variables: ]] -local function get_status(status) - return status and "ok" or "not found" +local lua_example = package.config:sub(1, 1) == "\\" + and "" + or "" + +local function show_status(file, status) + return (file and file .. " " or "") .. (status and "(ok)" or "(not found)") end -local function use_to_fix_location(key) +local function use_to_fix_location(key, what) local buf = " ****************************************\n" buf = buf .. " Use the command\n\n" - buf = buf .. " luarocks config " .. key .. " \n\n" + buf = buf .. " luarocks config " .. key .. " " .. (what or "") .. "\n\n" buf = buf .. " to fix the location\n" buf = buf .. " ****************************************\n" return buf @@ -358,8 +362,6 @@ local function get_config_text(cfg) -- luacheck: ignore 431 local libdir_ok = deps.check_lua_libdir(cfg.variables) local incdir_ok = deps.check_lua_incdir(cfg.variables) - local bindir_ok = cfg.variables.LUA_BINDIR and fs.exists(cfg.variables.LUA_BINDIR) - local luadir_ok = cfg.variables.LUA_DIR and fs.exists(cfg.variables.LUA_DIR) local lua_ok = cfg.variables.LUA and fs.exists(cfg.variables.LUA) local buf = "Configuration:\n" @@ -368,31 +370,29 @@ local function get_config_text(cfg) -- luacheck: ignore 431 if cfg.luajit_version then buf = buf.." LuaJIT : "..cfg.luajit_version.."\n" end - buf = buf.." Interpreter: "..(cfg.variables.LUA or "").." ("..get_status(lua_ok)..")\n" - buf = buf.." LUA_DIR : "..(cfg.variables.LUA_DIR or "").." ("..get_status(luadir_ok)..")\n" + buf = buf.." LUA : "..show_status(cfg.variables.LUA, lua_ok).."\n" if not lua_ok then - buf = buf .. use_to_fix_location("lua_dir") + buf = buf .. use_to_fix_location("variables.LUA", lua_example) end - buf = buf.." LUA_BINDIR : "..(cfg.variables.LUA_BINDIR or "").." ("..get_status(bindir_ok)..")\n" - buf = buf.." LUA_INCDIR : "..(cfg.variables.LUA_INCDIR or "").." ("..get_status(incdir_ok)..")\n" + buf = buf.." LUA_INCDIR : "..show_status(cfg.variables.LUA_INCDIR, incdir_ok).."\n" if lua_ok and not incdir_ok then buf = buf .. use_to_fix_location("variables.LUA_INCDIR") end - buf = buf.." LUA_LIBDIR : "..(cfg.variables.LUA_LIBDIR or "").." ("..get_status(libdir_ok)..")\n" + buf = buf.." LUA_LIBDIR : "..show_status(cfg.variables.LUA_LIBDIR, libdir_ok).."\n" if lua_ok and not libdir_ok then buf = buf .. use_to_fix_location("variables.LUA_LIBDIR") end buf = buf.."\n Configuration files:\n" local conf = cfg.config_files - buf = buf.." System : "..fs.absolute_name(conf.system.file).." ("..get_status(conf.system.found)..")\n" + buf = buf.." System : "..show_status(fs.absolute_name(conf.system.file), conf.system.found).."\n" if conf.user.file then - buf = buf.." User : "..fs.absolute_name(conf.user.file).." ("..get_status(conf.user.found)..")\n" + buf = buf.." User : "..show_status(fs.absolute_name(conf.user.file), conf.user.found).."\n" else buf = buf.." User : disabled in this LuaRocks installation.\n" end if conf.project then - buf = buf.." Project : "..fs.absolute_name(conf.project.file).." ("..get_status(conf.project.found)..")\n" + buf = buf.." Project : "..show_status(fs.absolute_name(conf.project.file), conf.project.found).."\n" end buf = buf.."\n Rocks trees in use: \n" for _, tree in ipairs(cfg.rocks_trees) do @@ -638,33 +638,35 @@ function cmd.run_command(description, commands, external_namespace, ...) cfg.variables.LUA_INCDIR = nil cfg.variables.LUA_LIBDIR = nil end + else + cfg.variables.LUA = nil + cfg.variables.LUA_DIR = nil + cfg.variables.LUA_BINDIR = nil + cfg.variables.LUA_INCDIR = nil + cfg.variables.LUA_LIBDIR = nil end end if lua_found then assert(cfg.variables.LUA) else - if args.command ~= "config" and args.command ~= "help" then - util.warning(tried .. - "\nModules may not install with the correct configurations. " .. - "You may want to configure the path prefix to your build " .. - "of Lua " .. cfg.lua_version .. " using\n\n" .. - " luarocks config --local lua_dir \n") - end - -- Fallback producing _some_ Lua configuration based on the running interpreter. -- Most likely won't produce correct results when running from the standalone binary, -- so eventually we need to drop this and outright fail if Lua is not found -- or explictly configured if not cfg.variables.LUA then local first_arg = get_first_arg() - cfg.variables.LUA_DIR = dir.dir_name(fs.absolute_name(first_arg)) - cfg.variables.LUA_BINDIR = cfg.variables.LUA_DIR - local exe = fs.base_name(first_arg) + local bin_dir = dir.dir_name(fs.absolute_name(first_arg)) + local exe = dir.base_name(first_arg) exe = exe:match("rocks") and ("lua" .. (cfg.arch:match("win") and ".exe" or "")) or exe - cfg.variables.LUA = dir.path(cfg.variables.LUA_BINDIR, exe) - cfg.variables.LUA_INCDIR = nil - cfg.variables.LUA_LIBDIR = nil + local full_path = dir.path(bin_dir, exe) + if util.check_lua_version(full_path, cfg.lua_version) then + cfg.variables.LUA = dir.path(bin_dir, exe) + cfg.variables.LUA_DIR = bin_dir:gsub("[/\\]bin[/\\]?$", "") + cfg.variables.LUA_BINDIR = bin_dir + cfg.variables.LUA_INCDIR = nil + cfg.variables.LUA_LIBDIR = nil + end end end @@ -718,10 +720,16 @@ function cmd.run_command(description, commands, external_namespace, ...) os.exit(cmd.errorcodes.OK) end - if not cfg.variables["LUA_BINDIR"] then - return nil, "LUA_BINDIR not configured.\n" .. - "Please configure the location of the Lua interpreter with:\n" .. - " luarocks config variables.LUA_BINDIR " + if not cfg.variables["LUA"] and args.command ~= "config" and args.command ~= "help" then + local flag = (not cfg.project_tree) + and "--local " + or "" + if args.lua_version then + flag = "--lua-version=" .. args.lua_version .. " " .. flag + end + die((tried or "Lua interpreter not found.") .. + "\nPlease set your Lua interpreter with:\n\n" .. + " luarocks " .. flag.. "config variables.LUA " .. lua_example .. "\n") end local cmd_mod = cmd_modules[args.command] diff --git a/src/luarocks/cmd/init.lua b/src/luarocks/cmd/init.lua index 20aa97a1..fabd92a1 100644 --- a/src/luarocks/cmd/init.lua +++ b/src/luarocks/cmd/init.lua @@ -125,10 +125,6 @@ function init.command(args) util.title("Initializing project '" .. args.name .. "' for Lua " .. cfg.lua_version .. " ...") - util.printout("Checking your Lua installation ...") - if not cfg.lua_found then - return nil, "Lua installation is not found." - end local ok, err = deps.check_lua_incdir(cfg.variables) if not ok then return nil, err diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 2e144c62..a420bb44 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -787,7 +787,7 @@ function cfg.init(detected, warning) -- if only cfg.variables.LUA is given in config files, -- derive LUA_BINDIR and LUA_DIR from them. if cfg.variables.LUA and not cfg.variables.LUA_BINDIR then - cfg.variables.LUA_BINDIR = cfg.variables.LUA:gsub("^(.*)[/\\][^/\\]*$") + cfg.variables.LUA_BINDIR = cfg.variables.LUA:match("^(.*)[/\\][^/\\]*$") if not cfg.variables.LUA_DIR then cfg.variables.LUA_DIR = cfg.variables.LUA_BINDIR:gsub("[/\\]bin$", "") or cfg.variables.LUA_BINDIR end diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 2680b64b..16631c58 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -706,7 +706,7 @@ local function lua_h_exists(d, luaver) return nil, "Lua header found at " .. d .. " does not match Lua version " .. luaver .. ". You may want to override this by configuring LUA_INCDIR.", "dependency", 2 end - return nil, "Failed finding Lua header files. You may need to install them or configure LUA_INCDIR.", "dependency", 1 + return nil, "Failed finding Lua header files (searched at " .. d .. "). You may need to install them or configure LUA_INCDIR.", "dependency", 1 end local function find_lua_incdir(prefix, luaver, luajitver) @@ -763,7 +763,7 @@ function deps.check_lua_incdir(vars) return nil, err end - return nil, "Failed finding Lua header files. You may need to install them or configure LUA_INCDIR.", "dependency" + return nil, "Failed finding Lua headers; neither LUA_DIR or LUA_INCDIR are set. You may need to install them or configure LUA_INCDIR.", "dependency" end function deps.check_lua_libdir(vars) @@ -789,6 +789,7 @@ function deps.check_lua_libdir(vars) } if ljv then table.insert(libnames, 1, "luajit-" .. cfg.lua_version) + table.insert(libnames, 2, "luajit") end local cache = {} local save_LUA_INCDIR = vars.LUA_INCDIR diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 902656fd..de9157fc 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -434,6 +434,10 @@ do end cfg.cache.luajit_version_checked = true + if not cfg.variables.LUA then + return nil + end + local ljv if cfg.lua_version == "5.1" then -- Ignores extra version info for custom builds, e.g. "LuaJIT 2.1.0-beta3 some-other-version-info" -- cgit v1.2.3-55-g6feb