From 3316c9073e23bfe93dc009c104f71a95b95060f7 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 7 Apr 2026 16:18:13 +0800 Subject: [PATCH] fix(sync): resolve relative readlink against link parent, check os.Remove errors filepath.Abs(dest) on a relative readlink result resolves against CWD, not the symlink's parent directory. This broke the 'already correct' check when comparing relative symlinks to absolute source paths. Add resolveReadlink() helper that joins relative targets with filepath.Dir(linkPath) before calling filepath.Abs. Also check os.Remove errors in all reformat paths instead of ignoring them silently. Addresses PR #122 review feedback. --- internal/sync/extras.go | 12 ++++++++---- internal/sync/relative.go | 9 +++++++++ internal/sync/relative_test.go | 30 ++++++++++++++++++++++++++++++ internal/sync/sync.go | 8 ++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/internal/sync/extras.go b/internal/sync/extras.go index 248ece006..8dd95f4df 100644 --- a/internal/sync/extras.go +++ b/internal/sync/extras.go @@ -104,7 +104,7 @@ func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, pro // Already a symlink — check if correct dest, readErr := os.Readlink(targetPath) if readErr == nil { - absDest, _ := filepath.Abs(dest) + absDest := resolveReadlink(dest, targetPath) if absDest == absSrc { relative := shouldUseRelative(projectRoot, absSrc, targetPath) if !linkNeedsReformat(dest, relative) { @@ -116,7 +116,9 @@ func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, pro result.Synced = 1 return result, nil } - os.Remove(targetPath) + if err := os.Remove(targetPath); err != nil { + return nil, fmt.Errorf("failed to remove symlink for reformat: %w", err) + } if err := createLink(targetPath, absSrc, relative); err != nil { return nil, fmt.Errorf("failed to reformat directory symlink: %w", err) } @@ -130,7 +132,9 @@ func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, pro return result, nil } if !dryRun { - os.Remove(targetPath) + if err := os.Remove(targetPath); err != nil { + return nil, fmt.Errorf("failed to remove wrong symlink: %w", err) + } } } else { // Real file/dir @@ -242,7 +246,7 @@ func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative boo if mode == "merge" && isSymlink { dest, readErr := os.Readlink(tgtFile) if readErr == nil { - absDest, _ := filepath.Abs(dest) + absDest := resolveReadlink(dest, tgtFile) if absDest == srcFile { if !linkNeedsReformat(dest, relative) { return 1, 0, nil diff --git a/internal/sync/relative.go b/internal/sync/relative.go index 5b96bae4f..e5519eb33 100644 --- a/internal/sync/relative.go +++ b/internal/sync/relative.go @@ -24,6 +24,15 @@ func shouldUseRelative(projectRoot, sourcePath, targetPath string) bool { return srcUnder && tgtUnder } +// resolveReadlink converts a raw os.Readlink result to an absolute path, +// resolving relative targets against the link's parent directory (not CWD). +func resolveReadlink(dest, linkPath string) string { + if filepath.IsAbs(dest) { + return dest + } + return filepath.Clean(filepath.Join(filepath.Dir(linkPath), dest)) +} + // linkNeedsReformat returns true if dest (the raw os.Readlink result) // uses the wrong format (relative vs absolute) for the desired mode. func linkNeedsReformat(dest string, wantRelative bool) bool { diff --git a/internal/sync/relative_test.go b/internal/sync/relative_test.go index eb6ff0347..ddc785fdd 100644 --- a/internal/sync/relative_test.go +++ b/internal/sync/relative_test.go @@ -78,6 +78,36 @@ func TestShouldUseRelative_CleansPaths(t *testing.T) { } } +func TestResolveReadlink(t *testing.T) { + tests := []struct { + name string + dest string + linkPath string + want string + }{ + { + "absolute dest unchanged", + "/abs/source/skill", + "/project/.claude/skills/skill", + "/abs/source/skill", + }, + { + "relative dest resolved from link parent", + "../../.skillshare/skills/skill", + "/project/.claude/skills/skill", + "/project/.skillshare/skills/skill", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := resolveReadlink(tt.dest, tt.linkPath) + if got != tt.want { + t.Errorf("resolveReadlink(%q, %q) = %q, want %q", tt.dest, tt.linkPath, got, tt.want) + } + }) + } +} + func TestLinkNeedsReformat(t *testing.T) { tests := []struct { name string diff --git a/internal/sync/sync.go b/internal/sync/sync.go index bbb8a0939..806e9672f 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -253,7 +253,9 @@ func SyncTarget(name string, target config.TargetConfig, sourcePath string, dryR fmt.Fprintf(DiagOutput, "[dry-run] Would reformat symlink: %s\n", sc.Path) return nil } - os.Remove(sc.Path) + if err := os.Remove(sc.Path); err != nil { + return fmt.Errorf("failed to remove symlink for reformat: %w", err) + } return CreateSymlink(sc.Path, sourcePath, projectRoot) case StatusNotExist: @@ -562,7 +564,9 @@ func SyncTargetMergeWithSkills(name string, target config.TargetConfig, allSkill } // Correct target but wrong format (abs↔rel) — recreate if !dryRun { - os.Remove(targetSkillPath) + if err := os.Remove(targetSkillPath); err != nil { + return nil, fmt.Errorf("failed to remove link for reformat: %w", err) + } if err := createLink(targetSkillPath, skill.SourcePath, relative); err != nil { return nil, fmt.Errorf("failed to reformat link for %s: %w", activeName, err) }