mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user