From 0127a150db131fbc2d642ada2160582fd45a688b Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 8 Oct 2026 16:33:19 +0800 Subject: [PATCH] fix(extras): only treat an actual link as linking to the source ResolveLinkTarget falls back to EvalSymlinks, which also resolves a real directory. A real directory reached through a symlinked ancestor of the source then counted as a link to it, so --remove-target --prune failed with "not a symlink" instead of keeping the directory. LinksTo now requires the path itself to be a symlink or junction. Refs #491 --- internal/sync/extras.go | 6 +++++- internal/sync/extras_prune_target_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/sync/extras.go b/internal/sync/extras.go index b70998044..0a8d9a88b 100644 --- a/internal/sync/extras.go +++ b/internal/sync/extras.go @@ -633,7 +633,11 @@ func ClearExtraTarget(file, sourceDir, targetPath, as, mode string, managedFiles // LinksTo reports whether link is a symlink to dir, comparing the link text // too, so a link whose folder has since moved or been deleted still counts. func LinksTo(link, dir string) bool { - // ResolveLinkTarget also handles Windows junctions, which Readlink can't read. + // ResolveLinkTarget also resolves a real directory, so check the link itself + // first; it handles Windows junctions, which Readlink can't read. + if !utils.IsSymlinkOrJunction(link) { + return false + } dest, err := utils.ResolveLinkTarget(link) if err != nil { return false diff --git a/internal/sync/extras_prune_target_test.go b/internal/sync/extras_prune_target_test.go index 70a0d18e0..7ba2ffcd4 100644 --- a/internal/sync/extras_prune_target_test.go +++ b/internal/sync/extras_prune_target_test.go @@ -149,3 +149,19 @@ func TestClearExtraTarget_SymlinkTargetThatCannotBeInspectedReportsError(t *test t.Errorf("kept = %v, errs = %v; want an error", kept, errs) } } + +func TestClearExtraTarget_KeepsRealDirectoryThatResolvesToSource(t *testing.T) { + root := t.TempDir() + src := filepath.Join(root, "docs") + os.MkdirAll(src, 0755) + alias := filepath.Join(t.TempDir(), "alias") + if err := os.Symlink(root, alias); err != nil { + t.Fatal(err) + } + + _, kept, errs := ClearExtraTarget("", src, filepath.Join(alias, "docs"), "", "symlink", nil) + + if !kept || len(errs) > 0 { + t.Errorf("kept = %v, errs = %v; want the real directory kept", kept, errs) + } +}