Skip to content

Commit

Permalink
fix(plugins): fix failure on repeated installation
Browse files Browse the repository at this point in the history
When a plugin or flavor is first installed and later updated, the
link to the plugin or flavor's directory is already in place. This
caused an error when the plugin or flavor was installed again (updated).

Now, reinstalling checks if the symlink is already in place and does
nothing if it is.
  • Loading branch information
mikavilpas committed Jun 3, 2024
1 parent 61e5c95 commit 3df04c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 18 additions & 1 deletion lua/yazi/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,28 @@ end
---@param to string
---@return YaziSpecInstallationResultSuccess | YaziSpecInstallationResultFailure
function M.symlink(spec, to)
-- Check if the symlink already exists, which will happen on repeated calls
local existing_stat = vim.uv.fs_lstat(to)
if
existing_stat
and existing_stat.type == 'link'
and vim.uv.fs_readlink(to) == spec.dir
then
---@type YaziSpecInstallationResultSuccess
local result = {
message = 'yazi.nvim: already installed ' .. spec.name,
from = spec.dir,
to = to,
}
-- don't notify about this as it's a common case
return result
end

local dir = vim.uv.fs_stat(spec.dir)
if dir == nil or dir.type ~= 'directory' then
---@type YaziSpecInstallationResultFailure
local result = {
error = 'yazi plugin/flavor directory does not exist',
error = 'source directory does not exist',
from = spec.dir,
message = 'yazi.nvim: failed to install',
}
Expand Down
20 changes: 16 additions & 4 deletions tests/yazi/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ describe('installing a plugin', function()
name = 'test-plugin-2',
}, { yazi_dir = yazi_dir })

assert.is_equal(
result.error,
'yazi plugin/flavor directory does not exist'
)
assert.is_equal(result.error, 'source directory does not exist')
assert.is_equal(result.from, plugin_dir)
end)
end)
Expand All @@ -74,4 +71,19 @@ describe('installing a plugin', function()
assert.are.same(flavor_dir, symlink)
end)
end)

describe('symlink', function()
it("doesn't complain if the symlink already exists", function()
local source = vim.fs.joinpath(base_dir, 'source-dir')
local target = vim.fs.joinpath(base_dir, 'target-dir')

vim.fn.mkdir(source)

local result = plugin.symlink({ name = 'source', dir = source }, target)
assert.are.same(result.error, nil)

local result2 = plugin.symlink({ name = 'source', dir = source }, target)
assert.are.same(result2.error, nil)
end)
end)
end)

0 comments on commit 3df04c4

Please sign in to comment.