Files
skillshare/internal/sync/relative.go
T
Willie 51656e9cb3 fix(sync): keep project agent symlinks stable across syncs on symlinked paths
Project-mode agent sync re-reported every agent as updated on the second
sync when the project lived under a symlinked ancestor (e.g. macOS temp
dirs under /var -> /private/var), failing idempotency on macOS CI.

Two interacting causes:

- shouldUseRelative was unstable: on the first sync the target directory
  does not exist yet, so evalOrClean fell back to a non-resolved path and
  the under-project-root check failed, producing an absolute link; on the
  second sync the directory existed and resolved, so it wanted a relative
  link and reformatted the existing one. evalOrClean now resolves the
  deepest existing ancestor and re-appends the missing tail, so the answer
  is the same whether or not the leaf exists.
- The existing-symlink comparison only did a direct string compare, so a
  stable relative link whose resolved target carried the unresolved alias
  prefix looked like it pointed elsewhere. It now canonicalizes both sides
  via EvalSymlinks on the slow path, mirroring isSymlinkToSource.

Adds a regression test that reproduces the symlinked-ancestor case on any
platform.
2026-06-11 01:18:05 +08:00

106 lines
3.6 KiB
Go

package sync
import (
"fmt"
"os"
"path/filepath"
"skillshare/internal/utils"
)
// shouldUseRelative returns true if both sourcePath and targetPath
// are under the given projectRoot, meaning a relative symlink
// between them would be portable across machines.
// Returns false if projectRoot is empty (global mode).
// Paths are resolved through EvalSymlinks so that symlinked ancestors
// do not fool the prefix check.
func shouldUseRelative(projectRoot, sourcePath, targetPath string) bool {
if projectRoot == "" {
return false
}
cleaned := evalOrClean(projectRoot)
prefix := cleaned
if prefix != string(filepath.Separator) {
prefix += string(filepath.Separator)
}
src := evalOrClean(sourcePath)
tgt := evalOrClean(targetPath)
srcUnder := utils.PathHasPrefix(src, prefix) || utils.PathsEqual(src, cleaned)
tgtUnder := utils.PathHasPrefix(tgt, prefix) || utils.PathsEqual(tgt, cleaned)
return srcUnder && tgtUnder
}
// evalOrClean resolves symlinks in path. When the full path does not exist yet
// (e.g. the target directory on the first sync), it resolves the deepest
// existing ancestor and re-appends the missing tail, so a symlinked ancestor
// (macOS /var → /private/var) is canonicalized consistently whether or not the
// leaf exists. Without this, shouldUseRelative would flip between runs — raw on
// the first sync (leaf missing) and resolved on the second (leaf present) —
// causing a needless absolute→relative reformat on every second sync.
func evalOrClean(p string) string {
p = filepath.Clean(p)
if resolved, err := filepath.EvalSymlinks(p); err == nil {
return resolved
}
tail := ""
cur := p
for {
parent := filepath.Dir(cur)
if parent == cur {
break // reached root without an existing ancestor
}
tail = filepath.Join(filepath.Base(cur), tail)
if resolved, err := filepath.EvalSymlinks(parent); err == nil {
return filepath.Join(resolved, tail)
}
cur = parent
}
return p
}
// 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 {
if dest == "" {
return false
}
if wantRelative && !canCreateRelativeLink() {
// Platform cannot create relative symlinks (e.g. Windows without
// Developer Mode falls back to junctions, which are always absolute).
// Skip reformat to avoid remove→recreate loop on every sync.
return false
}
return wantRelative == filepath.IsAbs(dest)
}
// reformatLink atomically replaces an existing symlink with a new one
// using the specified format. It creates a temp link and renames it
// over the original so the link is never missing. Falls back to
// remove→create when rename fails (e.g. Windows junctions).
func reformatLink(linkPath, sourcePath string, relative bool) error {
tmpPath := linkPath + ".ss-reformat"
os.Remove(tmpPath) // clean up stale temp
if err := createLink(tmpPath, sourcePath, relative); err != nil {
return err
}
if err := os.Rename(tmpPath, linkPath); err == nil {
return nil
}
// Rename failed (Windows junction, cross-device, etc.); fall back
os.Remove(tmpPath)
if err := os.Remove(linkPath); err != nil {
return fmt.Errorf("failed to remove old link: %w", err)
}
return createLink(linkPath, sourcePath, relative)
}