From f29b4ac1b88ad151a7cd40aaf37db515e0331770 Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 27 Feb 2026 03:54:23 +0800 Subject: [PATCH] fix(sync): dereference directory symlinks in copy mode and update changelog Copy mode now resolves directory symlinks to concrete directories, preventing broken symlinks in targets that use file copying (e.g., Windsurf). DirChecksum follows symlinks for consistent hash comparison. DirMaxMtime disables mtime fast-path for symlink-containing skills to avoid stale cache hits. Cycle detection prevents infinite recursion. Also updates CHANGELOG v0.16.3 with previously missing entries: hub risk badge colors, batch failure output, repo-grouped cloning. --- CHANGELOG.md | 4 + internal/sync/checksum_test.go | 40 +++++++++ internal/sync/copy.go | 125 ++++++++++++++++++++-------- internal/sync/sync.go | 32 +++++++ tests/integration/sync_copy_test.go | 46 ++++++++++ 5 files changed, 213 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46be4d166..d103bccfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,13 @@ - **`diff` output redesign** — actions are now labeled by what they do (`add`, `remove`, `update`, `restore`) with a grouped summary showing counts per action; overall summary line at the end - **Web UI log stats bar** — Log page now shows a stats bar with success rate and per-command breakdown - **Hub batch install progress** — multi-skill installs from `search --hub` now show real-time git clone progress (`cloning 45%`, `resolving 67%`) instead of a static "installing..." label; only the active install is shown to keep the display compact +- **Hub risk badge colors** — risk labels in hub search results are now color-coded by severity (green for clean, yellow for low, red for critical) in both the list and detail panel +- **Hub batch failure output** — failure details are classified by type (security / ambiguous / not found) with distinct icons; long audit findings and ambiguous path lists are truncated to 3 lines with a "(+N more)" summary ### Performance - **Batch install reconcile** — config reconciliation now runs once after all installs complete instead of after each skill, eliminating O(n²) directory walks that caused batch installs of large collections to appear stuck +- **Repo-grouped cloning** — skills from the same git repo are now cloned once and installed from the shared clone, reducing network requests for multi-skill repos ### Fixed @@ -23,6 +26,7 @@ - **Oplog data loss** — rewriteEntries now checks for write errors before truncating the original file - **TUI content clipping** — detail panels in `list` and `log` TUIs now hard-wrap content and account for padding, preventing text from being clipped at panel edges - **TUI footer spacing** — list and log TUI footers have proper breathing room between action hints +- **Copy mode symlink handling** — `sync` in copy mode now dereferences directory symlinks instead of copying broken link files; prevents missing content in targets like Windsurf that use file copying ## [0.16.2] - 2026-02-26 diff --git a/internal/sync/checksum_test.go b/internal/sync/checksum_test.go index 6876e9c46..5c43aa43b 100644 --- a/internal/sync/checksum_test.go +++ b/internal/sync/checksum_test.go @@ -3,6 +3,7 @@ package sync import ( "os" "path/filepath" + "runtime" "testing" "time" ) @@ -108,6 +109,45 @@ func TestDirChecksum_NestedFiles(t *testing.T) { } } +func TestDirChecksum_SymlinkDirectory(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("symlink behavior differs on Windows") + } + + dir := t.TempDir() + realA := filepath.Join(dir, "real-a") + realB := filepath.Join(dir, "real-b") + os.MkdirAll(realA, 0755) + os.MkdirAll(realB, 0755) + os.WriteFile(filepath.Join(realA, "data.txt"), []byte("A"), 0644) + os.WriteFile(filepath.Join(realB, "data.txt"), []byte("B"), 0644) + + linkPath := filepath.Join(dir, "data") + if err := os.Symlink("real-a", linkPath); err != nil { + t.Skipf("symlink not supported in this environment: %v", err) + } + + c1, err := DirChecksum(dir) + if err != nil { + t.Fatalf("checksum should support symlinked directories: %v", err) + } + + if err := os.Remove(linkPath); err != nil { + t.Fatal(err) + } + if err := os.Symlink("real-b", linkPath); err != nil { + t.Fatal(err) + } + + c2, err := DirChecksum(dir) + if err != nil { + t.Fatal(err) + } + if c1 == c2 { + t.Error("changing symlinked directory content should change checksum") + } +} + // --- DirMaxMtime tests --- func TestDirMaxMtime_Basic(t *testing.T) { diff --git a/internal/sync/copy.go b/internal/sync/copy.go index 490d6e253..dea8a4b3d 100644 --- a/internal/sync/copy.go +++ b/internal/sync/copy.go @@ -317,6 +317,7 @@ func CheckStatusCopy(targetPath string) (TargetStatus, int, int) { // Skips .git directories. Only uses os.Stat (via filepath.Walk), never reads file content. func DirMaxMtime(dir string) (int64, error) { var maxMtime int64 + hasSymlink := false err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -327,50 +328,31 @@ func DirMaxMtime(dir string) (int64, error) { } return nil } + // Symlink targets can change without updating the link mtime. + // Disable mtime fast-path for symlink-containing skills. + if info.Mode()&os.ModeSymlink != 0 { + hasSymlink = true + return nil + } if mt := info.ModTime().UnixNano(); mt > maxMtime { maxMtime = mt } return nil }) - return maxMtime, err + if err != nil { + return 0, err + } + if hasSymlink { + return 0, fmt.Errorf("mtime fast-path not supported for directories containing symlinks") + } + return maxMtime, nil } // DirChecksum computes a deterministic SHA256 checksum of a directory. // It hashes sorted relative paths and file contents. func DirChecksum(dir string) (string, error) { - type entry struct { - relPath string - content []byte - } - var entries []entry - - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() { - // Skip .git directories - if info.Name() == ".git" { - return filepath.SkipDir - } - return nil - } - - relPath, err := filepath.Rel(dir, path) - if err != nil { - return err - } - // Normalize path separators for cross-platform consistency - relPath = strings.ReplaceAll(relPath, "\\", "/") - - content, err := os.ReadFile(path) - if err != nil { - return err - } - - entries = append(entries, entry{relPath: relPath, content: content}) - return nil - }) + var entries []checksumEntry + err := collectChecksumEntries(dir, "", &entries, map[string]bool{}) if err != nil { return "", err } @@ -390,3 +372,78 @@ func DirChecksum(dir string) (string, error) { return fmt.Sprintf("%x", h.Sum(nil)), nil } + +type checksumEntry struct { + relPath string + content []byte +} + +// collectChecksumEntries recursively collects file entries for checksumming. +// Directory symlinks are dereferenced to hash effective copied content. +func collectChecksumEntries(root, relPrefix string, entries *[]checksumEntry, active map[string]bool) error { + resolvedRoot, err := filepath.EvalSymlinks(root) + if err != nil { + return fmt.Errorf("failed to resolve checksum root %s: %w", root, err) + } + if active[resolvedRoot] { + return fmt.Errorf("detected symlink directory cycle while checksumming: %s", root) + } + active[resolvedRoot] = true + defer delete(active, resolvedRoot) + + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + // Skip .git directories + if info.Name() == ".git" { + return filepath.SkipDir + } + return nil + } + + relPath, err := filepath.Rel(root, path) + if err != nil { + return err + } + // Normalize path separators for cross-platform consistency + relPath = strings.ReplaceAll(relPath, "\\", "/") + if relPath == "." { + relPath = "" + } + + fullRelPath := relPath + if relPrefix != "" { + if fullRelPath == "" { + fullRelPath = relPrefix + } else { + fullRelPath = relPrefix + "/" + fullRelPath + } + } + + // A symlink can point to a directory. In copy mode we dereference + // directory symlinks, so checksum should include effective contents. + if info.Mode()&os.ModeSymlink != 0 { + targetInfo, statErr := os.Stat(path) + if statErr != nil { + return fmt.Errorf("failed to stat symlink target %s: %w", path, statErr) + } + if targetInfo.IsDir() { + resolvedDir, resolveErr := filepath.EvalSymlinks(path) + if resolveErr != nil { + return fmt.Errorf("failed to resolve symlink directory %s: %w", path, resolveErr) + } + return collectChecksumEntries(resolvedDir, fullRelPath, entries, active) + } + } + + content, err := os.ReadFile(path) + if err != nil { + return err + } + + *entries = append(*entries, checksumEntry{relPath: fullRelPath, content: content}) + return nil + }) +} diff --git a/internal/sync/sync.go b/internal/sync/sync.go index 7ce72ed17..fb01549d5 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -352,6 +352,22 @@ func mergeDirectories(src, dst string) error { // copyDirectory copies a directory recursively func copyDirectory(src, dst string) error { + return copyDirectoryWithState(src, dst, map[string]bool{}) +} + +// copyDirectoryWithState copies recursively and dereferences directory symlinks. +// active tracks real paths in the current recursion stack to prevent cycles. +func copyDirectoryWithState(src, dst string, active map[string]bool) error { + resolvedSrc, err := filepath.EvalSymlinks(src) + if err != nil { + return fmt.Errorf("failed to resolve source directory %s: %w", src, err) + } + if active[resolvedSrc] { + return fmt.Errorf("detected symlink directory cycle while copying: %s", src) + } + active[resolvedSrc] = true + defer delete(active, resolvedSrc) + return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -364,6 +380,22 @@ func copyDirectory(src, dst string) error { return os.MkdirAll(dstPath, info.Mode()) } + // In copy mode we need real files/dirs, so directory symlinks are + // dereferenced and copied as concrete directories. + if info.Mode()&os.ModeSymlink != 0 { + targetInfo, statErr := os.Stat(path) + if statErr != nil { + return fmt.Errorf("failed to stat symlink target %s: %w", path, statErr) + } + if targetInfo.IsDir() { + resolvedDir, resolveErr := filepath.EvalSymlinks(path) + if resolveErr != nil { + return fmt.Errorf("failed to resolve symlink directory %s: %w", path, resolveErr) + } + return copyDirectoryWithState(resolvedDir, dstPath, active) + } + } + return copyFile(path, dstPath) }) } diff --git a/tests/integration/sync_copy_test.go b/tests/integration/sync_copy_test.go index 9ef827080..343f60a35 100644 --- a/tests/integration/sync_copy_test.go +++ b/tests/integration/sync_copy_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "os" "path/filepath" + "runtime" "testing" "skillshare/internal/sync" @@ -244,6 +245,51 @@ targets: } } +func TestSync_CopyMode_SymlinkedDirectoryInSkill(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("symlink behavior differs on Windows") + } + + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + skillDir := sb.CreateSkill("ui-ux-pro-max", map[string]string{ + "SKILL.md": "# UI UX Pro Max", + }) + realData := filepath.Join(skillDir, "real-data") + if err := os.MkdirAll(realData, 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(realData, "payload.txt"), []byte("hello"), 0644); err != nil { + t.Fatal(err) + } + if err := os.Symlink("real-data", filepath.Join(skillDir, "data")); err != nil { + t.Skipf("symlink not supported in this environment: %v", err) + } + + targetPath := sb.CreateTarget("copilot") + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: + copilot: + path: ` + targetPath + ` + mode: copy +`) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + copiedDataPath := filepath.Join(targetPath, "ui-ux-pro-max", "data") + if sb.IsSymlink(copiedDataPath) { + t.Error("copy mode should materialize symlinked directories as real directories") + } + if !sb.FileExists(filepath.Join(copiedDataPath, "payload.txt")) { + t.Error("symlinked directory content should be copied") + } + + // Regression check: checksum path should also succeed on subsequent sync. + sb.RunCLI("sync").AssertSuccess(t) +} + func TestSync_CopyMode_Pruning(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup()