Files
skillshare/internal/sync/discover_walk.go
T
Willie e2cb9f66fc fix(sync): expand grouped target members in host path inference
GroupedProjectTargets() merges targets sharing the same project path
into a single canonical entry, dropping member targets. For example,
.agents/skills is shared by universal, codex, amp, and others, but
only 'universal' was returned as the inferred target.

Expand Members in getProjectTargetPaths() so all targets sharing a
project path are included in inference results.
2026-05-05 04:07:16 +08:00

349 lines
10 KiB
Go

package sync
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"skillshare/internal/config"
"skillshare/internal/skillignore"
"skillshare/internal/utils"
)
// discoverOptions controls the behavior of discoverSourceSkillsInternal.
type discoverOptions struct {
parseFrontmatter bool // parse SKILL.md frontmatter for targets
collectIgnored bool // collect ignored skill paths into IgnoreStats
collectTracked bool // collect tracked repo paths (for Lite mode)
collectContext bool // compute DescChars/BodyChars during walk (for analyze)
includeIgnored bool // include ignored skills in results with Disabled=true
}
var (
knownTargetNameSet map[string]bool
knownTargetNameSetOnce sync.Once
projectTargetPaths []projectTargetPath
projectTargetPathsOnce sync.Once
)
type projectTargetPath struct {
name string
path string
}
func getKnownTargetNameSet() map[string]bool {
knownTargetNameSetOnce.Do(func() {
targetNames := config.KnownTargetNames()
knownTargetNameSet = make(map[string]bool, len(targetNames))
for _, name := range targetNames {
knownTargetNameSet[name] = true
}
})
return knownTargetNameSet
}
func getProjectTargetPaths() []projectTargetPath {
projectTargetPathsOnce.Do(func() {
groupedTargets := config.GroupedProjectTargets()
projectTargetPaths = make([]projectTargetPath, 0, len(groupedTargets)*2)
for _, target := range groupedTargets {
if target.Name == "" || target.Path == "" {
continue
}
p := filepath.ToSlash(target.Path)
projectTargetPaths = append(projectTargetPaths, projectTargetPath{
name: target.Name,
path: p,
})
for _, member := range target.Members {
projectTargetPaths = append(projectTargetPaths, projectTargetPath{
name: member,
path: p,
})
}
}
sort.Slice(projectTargetPaths, func(i, j int) bool {
if projectTargetPaths[i].path == projectTargetPaths[j].path {
return projectTargetPaths[i].name < projectTargetPaths[j].name
}
return projectTargetPaths[i].path < projectTargetPaths[j].path
})
})
return projectTargetPaths
}
func inferSkillTargetsFromRelPath(relPath string) []string {
// Prefer explicit project paths from known targets (e.g. ".factory/skills").
targets := inferTargetsFromProjectPaths(relPath)
if len(targets) > 0 {
return targets
}
parts := strings.Split(relPath, "/")
if len(parts) < 3 || parts[1] != "skills" {
return nil
}
// Fallback to top-level host-style paths that match known target names
// (e.g. "openclaw/skills/*" for openclaw project path "skills").
host := strings.TrimPrefix(parts[0], ".")
if host == "" {
return nil
}
if getKnownTargetNameSet()[host] {
return []string{host}
}
return nil
}
func inferTargetsFromProjectPaths(relPath string) []string {
targets := make([]string, 0, 2)
for _, target := range getProjectTargetPaths() {
if matchesHostSkillPath(relPath, target.name, target.path) {
targets = append(targets, target.name)
}
}
return targets
}
func matchesHostSkillPath(relPath, targetName, targetPath string) bool {
switch {
case targetPath == "skills":
parts := strings.Split(relPath, "/")
return len(parts) >= 2 && parts[1] == "skills" && strings.TrimPrefix(parts[0], ".") == targetName
case relPath == targetPath:
return false
case strings.HasPrefix(relPath, targetPath+"/"):
return true
default:
return false
}
}
// discoverSourceSkillsInternal is the shared walk implementation used by all
// public discovery functions. It returns:
// - discovered skills
// - tracked repo paths (only when opts.collectTracked is true; nil otherwise)
// - ignore stats (only when opts.collectIgnored is true; nil otherwise)
// - error
func discoverSourceSkillsInternal(sourcePath string, opts discoverOptions) ([]DiscoveredSkill, []string, *skillignore.IgnoreStats, error) {
var skills []DiscoveredSkill
var trackedRepos []string
ignoreMatchers := make(map[string]*skillignore.Matcher) // tracked repo abs path → .skillignore matcher
walkRoot := utils.ResolveSymlink(sourcePath)
rootMatcher := skillignore.ReadMatcher(walkRoot)
// Stats collection (only allocated when needed)
var stats *skillignore.IgnoreStats
if opts.collectIgnored {
stats = &skillignore.IgnoreStats{}
// Record root .skillignore if it exists
rootIgnorePath := filepath.Join(walkRoot, ".skillignore")
if _, err := os.Stat(rootIgnorePath); err == nil {
stats.RootFile = rootIgnorePath
}
// Record root .skillignore.local if it exists
if rootMatcher.HasLocal {
stats.RootLocalFile = filepath.Join(walkRoot, ".skillignore.local")
}
if pats := rootMatcher.Patterns(); len(pats) > 0 {
stats.Patterns = append(stats.Patterns, pats...)
}
}
err := filepath.Walk(walkRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // Skip inaccessible paths
}
// Skip .git directory
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
// Skip directories matching root-level .skillignore.
// When collectIgnored is true, disable CanSkipDir so the walk
// descends into ignored directories and the file-level Match
// check can record each ignored SKILL.md path.
if info.IsDir() && !opts.collectIgnored && !opts.includeIgnored {
relPath, relErr := filepath.Rel(walkRoot, path)
if relErr == nil && relPath != "." {
relPath = strings.ReplaceAll(relPath, "\\", "/")
if rootMatcher.CanSkipDir(relPath) {
return filepath.SkipDir
}
}
}
// Collect tracked repos: _-prefixed directories that are git repos
if info.IsDir() && info.Name() != "." && utils.IsTrackedRepoDir(info.Name()) {
gitDir := filepath.Join(path, ".git")
if _, statErr := os.Stat(gitDir); statErr == nil {
if opts.collectTracked {
relPath, relErr := filepath.Rel(walkRoot, path)
if relErr == nil && relPath != "." {
trackedRepos = append(trackedRepos, relPath)
}
}
m := skillignore.ReadMatcher(path)
if m.HasRules() {
ignoreMatchers[path] = m
// Record repo-level .skillignore in stats
if opts.collectIgnored {
repoIgnorePath := filepath.Join(path, ".skillignore")
stats.RepoFiles = append(stats.RepoFiles, repoIgnorePath)
if m.HasLocal {
stats.RepoLocalFiles = append(stats.RepoLocalFiles, filepath.Join(path, ".skillignore.local"))
}
if pats := m.Patterns(); len(pats) > 0 {
stats.Patterns = append(stats.Patterns, pats...)
}
}
}
}
}
// Skip directories matching repo-level .skillignore inside tracked repos.
// Same CanSkipDir bypass as above when collectIgnored is true.
if info.IsDir() && !opts.collectIgnored && !opts.includeIgnored {
relPath, relErr := filepath.Rel(walkRoot, path)
if relErr == nil && relPath != "." {
relPath = strings.ReplaceAll(relPath, "\\", "/")
parts := strings.Split(relPath, "/")
if len(parts) > 1 && utils.IsTrackedRepoDir(parts[0]) {
repoAbsPath := filepath.Join(walkRoot, parts[0])
if m, ok := ignoreMatchers[repoAbsPath]; ok {
repoRelPath := strings.Join(parts[1:], "/")
if m.CanSkipDir(repoRelPath) {
return filepath.SkipDir
}
}
}
}
}
// Look for SKILL.md files
if !info.IsDir() && info.Name() == "SKILL.md" {
skillDir := filepath.Dir(path)
relPath, err := filepath.Rel(walkRoot, skillDir)
if err != nil {
return nil
}
if relPath == "." {
return nil
}
relPath = strings.ReplaceAll(relPath, "\\", "/")
// Root-level .skillignore fallback (for files in non-skipped dirs)
if rootMatcher.Match(relPath, false) {
if opts.collectIgnored {
stats.IgnoredSkills = append(stats.IgnoredSkills, relPath)
}
if opts.includeIgnored {
inRepo := false
igParts := strings.Split(relPath, "/")
if len(igParts) > 0 && utils.IsTrackedRepoDir(igParts[0]) {
inRepo = true
}
skills = append(skills, DiscoveredSkill{
SourcePath: filepath.Join(sourcePath, relPath),
RelPath: relPath,
FlatName: utils.PathToFlatName(relPath),
IsInRepo: inRepo,
Targets: inferSkillTargetsFromRelPath(relPath),
Disabled: true,
})
}
return nil
}
isInRepo := false
parts := strings.Split(relPath, "/")
if len(parts) > 0 && utils.IsTrackedRepoDir(parts[0]) {
isInRepo = true
}
if isInRepo && isSkillIgnored(parts, walkRoot, ignoreMatchers) {
if opts.collectIgnored {
stats.IgnoredSkills = append(stats.IgnoredSkills, relPath)
}
if opts.includeIgnored {
skills = append(skills, DiscoveredSkill{
SourcePath: filepath.Join(sourcePath, relPath),
RelPath: relPath,
FlatName: utils.PathToFlatName(relPath),
IsInRepo: true,
Targets: inferSkillTargetsFromRelPath(relPath),
Disabled: true,
})
}
return nil
}
skillFile := filepath.Join(skillDir, "SKILL.md")
var targets []string
var descChars, bodyChars int
var description string
var lintIssues []LintIssue
if opts.collectContext {
// Single read: parse targets + context from one os.ReadFile
content, readErr := os.ReadFile(skillFile)
if readErr == nil {
targets = utils.ParseFrontmatterListFromBytes(content, "targets")
var fmName string
var yamlErr error
fmName, descChars, bodyChars, description, yamlErr = calcContextFromContent(content)
if yamlErr != nil {
lintIssues = append(lintIssues, LintIssue{
Rule: "malformed-frontmatter",
Severity: LintError,
Category: "structure",
Message: fmt.Sprintf("frontmatter YAML is malformed: %v", yamlErr),
})
}
lintIssues = append(lintIssues, LintSkill(fmName, description, bodyChars)...)
}
} else if opts.parseFrontmatter {
targets = utils.ParseFrontmatterList(skillFile, "targets")
}
if len(targets) == 0 && (opts.parseFrontmatter || opts.collectContext) {
targets = inferSkillTargetsFromRelPath(relPath)
}
// Use original sourcePath (not walkRoot) so SourcePath preserves
// the caller's logical path, even if sourcePath is a symlink.
skills = append(skills, DiscoveredSkill{
SourcePath: filepath.Join(sourcePath, relPath),
RelPath: relPath,
FlatName: utils.PathToFlatName(relPath),
IsInRepo: isInRepo,
Targets: targets,
DescChars: descChars,
BodyChars: bodyChars,
Description: description,
LintIssues: lintIssues,
})
}
return nil
})
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to walk source directory: %w", err)
}
return skills, trackedRepos, stats, nil
}