Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luarocks is not executable because .rocks directory is installed in temporary dir instead of plugin root #24

Open
gut11 opened this issue May 13, 2024 · 0 comments

Comments

@gut11
Copy link

gut11 commented May 13, 2024

When manually running nvim -l build.lua from within the plugin root directory, the .rocks directory ends up in /run/user/1000/luarocks-* instead of within the plugin root directory.

I tested this by I adding a print statement to paths.lua on the plugin_path variable:

local utils = require("luarocks-nvim.utils")

local plugin_path = utils.get_plugin_path()
print(plugin_path) <-- HERE
local rocks_path = utils.combine_paths(plugin_path, ".rocks")
local lib_extension = utils.is_win() and "?.dll" or "?.so"

When executing the build directly from the plugin root, the plugin path was printed as ".", whereas when run from inside Neovim, it was printed as the full path to the plugin root directory.

I wrote some code to fix the issue and it worked here.

-- utils.lua
local utils = {}

function utils.is_win()
	return vim.loop.os_uname().sysname:lower():find("windows")
end

function utils.get_path_separator()
	if utils.is_win() then
		return "\\"
	end
	return "/"
end

function utils.combine_paths(...)
	return table.concat({ ... }, utils.get_path_separator())
end

local function get_current_dir()
	if utils.is_win() then
		return os.getenv("CD")
	else
		return os.getenv("PWD")
	end
end

local function change_relative_to_absolute_path(path)
	local string_without_dot = path:sub(2)
	local absolute_path = get_current_dir()
	return (absolute_path .. string_without_dot)
end

local function path_is_relative(path)
	if path:sub(1,1) == "." and (path:sub(2,2) == "/" or path:sub(2,2) == "\\") then
		return true
	else
		return false
	end
end


function utils.get_plugin_path()
	local str = debug.getinfo(2, "S").source:sub(2)
	if utils.is_win() then
		str = str:gsub("/", "\\")
	end
	if path_is_relative(str) then
		str = change_relative_to_absolute_path(str)
	end
	return vim.fn.fnamemodify(str:match("(.*" .. utils.get_path_separator() .. ")"), ":h:h:h")
end

return utils

All I did was checking if the first char in the path is a '.' and if it is change the dot for the $PWD or CD for windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant