From effa0ffeb845702d2f49c2b0c5d71f9075b61873 Mon Sep 17 00:00:00 2001 From: Denzel <> Date: Thu, 9 Apr 2026 22:35:02 +0200 Subject: [PATCH] fix: re-create softlink when target differs --- lua/cmake-tools/utils.lua | 43 +++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/lua/cmake-tools/utils.lua b/lua/cmake-tools/utils.lua index 8afc38f..a9ac788 100644 --- a/lua/cmake-tools/utils.lua +++ b/lua/cmake-tools/utils.lua @@ -86,27 +86,40 @@ end function utils.copyfile(src, target) if utils.file_exists(src) then -- if we don't always use terminal - local cmd = "exec " - .. "'!cmake -E copy " - .. utils.shell_quote(src) - .. " " - .. utils.shell_quote(target) - .. "'" + local cmd = table.concat({ + "exec", + "'!cmake -E copy", + utils.shell_quote(src), + utils.shell_quote(target) .. "'", + }, " ") vim.cmd(cmd) end end function utils.softlink(src, target) - if utils.file_exists(src) and not utils.file_exists(target) then - -- if we don't always use terminal - local cmd = "exec " - .. "'!cmake -E create_symlink " - .. utils.shell_quote(src) - .. " " - .. utils.shell_quote(target) - .. "'" - vim.cmd(cmd) + if not utils.file_exists(src) then + return + end + + local stat = vim.loop.fs_lstat(target) + if stat then + if stat.type == "link" then + if vim.loop.fs_readlink(target) == src then + return + end + else + -- target is a regular file, remove it first + os.remove(target) + end end + + local cmd = table.concat({ + "exec", + "'!cmake -E create_symlink", + utils.shell_quote(src), + utils.shell_quote(target) .. "'", + }, " ") + vim.cmd(cmd) end function utils.shell_quote(str)