From 3612c269f3cd6397c883799b0bea549d5a7d1512 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 17 Feb 2024 01:01:12 -0300 Subject: fix: install build_dependencies on the correct Lua version When installing build dependencies, do so for the Lua version on which the LuaRocks program is actually running, and not the one selected by the user via config or `--lua-version`. To do that, we temporarily flip the user-selected Lua version. It's a hacky approach because we have to do this by flipping a bunch of global config settings, and we may be missing some entries. This definitely needs a better solution, but this is what can be done somewhat easily in the current architecture. A full solution needs to address have several complications (e.g. if you have a build dependency that needs to be compiled, it will require C headers for another version of LuaRocks, and the binary might be compiled with a different Lua version than the one the developer has set up in their machine.) --- src/luarocks/build.lua | 30 ++++++++++++++++++++++++++++++ src/luarocks/path.lua | 13 +++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index e545d390..d5c474ca 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -129,7 +129,37 @@ local function process_dependencies(rockspec, opts) if not opts.build_only_deps then if next(rockspec.build_dependencies) then + + local user_lua_version = cfg.lua_version + local running_lua_version = _VERSION:sub(5) + + if running_lua_version ~= user_lua_version then + -- Temporarily flip the user-selected Lua version, + -- so that we install build dependencies for the + -- Lua version on which the LuaRocks program is running. + + -- HACK: we have to do this by flipping a bunch of + -- global config settings, and this list may not be complete. + cfg.lua_version = running_lua_version + cfg.lua_modules_path = cfg.lua_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) + cfg.lib_modules_path = cfg.lib_modules_path:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) + cfg.rocks_subdir = cfg.rocks_subdir:gsub(user_lua_version:gsub("%.", "%%."), running_lua_version) + path.use_tree(cfg.root_dir) + end + local ok, err, errcode = deps.fulfill_dependencies(rockspec, "build_dependencies", "all", opts.verify) + + path.add_to_package_paths(cfg.root_dir) + + if running_lua_version ~= user_lua_version then + -- flip the settings back + cfg.lua_version = user_lua_version + cfg.lua_modules_path = cfg.lua_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) + cfg.lib_modules_path = cfg.lib_modules_path:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) + cfg.rocks_subdir = cfg.rocks_subdir:gsub(running_lua_version:gsub("%.", "%%."), user_lua_version) + path.use_tree(cfg.root_dir) + end + if err then return nil, err, errcode end diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index a9f04ef0..19657c83 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua @@ -219,13 +219,14 @@ function path.use_tree(tree) cfg.deploy_bin_dir = path.deploy_bin_dir(tree) cfg.deploy_lua_dir = path.deploy_lua_dir(tree) cfg.deploy_lib_dir = path.deploy_lib_dir(tree) +end - -- TODO Do we want LuaRocks itself to use whatever tree is in use? - -- package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" - -- .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" - -- .. package.path - -- package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" - -- .. package.cpath +function path.add_to_package_paths(tree) + package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" + .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" + .. package.path + package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" + .. package.cpath end --- Get the namespace of a locally-installed rock, if any. -- cgit v1.2.3-55-g6feb