From 1b9fa01eb0e673f01bb8dbe35b7a52b0d0b5f975 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Tue, 15 Oct 2024 19:08:53 +0200 Subject: [PATCH] fix: more robust initialisation of site symlinks (#558) --- lua/rocks/adapter.lua | 12 ++++++++++-- lua/rocks/runtime.lua | 26 ++++++-------------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lua/rocks/adapter.lua b/lua/rocks/adapter.lua index f0c05be..ba4f63e 100644 --- a/lua/rocks/adapter.lua +++ b/lua/rocks/adapter.lua @@ -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) @@ -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) diff --git a/lua/rocks/runtime.lua b/lua/rocks/runtime.lua index 4f999f6..70d0066 100644 --- a/lua/rocks/runtime.lua +++ b/lua/rocks/runtime.lua @@ -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 @@ -30,19 +31,6 @@ 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 @@ -50,14 +38,12 @@ 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)