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) + } +}