Skip to content

Commit c0e2e59

Browse files
fix(opencode): skip dependency directories during skill discovery
Pass ignore globs through Glob.scan so discovery does not descend into node_modules, .git, dist, or .cache. Skip a scan root already seen by realpath, so a skills symlink is not walked twice. Fixes anomalyco#51080
1 parent b471c2b commit c0e2e59

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎packages/core/src/util/glob.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export namespace Glob {
88
include?: "file" | "all"
99
dot?: boolean
1010
symlink?: boolean
11+
ignore?: string[]
1112
}
1213

1314
function toGlobOptions(options: Options): GlobOptions {
@@ -17,6 +18,7 @@ export namespace Glob {
1718
dot: options.dot,
1819
follow: options.symlink ?? false,
1920
nodir: options.include !== "all",
21+
ignore: options.ignore,
2022
}
2123
}
2224

‎packages/opencode/src/skill/index.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
2+
import { realpath } from "fs/promises"
23
import path from "path"
34
import { Effect, Layer, Context, Schema } from "effect"
45
import { NamedError } from "@opencode-ai/core/util/error"
@@ -23,6 +24,8 @@ const AGENTS_EXTERNAL_DIR = ".agents"
2324
const EXTERNAL_SKILL_PATTERN = "skills/**/SKILL.md"
2425
const OPENCODE_SKILL_PATTERN = "{skill,skills}/**/SKILL.md"
2526
const SKILL_PATTERN = "**/SKILL.md"
27+
// Trailing /** makes glob skip the directory instead of stat-ing every file inside it.
28+
const SKIP = ["**/node_modules/**", "**/.git/**", "**/dist/**", "**/.cache/**"]
2629

