summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-10-09 15:53:45 -0400
committerDax Raad <[email protected]>2025-10-09 15:54:01 -0400
commitdfc7ac4cf083eeeaa0cc8cabad21c1524b3903a2 (patch)
tree3bd2ad1c27b3c79d87dd451e88b230f9b030fe11
parentc2950d26f0bd27656556efaa72a3dcc51b144d64 (diff)
downloadopencode-dfc7ac4cf083eeeaa0cc8cabad21c1524b3903a2.tar.gz
opencode-dfc7ac4cf083eeeaa0cc8cabad21c1524b3903a2.zip
ignore: improve file ignore performance and cross-platform support
- Replace glob patterns with Set lookup for common folders to speed up matching - Use path.sep for cross-platform compatibility on Windows/Unix systems - Add comprehensive test coverage for nested and non-nested folder matching - Simplify implementation by removing unnecessary caching complexity
-rw-r--r--packages/opencode/src/file/ignore.ts63
-rw-r--r--packages/opencode/test/file/ignore.test.ts10
2 files changed, 44 insertions, 29 deletions
diff --git a/packages/opencode/src/file/ignore.ts b/packages/opencode/src/file/ignore.ts
index 912f2159e..fc0c95cad 100644
--- a/packages/opencode/src/file/ignore.ts
+++ b/packages/opencode/src/file/ignore.ts
@@ -1,28 +1,26 @@
+import { sep } from "node:path"
+
export namespace FileIgnore {
- const DEFAULT_PATTERNS = [
- // Dependencies
- "**/node_modules/**",
- "**/bower_components/**",
- "**/.pnpm-store/**",
- "**/vendor/**",
-
- // Build outputs
- "**/dist/**",
- "**/build/**",
- "**/out/**",
- "**/.next/**",
- "**/target/**", // Rust
- "**/bin/**",
- "**/obj/**", // .NET
-
- // Version control
- "**/.git/**",
- "**/.svn/**",
- "**/.hg/**",
-
- // IDE/Editor
- "**/.vscode/**",
- "**/.idea/**",
+ const FOLDERS = new Set([
+ "node_modules",
+ "bower_components",
+ ".pnpm-store",
+ "vendor",
+ "dist",
+ "build",
+ "out",
+ ".next",
+ "target",
+ "bin",
+ "obj",
+ ".git",
+ ".svn",
+ ".hg",
+ ".vscode",
+ ".idea",
+ ])
+
+ const FILES = [
"**/*.swp",
"**/*.swo",
@@ -41,22 +39,29 @@ export namespace FileIgnore {
"**/.nyc_output/**",
]
- const GLOBS = DEFAULT_PATTERNS.map((p) => new Bun.Glob(p))
+ const FILE_GLOBS = FILES.map((p) => new Bun.Glob(p))
export function match(
filepath: string,
- opts: {
+ opts?: {
extra?: Bun.Glob[]
whitelist?: Bun.Glob[]
},
) {
- for (const glob of opts.whitelist || []) {
+ for (const glob of opts?.whitelist || []) {
if (glob.match(filepath)) return false
}
- const extra = opts.extra || []
- for (const glob of [...GLOBS, ...extra]) {
+
+ const parts = filepath.split(sep)
+ for (let i = 0; i < parts.length; i++) {
+ if (FOLDERS.has(parts[i])) return true
+ }
+
+ const extra = opts?.extra || []
+ for (const glob of [...FILE_GLOBS, ...extra]) {
if (glob.match(filepath)) return true
}
+
return false
}
}
diff --git a/packages/opencode/test/file/ignore.test.ts b/packages/opencode/test/file/ignore.test.ts
new file mode 100644
index 000000000..6387ff63e
--- /dev/null
+++ b/packages/opencode/test/file/ignore.test.ts
@@ -0,0 +1,10 @@
+import { test, expect } from "bun:test"
+import { FileIgnore } from "../../src/file/ignore"
+
+test("match nested and non-nested", () => {
+ expect(FileIgnore.match("node_modules/index.js")).toBe(true)
+ expect(FileIgnore.match("node_modules")).toBe(true)
+ expect(FileIgnore.match("node_modules/")).toBe(true)
+ expect(FileIgnore.match("node_modules/bar")).toBe(true)
+ expect(FileIgnore.match("node_modules/bar/")).toBe(true)
+})