Skip to content

Commit

Permalink
fix: more robust initialisation of site symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Oct 15, 2024
1 parent 2bb661b commit f5eef47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
12 changes: 10 additions & 2 deletions lua/rocks/adapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ local function validate_site_symlinks_async()
end
end

---@param rock Rock
---@param rock RockSpec | Rock
---@return string
local function get_rock_dir(rock)
return vim.fs.joinpath(config.rocks_path, "lib", "luarocks", "rocks-5.1", rock.name)
Expand All @@ -126,8 +126,16 @@ local function init_site_symlink_async(rock)
end
end

---Check if a site symlink exists for a rock
---@param rock RockSpec | Rock
---@return boolean exists
function adapter.has_site_symlink(rock)
local symlink_dir_path = vim.fs.joinpath(site_link_dir, rock.name)
return vim.uv.fs_stat(symlink_dir_path) ~= nil
end

---Synchronously initialise a site symlink
---@param rock Rock
---@param rock RockSpec | Rock
---@return boolean created
function adapter.init_site_symlink_sync(rock)
local rock_dir = get_rock_dir(rock)
Expand Down
26 changes: 6 additions & 20 deletions lua/rocks/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local runtime = {}

local constants = require("rocks.constants")
local log = require("rocks.log")
local adapter = require("rocks.adapter")

---@alias rock_pattern "*" | rock_name

Expand All @@ -30,34 +31,19 @@ local function is_not_found(err_msg)
return err_msg and err_msg:find("Directory not found in 'packpath'") ~= nil
end

---@param rock Rock
---@param opts rocks.PackaddOpts
---@param err string The previous error message
---@return boolean success
---@return string | nil error_message
local function init_site_symlink_and_retry_packadd(rock, opts, err)
local symlink_created = require("rocks.adapter").init_site_symlink_sync(rock)
if symlink_created then
return pcall(vim.cmd.packadd, { rock.name, bang = opts.bang })
end
return false, err
end

---@param rock_spec RockSpec
---@param opts? rocks.PackaddOpts
---@return boolean found
function runtime.packadd(rock_spec, opts)
opts = vim.tbl_deep_extend("force", {
bang = false,
}, opts or {})
local ok, err = pcall(vim.cmd.packadd, { rock_spec.name, bang = opts.bang })
if is_not_found(err) and rock_spec.version then
ok, err = init_site_symlink_and_retry_packadd({
name = rock_spec.name,
version = rock_spec.version, --[[ @as string ]]
}, opts, err)
if not adapter.has_site_symlink(rock_spec) and not adapter.init_site_symlink_sync(rock_spec) then
log.warn(("Rock %s does not appear to be installed."):format(rock_spec.name))
return false
end
if not ok and err and not is_not_found(err) then
local ok, err = pcall(vim.cmd.packadd, { rock_spec.name, bang = opts.bang })
if not ok and not is_not_found(err) then
vim.schedule(function()
vim.notify(err, vim.log.levels.ERROR)
end)
Expand Down

0 comments on commit f5eef47

Please sign in to comment.