mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
fix(sync): keep naming migration from claiming another skill's entry
The legacy lookup checks a skill's flat, standard and prefixed names. With a manifest that has no naming records yet, aaa/dev looked at "dev", found the top-level dev skill's managed copy and synced into it, because nothing said the entry belonged to someone else. A skill that already has its current entry now skips the lookup, and names other skills sync to in the same run are never treated as old names.
This commit is contained in:
@@ -101,9 +101,10 @@ func SyncTargetCopyWithSkillsOptions(name string, target config.TargetConfig, al
|
||||
manifest.Naming = make(map[string]string)
|
||||
}
|
||||
|
||||
taken := resolution.ValidTargetNames()
|
||||
for i, resolved := range resolution.Skills {
|
||||
skill := resolved.Skill
|
||||
activeName, err := selectActiveTargetNameForSync("copy", sc.Path, resolved, manifest, dryRun)
|
||||
activeName, err := selectActiveTargetNameForSync("copy", sc.Path, resolved, taken, manifest, dryRun)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -609,9 +609,10 @@ func SyncTargetMergeWithSkills(name string, target config.TargetConfig, allSkill
|
||||
return nil, fmt.Errorf("failed to read manifest: %w", err)
|
||||
}
|
||||
|
||||
taken := resolution.ValidTargetNames()
|
||||
for _, resolved := range resolution.Skills {
|
||||
skill := resolved.Skill
|
||||
activeName, err := selectActiveTargetNameForSync("merge", sc.Path, resolved, manifest, dryRun)
|
||||
activeName, err := selectActiveTargetNameForSync("merge", sc.Path, resolved, taken, manifest, dryRun)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -167,8 +167,9 @@ func (r *TargetSkillResolution) LegacyNames(mode, targetPath string, manifest *M
|
||||
if r == nil {
|
||||
return legacy
|
||||
}
|
||||
taken := r.ValidTargetNames()
|
||||
for _, skill := range r.Skills {
|
||||
if name, _, err := findLegacyTargetEntry(mode, targetPath, skill, manifest); err == nil && name != "" {
|
||||
if name, _, err := findLegacyTargetEntry(mode, targetPath, skill, taken, manifest); err == nil && name != "" {
|
||||
legacy[name] = skill
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,10 @@ import (
|
||||
// selectActiveTargetNameForSync returns the entry name a skill syncs to. When
|
||||
// the skill still has a managed entry under the name an earlier target naming
|
||||
// gave it, that entry is renamed in place, unless the new name is taken.
|
||||
func selectActiveTargetNameForSync(mode, targetPath string, skill ResolvedTargetSkill, manifest *Manifest, dryRun bool) (string, error) {
|
||||
// taken holds the names other skills sync to in this run.
|
||||
func selectActiveTargetNameForSync(mode, targetPath string, skill ResolvedTargetSkill, taken map[string]bool, manifest *Manifest, dryRun bool) (string, error) {
|
||||
desiredName := skill.TargetName
|
||||
legacyName, legacyNaming, err := findLegacyTargetEntry(mode, targetPath, skill, manifest)
|
||||
legacyName, legacyNaming, err := findLegacyTargetEntry(mode, targetPath, skill, taken, manifest)
|
||||
if err != nil || legacyName == "" {
|
||||
return desiredName, err
|
||||
}
|
||||
@@ -51,10 +52,16 @@ func selectActiveTargetNameForSync(mode, targetPath string, skill ResolvedTarget
|
||||
}
|
||||
|
||||
// findLegacyTargetEntry returns the managed entry a skill holds under the name
|
||||
// another target naming gave it, and that naming, or "" when there is none.
|
||||
func findLegacyTargetEntry(mode, targetPath string, skill ResolvedTargetSkill, manifest *Manifest) (string, string, error) {
|
||||
// another target naming gave it, and that naming, or "" when there is none:
|
||||
// when the skill already has its current name, or the old name is one another
|
||||
// skill syncs to (taken), there is nothing to migrate.
|
||||
func findLegacyTargetEntry(mode, targetPath string, skill ResolvedTargetSkill, taken map[string]bool, manifest *Manifest) (string, string, error) {
|
||||
desiredPath := filepath.Join(targetPath, skill.TargetName)
|
||||
if info, err := os.Lstat(desiredPath); err == nil && isManagedTargetEntry(mode, desiredPath, info, skill, manifest, namedTarget{name: skill.TargetName}) {
|
||||
return "", "", nil
|
||||
}
|
||||
for _, c := range targetNameCandidates(skill.Skill) {
|
||||
if c.name == "" || c.name == skill.TargetName {
|
||||
if c.name == "" || c.name == skill.TargetName || taken[c.name] {
|
||||
continue
|
||||
}
|
||||
legacyPath := filepath.Join(targetPath, c.name)
|
||||
@@ -65,19 +72,22 @@ func findLegacyTargetEntry(mode, targetPath string, skill ResolvedTargetSkill, m
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to inspect legacy target entry %s: %w", c.name, err)
|
||||
}
|
||||
if isManagedLegacyTargetEntry(mode, legacyPath, info, skill, manifest, c) {
|
||||
if isManagedTargetEntry(mode, legacyPath, info, skill, manifest, c) {
|
||||
return c.name, c.naming, nil
|
||||
}
|
||||
}
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
func isManagedLegacyTargetEntry(mode, legacyPath string, info os.FileInfo, skill ResolvedTargetSkill, manifest *Manifest, c namedTarget) bool {
|
||||
// isManagedTargetEntry reports whether the entry c.name at entryPath is the
|
||||
// skill's managed entry. With c.naming set, a copy must also have been made
|
||||
// under that naming.
|
||||
func isManagedTargetEntry(mode, entryPath string, info os.FileInfo, skill ResolvedTargetSkill, manifest *Manifest, c namedTarget) bool {
|
||||
switch mode {
|
||||
case "merge":
|
||||
return utils.IsSymlinkOrJunction(legacyPath) && isSymlinkToSource(legacyPath, skill.Skill.SourcePath)
|
||||
return utils.IsSymlinkOrJunction(entryPath) && isSymlinkToSource(entryPath, skill.Skill.SourcePath)
|
||||
case "copy":
|
||||
if manifest == nil || !info.IsDir() || utils.IsSymlinkOrJunction(legacyPath) {
|
||||
if manifest == nil || !info.IsDir() || utils.IsSymlinkOrJunction(entryPath) {
|
||||
return false
|
||||
}
|
||||
if _, managed := manifest.Managed[c.name]; !managed {
|
||||
@@ -86,7 +96,7 @@ func isManagedLegacyTargetEntry(mode, legacyPath string, info os.FileInfo, skill
|
||||
// A recorded naming says exactly which naming made the entry; older
|
||||
// manifests have none, so the first managed candidate wins.
|
||||
recorded := manifest.Naming[c.name]
|
||||
return recorded == "" || recorded == c.naming
|
||||
return c.naming == "" || recorded == "" || recorded == c.naming
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -217,3 +217,34 @@ func TestTarget_SetPrefixedNamingNeedsCopyMode(t *testing.T) {
|
||||
sb.RunCLI("sync").AssertSuccess(t)
|
||||
assertEntries(t, sb, targetPath, "emil-design-prototype", "mattpocock-skills-prototype", "my-skill")
|
||||
}
|
||||
|
||||
func TestSync_TargetNaming_OldManifestDoesNotTakeAnotherSkillsEntry(t *testing.T) {
|
||||
sb := testutil.NewSandbox(t)
|
||||
defer sb.Cleanup()
|
||||
sb.CreateSkill("dev", map[string]string{"SKILL.md": "---\nname: dev\n---\n# top"})
|
||||
// "aaa/dev" syncs before "dev", so its lookup runs before dev records its naming.
|
||||
sb.CreateNestedSkill("aaa/dev", map[string]string{"SKILL.md": "---\nname: dev\n---\n# nested"})
|
||||
targetPath := sb.CreateTarget("claude")
|
||||
writeNamingConfig(sb, targetPath, "flat", "copy")
|
||||
sb.RunCLI("sync").AssertSuccess(t)
|
||||
|
||||
// Under an old manifest, "dev" is also aaa/dev's standard name; it
|
||||
// still belongs to the top-level skill that syncs to it.
|
||||
manifest := readSyncManifest(t, sb, targetPath)
|
||||
manifest.Naming = nil
|
||||
data, err := json.Marshal(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sb.WriteFile(filepath.Join(targetPath, ssync.ManifestFile), string(data))
|
||||
|
||||
result := sb.RunCLI("sync")
|
||||
result.AssertSuccess(t)
|
||||
result.AssertOutputNotContains(t, "kept legacy")
|
||||
if got := sb.ReadFile(filepath.Join(targetPath, "dev", "SKILL.md")); !strings.Contains(got, "# top") {
|
||||
t.Fatalf("dev was overwritten:\n%s", got)
|
||||
}
|
||||
if got := sb.ReadFile(filepath.Join(targetPath, "aaa__dev", "SKILL.md")); !strings.Contains(got, "# nested") {
|
||||
t.Fatalf("aaa__dev has the wrong content:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user