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
This commit is contained in:
Willie
2026-10-08 16:33:19 +08:00
parent e5dcaa5f64
commit 0127a150db
2 changed files with 21 additions and 1 deletions
+5 -1
View File
@@ -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
+16
View File
@@ -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)
}
}