From d925bf4e585b0e7bc5d03cf2718abc09db65cadd Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 17 Mar 2026 16:06:25 +0800 Subject: [PATCH] fix(discovery): respect .skillignore in source discovery (#83) DiscoverSourceSkills and DiscoverSourceSkillsLite now load .skillignore from tracked repos and skip matching paths during walk. This prevents false warnings in doctor/status/list for vendored SKILL.md files (e.g. .venv/ inside a tracked repo). Extract shared skillignore package from internal/install so both install-time and source discovery use the same filtering logic. --- internal/install/ignore_test.go | 59 ---------------------- internal/install/install_discovery.go | 40 ++------------- internal/skillignore/skillignore.go | 41 +++++++++++++++ internal/skillignore/skillignore_test.go | 63 +++++++++++++++++++++++ internal/sync/discover_test.go | 64 ++++++++++++++++++++++++ internal/sync/sync.go | 40 ++++++++++++++- 6 files changed, 210 insertions(+), 97 deletions(-) create mode 100644 internal/skillignore/skillignore.go create mode 100644 internal/skillignore/skillignore_test.go diff --git a/internal/install/ignore_test.go b/internal/install/ignore_test.go index f1e6be7e2..cf4b346f2 100644 --- a/internal/install/ignore_test.go +++ b/internal/install/ignore_test.go @@ -6,65 +6,6 @@ import ( "testing" ) -func TestMatchSkillIgnore_ExactMatch(t *testing.T) { - patterns := []string{"debug-tool"} - if !matchSkillIgnore("debug-tool", patterns) { - t.Error("expected exact match to return true") - } -} - -func TestMatchSkillIgnore_GroupPrefix(t *testing.T) { - patterns := []string{"experimental"} - // "experimental" matches "experimental/sub-skill" as a group prefix - if !matchSkillIgnore("experimental/sub-skill", patterns) { - t.Error("expected group prefix match to return true") - } -} - -func TestMatchSkillIgnore_WildcardSuffix(t *testing.T) { - patterns := []string{"test-*"} - if !matchSkillIgnore("test-alpha", patterns) { - t.Error("expected wildcard suffix match for 'test-alpha'") - } - if !matchSkillIgnore("test-beta/sub", patterns) { - t.Error("expected wildcard suffix match for 'test-beta/sub'") - } -} - -func TestMatchSkillIgnore_NoMatch(t *testing.T) { - patterns := []string{"debug-tool", "test-*"} - if matchSkillIgnore("production-skill", patterns) { - t.Error("expected no match for 'production-skill'") - } -} - -func TestReadSkillIgnore_ParsesFile(t *testing.T) { - dir := t.TempDir() - content := "# Comment line\n\ndebug-tool\ntest-*\nexperimental\n" - if err := os.WriteFile(filepath.Join(dir, ".skillignore"), []byte(content), 0644); err != nil { - t.Fatal(err) - } - - patterns := readSkillIgnore(dir) - if len(patterns) != 3 { - t.Fatalf("expected 3 patterns (skipping comment and blank), got %d: %v", len(patterns), patterns) - } - expected := []string{"debug-tool", "test-*", "experimental"} - for i, p := range patterns { - if p != expected[i] { - t.Errorf("pattern[%d] = %q, want %q", i, p, expected[i]) - } - } -} - -func TestReadSkillIgnore_NoFile(t *testing.T) { - dir := t.TempDir() - patterns := readSkillIgnore(dir) - if patterns != nil { - t.Errorf("expected nil patterns for missing .skillignore, got %v", patterns) - } -} - func TestDiscoverSkills_WithSkillIgnore(t *testing.T) { dir := t.TempDir() diff --git a/internal/install/install_discovery.go b/internal/install/install_discovery.go index 1986ce7ed..4364e0229 100644 --- a/internal/install/install_discovery.go +++ b/internal/install/install_discovery.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" + "skillshare/internal/skillignore" "skillshare/internal/utils" ) @@ -97,41 +98,6 @@ func resolveSubdir(repoPath, subdir string) (string, error) { } } -// readSkillIgnore reads a .skillignore file from the given directory. -// Returns a list of patterns (exact names or trailing-wildcard like "prefix-*"). -// Lines starting with # and empty lines are skipped. -func readSkillIgnore(dir string) []string { - data, err := os.ReadFile(filepath.Join(dir, ".skillignore")) - if err != nil { - return nil - } - var patterns []string - for _, line := range strings.Split(string(data), "\n") { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - patterns = append(patterns, line) - } - return patterns -} - -// matchSkillIgnore returns true if skillPath matches any pattern. -// Matching is path-based: exact path, group prefix (pattern matches a -// directory prefix of skillPath), and trailing wildcard ("prefix-*"). -func matchSkillIgnore(skillPath string, patterns []string) bool { - for _, p := range patterns { - if strings.HasSuffix(p, "*") { - if strings.HasPrefix(skillPath, strings.TrimSuffix(p, "*")) { - return true - } - } else if skillPath == p || strings.HasPrefix(skillPath, p+"/") { - return true - } - } - return false -} - // discoverSkills finds directories containing SKILL.md // If includeRoot is true, root-level SKILL.md is also included (with Path=".") func discoverSkills(repoPath string, includeRoot bool) []SkillInfo { @@ -181,11 +147,11 @@ func discoverSkills(repoPath string, includeRoot bool) []SkillInfo { }) // Apply .skillignore filtering - patterns := readSkillIgnore(repoPath) + patterns := skillignore.ReadPatterns(repoPath) if len(patterns) > 0 { filtered := skills[:0] for _, s := range skills { - if !matchSkillIgnore(s.Path, patterns) { + if !skillignore.Match(s.Path, patterns) { filtered = append(filtered, s) } } diff --git a/internal/skillignore/skillignore.go b/internal/skillignore/skillignore.go new file mode 100644 index 000000000..cc7fc50d1 --- /dev/null +++ b/internal/skillignore/skillignore.go @@ -0,0 +1,41 @@ +package skillignore + +import ( + "os" + "path/filepath" + "strings" +) + +// ReadPatterns reads a .skillignore file from dir. +// Returns nil if no .skillignore exists. +// Skips blank lines and lines starting with #. +func ReadPatterns(dir string) []string { + data, err := os.ReadFile(filepath.Join(dir, ".skillignore")) + if err != nil { + return nil + } + var patterns []string + for _, line := range strings.Split(string(data), "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + patterns = append(patterns, line) + } + return patterns +} + +// Match returns true if skillPath matches any pattern. +// Supports: exact path, group prefix (path/sub), trailing wildcard (prefix-*). +func Match(skillPath string, patterns []string) bool { + for _, p := range patterns { + if strings.HasSuffix(p, "*") { + if strings.HasPrefix(skillPath, strings.TrimSuffix(p, "*")) { + return true + } + } else if skillPath == p || strings.HasPrefix(skillPath, p+"/") { + return true + } + } + return false +} diff --git a/internal/skillignore/skillignore_test.go b/internal/skillignore/skillignore_test.go new file mode 100644 index 000000000..e60cc15a9 --- /dev/null +++ b/internal/skillignore/skillignore_test.go @@ -0,0 +1,63 @@ +package skillignore + +import ( + "os" + "path/filepath" + "testing" +) + +func TestMatch_ExactMatch(t *testing.T) { + patterns := []string{"debug-tool"} + if !Match("debug-tool", patterns) { + t.Error("expected exact match") + } +} + +func TestMatch_GroupPrefix(t *testing.T) { + patterns := []string{"experimental"} + if !Match("experimental/sub-skill", patterns) { + t.Error("expected group prefix match") + } +} + +func TestMatch_WildcardSuffix(t *testing.T) { + patterns := []string{"test-*"} + if !Match("test-alpha", patterns) { + t.Error("expected wildcard match for test-alpha") + } + if !Match("test-beta/sub", patterns) { + t.Error("expected wildcard match for test-beta/sub") + } +} + +func TestMatch_NoMatch(t *testing.T) { + patterns := []string{"debug-tool", "test-*"} + if Match("production-skill", patterns) { + t.Error("expected no match") + } +} + +func TestReadPatterns_ParsesFile(t *testing.T) { + dir := t.TempDir() + content := "# Comment line\n\ndebug-tool\ntest-*\nexperimental\n" + os.WriteFile(filepath.Join(dir, ".skillignore"), []byte(content), 0644) + + patterns := ReadPatterns(dir) + if len(patterns) != 3 { + t.Fatalf("expected 3 patterns, got %d: %v", len(patterns), patterns) + } + expected := []string{"debug-tool", "test-*", "experimental"} + for i, p := range patterns { + if p != expected[i] { + t.Errorf("pattern[%d] = %q, want %q", i, p, expected[i]) + } + } +} + +func TestReadPatterns_NoFile(t *testing.T) { + dir := t.TempDir() + patterns := ReadPatterns(dir) + if patterns != nil { + t.Errorf("expected nil, got %v", patterns) + } +} diff --git a/internal/sync/discover_test.go b/internal/sync/discover_test.go index 66bc716ef..0f3217f3d 100644 --- a/internal/sync/discover_test.go +++ b/internal/sync/discover_test.go @@ -3,6 +3,7 @@ package sync import ( "os" "path/filepath" + "strings" "testing" ) @@ -232,6 +233,69 @@ func TestDiscoverSourceSkillsLite_BasicDiscovery(t *testing.T) { } } +// --- .skillignore tests (Issue #83) --- + +func TestDiscoverSourceSkills_RespectsSkillIgnore(t *testing.T) { + src := t.TempDir() + + // Create a tracked repo with .skillignore excluding .venv + repoDir := filepath.Join(src, "_team-skills") + os.MkdirAll(filepath.Join(repoDir, ".git"), 0755) + os.WriteFile(filepath.Join(repoDir, ".skillignore"), []byte(".venv\n"), 0644) + + // Vendored SKILL.md inside .venv — should be ignored + venvSkill := filepath.Join(repoDir, ".venv", "lib", "python3.13", "site-packages", "fastapi", ".agents", "skills", "fastapi") + writeSkillMD(t, venvSkill, "not a real skill") + + // Normal skill — should be discovered + writeSkillMD(t, filepath.Join(repoDir, "my-skill"), "---\nname: my-skill\n---\n# My Skill") + + skills, err := DiscoverSourceSkills(src) + if err != nil { + t.Fatal(err) + } + + for _, s := range skills { + if strings.Contains(s.RelPath, ".venv") { + t.Errorf("expected .venv skill to be filtered by .skillignore, got %s", s.RelPath) + } + } + if len(skills) != 1 { + t.Errorf("expected 1 skill, got %d", len(skills)) + } +} + +func TestDiscoverSourceSkillsLite_RespectsSkillIgnore(t *testing.T) { + src := t.TempDir() + + // Same layout as above + repoDir := filepath.Join(src, "_team-skills") + os.MkdirAll(filepath.Join(repoDir, ".git"), 0755) + os.WriteFile(filepath.Join(repoDir, ".skillignore"), []byte(".venv\n"), 0644) + + venvSkill := filepath.Join(repoDir, ".venv", "lib", "fastapi") + writeSkillMD(t, venvSkill, "not a real skill") + + writeSkillMD(t, filepath.Join(repoDir, "my-skill"), "---\nname: my-skill\n---\n# My Skill") + + skills, repos, err := DiscoverSourceSkillsLite(src) + if err != nil { + t.Fatal(err) + } + + for _, s := range skills { + if strings.Contains(s.RelPath, ".venv") { + t.Errorf("expected .venv skill to be filtered by .skillignore, got %s", s.RelPath) + } + } + if len(skills) != 1 { + t.Errorf("expected 1 skill, got %d", len(skills)) + } + if len(repos) != 1 || repos[0] != "_team-skills" { + t.Errorf("expected tracked repo [_team-skills], got %v", repos) + } +} + func TestDiscoverSourceSkillsLite_EmptyDir(t *testing.T) { src := t.TempDir() diff --git a/internal/sync/sync.go b/internal/sync/sync.go index 1d2db71bd..8cebfe175 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -8,6 +8,7 @@ import ( "strings" "skillshare/internal/config" + "skillshare/internal/skillignore" "skillshare/internal/utils" ) @@ -33,7 +34,8 @@ type DiscoveredSkill struct { func DiscoverSourceSkillsLite(sourcePath string) ([]DiscoveredSkill, []string, error) { var skills []DiscoveredSkill var trackedRepos []string - trackedRepoPaths := make(map[string]bool) // track paths to detect nested tracked repos + trackedRepoPaths := make(map[string]bool) // track paths to detect nested tracked repos + ignorePatterns := make(map[string][]string) // tracked repo abs path → .skillignore patterns walkRoot := utils.ResolveSymlink(sourcePath) @@ -56,6 +58,9 @@ func DiscoverSourceSkillsLite(sourcePath string) ([]DiscoveredSkill, []string, e trackedRepos = append(trackedRepos, relPath) trackedRepoPaths[path] = true } + if patterns := skillignore.ReadPatterns(path); len(patterns) > 0 { + ignorePatterns[path] = patterns + } } } @@ -79,6 +84,17 @@ func DiscoverSourceSkillsLite(sourcePath string) ([]DiscoveredSkill, []string, e isInRepo = true } + // Check .skillignore for tracked repos + if isInRepo && len(parts) > 1 { + repoAbsPath := filepath.Join(walkRoot, parts[0]) + if patterns, ok := ignorePatterns[repoAbsPath]; ok { + repoRelPath := strings.Join(parts[1:], "/") + if skillignore.Match(repoRelPath, patterns) { + return nil + } + } + } + // Skip frontmatter parsing — Targets stays nil // Use original sourcePath (not walkRoot) so SourcePath preserves // the caller's logical path, even if sourcePath is a symlink. @@ -106,6 +122,7 @@ func DiscoverSourceSkillsLite(sourcePath string) ([]DiscoveredSkill, []string, e // Returns all discovered skills with their metadata for syncing. func DiscoverSourceSkills(sourcePath string) ([]DiscoveredSkill, error) { var skills []DiscoveredSkill + ignorePatterns := make(map[string][]string) // tracked repo abs path → .skillignore patterns walkRoot := utils.ResolveSymlink(sourcePath) @@ -120,6 +137,16 @@ func DiscoverSourceSkills(sourcePath string) ([]DiscoveredSkill, error) { return filepath.SkipDir } + // Load .skillignore for tracked repos + if info.IsDir() && info.Name() != "." && utils.IsTrackedRepoDir(info.Name()) { + gitDir := filepath.Join(path, ".git") + if _, statErr := os.Stat(gitDir); statErr == nil { + if patterns := skillignore.ReadPatterns(path); len(patterns) > 0 { + ignorePatterns[path] = patterns + } + } + } + // Look for SKILL.md files if !info.IsDir() && info.Name() == "SKILL.md" { skillDir := filepath.Dir(path) @@ -143,6 +170,17 @@ func DiscoverSourceSkills(sourcePath string) ([]DiscoveredSkill, error) { isInRepo = true } + // Check .skillignore for tracked repos + if isInRepo && len(parts) > 1 { + repoAbsPath := filepath.Join(walkRoot, parts[0]) + if patterns, ok := ignorePatterns[repoAbsPath]; ok { + repoRelPath := strings.Join(parts[1:], "/") + if skillignore.Match(repoRelPath, patterns) { + return nil + } + } + } + // Use original sourcePath for SourcePath to preserve the caller's // logical path. Parse frontmatter from the resolved path. targets := utils.ParseFrontmatterList(filepath.Join(skillDir, "SKILL.md"), "targets")