2730
// Built-in skill that ships with opencode. The model's intuition for what an
2831
// opencode.json should look like is often wrong, and opencode hard-fails on
@@ -92,6 +95,7 @@ type DiscoveryState = {
9295
type ScanState = {
9396
matches: Set<string>
9497
dirs: Set<string>
98+
seen: Set<string>
9599
}
96100

97101
export interface Interface {
@@ -139,12 +143,28 @@ const add = Effect.fnUntraced(function* (state: State, match: string, events: Ev
139143
}
140144
})
141145

146+
const canonical = (input: string) =>
147+
Effect.tryPromise({
148+
try: () => realpath(input),
149+
catch: (cause) => cause,
150+
}).pipe(Effect.catch(() => Effect.succeed(input)))
151+
142152
const scan = Effect.fnUntraced(function* (
143153
state: ScanState,
144154
root: string,
145155
pattern: string,
146156
opts?: { dot?: boolean; scope?: string },
147157
) {
158+
const real = yield* canonical(root)
159+
if (state.seen.has(real)) return
160+
const realSkills = yield* Effect.tryPromise({
161+
try: () => realpath(path.join(root, "skills")),
162+
catch: (cause) => cause,
163+
}).pipe(Effect.catch(() => Effect.succeed(undefined)))
164+
if (pattern === EXTERNAL_SKILL_PATTERN && realSkills && state.seen.has(realSkills)) return
165+
state.seen.add(real)
166+
if (pattern === EXTERNAL_SKILL_PATTERN && realSkills) state.seen.add(realSkills)
167+
148168
const matches = yield* Effect.tryPromise({
149169
try: () =>
150170
Glob.scan(pattern, {
@@ -153,6 +173,7 @@ const scan = Effect.fnUntraced(function* (
153173
include: "file",
154174
symlink: true,
155175
dot: opts?.dot,
176+
ignore: SKIP,
156177
}),
157178
catch: (error) => error,
158179
}).pipe(
@@ -165,6 +186,9 @@ const scan = Effect.fnUntraced(function* (
165186
)
166187

167188
for (const match of matches) {
189+
const file = yield* canonical(match)
190+
if (state.seen.has(file)) continue
191+
state.seen.add(file)
168192
state.matches.add(match)
169193
state.dirs.add(path.dirname(match))
170194
}
@@ -180,7 +204,7 @@ const discoverSkills = Effect.fnUntraced(function* (
180204
directory: string,
181205
worktree: string,
182206
) {
183-
const state: ScanState = { matches: new Set(), dirs: new Set() }
207+
const state: ScanState = { matches: new Set(), dirs: new Set(), seen: new Set() }
184208

185209
const externalDirs: string[] = []
186210
if (!disableExternalSkills) {

‎packages/opencode/test/skill/skill.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,45 @@ description: A skill in the .opencode/skills directory.
582582
{ git: true },
583583
),
584584
)
585+
586+
it.live("skips dependency directories and a skills symlink", () =>
587+
provideTmpdirInstance(
588+
(dir) =>
589+
Effect.gen(function* () {
590+
const skillFile = (name: string, description: string) => `---
591+
name: ${name}
592+
description: ${description}
593+
---
594+
595+
# ${name}
596+
`
597+
yield* Effect.promise(async () => {
598+
await Bun.write(
599+
path.join(dir, ".agents", "skills", "kept", "SKILL.md"),
600+
skillFile("kept", "A skill that should load."),
601+
)
602+
await Bun.write(
603+
path.join(dir, ".agents", "skills", "kept", "nested", "SKILL.md"),
604+
skillFile("nested", "A nested skill that should load."),
605+
)
606+
await Bun.write(
607+
path.join(dir, ".agents", "skills", "kept", "node_modules", "pkg", "SKILL.md"),
608+
skillFile("vendored", "A dependency skill that should not load."),
609+
)
610+
await Bun.write(
611+
path.join(dir, ".agents", "skills", "kept", ".git", "SKILL.md"),
612+
skillFile("git-skill", "A git skill that should not load."),
613+
)
614+
await fs.mkdir(path.join(dir, ".claude"), { recursive: true })
615+
await fs.symlink(path.join(dir, ".agents", "skills"), path.join(dir, ".claude", "skills"))
616+
})
617+
618+
const skill = yield* Skill.Service
619+
const list = (yield* skill.all()).filter((item) => item.location !== "<built-in>")
620+
expect(list.map((item) => item.name).toSorted()).toEqual(["kept", "nested"])
621+
expect((yield* skill.dirs()).length).toBe(2)
622+
}),
623+
{ git: true },
624+
),
625+
)
585626
})

‎packages/opencode/test/util/glob.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@ describe("Glob", () => {
106106
expect(results.sort()).toEqual([".hidden", "visible"])
107107
})
108108

109+
test("does not descend into ignored directories", async () => {
110+
await using tmp = await tmpdir()
111+
await fs.mkdir(path.join(tmp.path, "ok", "nested"), { recursive: true })
112+
await fs.mkdir(path.join(tmp.path, "ok", "node_modules", "pkg"), { recursive: true })
113+
await fs.mkdir(path.join(tmp.path, "ok", ".git"), { recursive: true })
114+
await fs.mkdir(path.join(tmp.path, "ok", "dist"), { recursive: true })
115+
await fs.mkdir(path.join(tmp.path, "ok", ".cache"), { recursive: true })
116+
await fs.writeFile(path.join(tmp.path, "ok", "SKILL.md"), "", "utf-8")
117+
await fs.writeFile(path.join(tmp.path, "ok", "nested", "SKILL.md"), "", "utf-8")
118+
await fs.writeFile(path.join(tmp.path, "ok", "node_modules", "pkg", "SKILL.md"), "", "utf-8")
119+
await fs.writeFile(path.join(tmp.path, "ok", ".git", "SKILL.md"), "", "utf-8")
120+
await fs.writeFile(path.join(tmp.path, "ok", "dist", "SKILL.md"), "", "utf-8")
121+
await fs.writeFile(path.join(tmp.path, "ok", ".cache", "SKILL.md"), "", "utf-8")
122+
123+
const results = await Glob.scan("**/SKILL.md", {
124+
cwd: tmp.path,
125+
absolute: true,
126+
include: "file",
127+
symlink: true,
128+
dot: true,
129+
ignore: ["**/node_modules/**", "**/.git/**", "**/dist/**", "**/.cache/**"],
130+
})
131+
132+
expect(results.sort()).toEqual(
133+
[path.join(tmp.path, "ok", "SKILL.md"), path.join(tmp.path, "ok", "nested", "SKILL.md")].sort(),
134+
)
135+
})
136+
109137
test("excludes dotfiles when dot option is false", async () => {
110138
await using tmp = await tmpdir()
111139
await fs.writeFile(path.join(tmp.path, ".hidden"), "", "utf-8")

0 commit comments

Comments
 (0)