xonecas.io
64875
Mc

mini-coder / commit d9a4192

fix(tools): respect gitignore and dotfiles for glob/grep

- Stop ignoring dotfiles by default in `glob` and `grep` tools.
- Introduce `ignore` library to evaluate local `.gitignore` rules.
- Extract duplicated gitignore loading logic into a shared helper `src/tools/ignore.ts`.
- Pre-compile ignore glob patterns before loop to prevent heavy performance overhead.
- Optimize path string parsing to run only once per file instead of inside nested `.some()` blocks.

Sean2026-02-26 d9a4192c6ba5b2023f2818557e62f439ea3f440b parent f064e9d

5 files changed

TODO.md+0−5
@@ -127,11 +127,6 @@ ZodError: [
127127
128128 I'd like to have the subgent output/activity to be streamed to the main output. Right now we wait for the subagent to be complete to print all at once
129129
130-## Glob and grep ignore dot files and folders
131-
132-This is just dumb and an error when implementing the spec. We are making a dev tool, it's
133-**obvious** that dotfiles shouldn't be ignored. We should respect the gitignore and nothing more.
134-
135130 ## Implement WebContent and WebSearch using exa.ai api
136131
137132 Why not just keep using them via their MCP? Simple answer: their tools descriptions/schema is bloated with params that don't concern the llm. They are also misleading in their naming conventions, futher worsening the issue. Let's use their api (let's auto discover the api key like we do for other providers "EXA_AI_API_KEY") and if Exa is enabled, the tools are given to the LLM.
package.json+1−0
@@ -22,6 +22,7 @@
2222 "@ai-sdk/openai-compatible": "^2.0.30",
2323 "@modelcontextprotocol/sdk": "^1.26.0",
2424 "ai": "^6.0.94",
25+ "ignore": "^7.0.5",
2526 "ollama-ai-provider": "^1.2.0",
2627 "yoctocolors": "^2.1.1",
2728 "zod": "^4.3.6"
src/tools/glob.ts+15−15
@@ -1,6 +1,7 @@
11 import { join, relative } from "node:path";
22 import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
4+import { loadGitignore } from "./ignore.ts";
45
56 const GlobSchema = z.object({
67 pattern: z
@@ -27,27 +28,26 @@ export const globTool: ToolDef<GlobInput, GlobOutput> = {
2728 schema: GlobSchema,
2829 execute: async (input) => {
2930 const cwd = input.cwd ?? process.cwd();
30- const defaultIgnore = [
31- "node_modules/**",
32- ".git/**",
33- "dist/**",
34- "*.db",
35- "*.db-shm",
36- "*.db-wal",
37- ];
31+ const defaultIgnore = [".git/**", "node_modules/**"];
3832 const ignorePatterns = [...defaultIgnore, ...(input.ignore ?? [])];
3933
34+ const ignoreGlobs = ignorePatterns.map((pat) => new Bun.Glob(pat));
35+
36+ const ig = await loadGitignore(cwd);
37+
4038 const glob = new Bun.Glob(input.pattern);
4139 const matches: Array<{ path: string; mtime: number }> = [];
4240
43- for await (const file of glob.scan({ cwd, onlyFiles: true })) {
44- // Check ignore patterns
45- const ignored = ignorePatterns.some((pat) => {
46- const ig = new Bun.Glob(pat);
47- return ig.match(file);
48- });
49- if (ignored) continue;
41+ for await (const file of glob.scan({ cwd, onlyFiles: true, dot: true })) {
42+ // Check if ignored by .gitignore
43+ if (ig?.ignores(file)) continue;
5044
45+ // Check explicit ignore patterns
46+ const firstSegment = file.split("/")[0] ?? "";
47+ const ignored = ignoreGlobs.some(
48+ (g) => g.match(file) || g.match(firstSegment),
49+ );
50+ if (ignored) continue;
5151 try {
5252 const fullPath = join(cwd, file);
5353 const stat = (await Bun.file(fullPath).stat?.()) ?? null;
src/tools/grep.ts+7−6
@@ -2,6 +2,7 @@ import { join } from "node:path";
22 import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
44 import { formatHashLine } from "./hashline.ts";
5+import { loadGitignore } from "./ignore.ts";
56
67 const GrepSchema = z.object({
78 pattern: z.string().describe("Regular expression to search for"),
@@ -66,20 +67,20 @@ export const grepTool: ToolDef<GrepInput, GrepOutput> = {
6667
6768 const allMatches: GrepMatch[] = [];
6869 let truncated = false;
70+ const ig = await loadGitignore(cwd);
6971
7072 outer: for await (const relPath of fileGlob.scan({
7173 cwd,
7274 onlyFiles: true,
75+ dot: true,
7376 })) {
77+ if (ig?.ignores(relPath)) continue;
78+
79+ const firstSegment = relPath.split("/")[0] ?? "";
7480 // Skip ignored
75- if (
76- ignoreGlob.some(
77- (g) => g.match(relPath) || g.match(relPath.split("/")[0] ?? ""),
78- )
79- ) {
81+ if (ignoreGlob.some((g) => g.match(relPath) || g.match(firstSegment))) {
8082 continue;
8183 }
82-
8384 const fullPath = join(cwd, relPath);
8485 let text: string;
8586 try {
src/tools/ignore.ts+13−0
@@ -0,0 +1,13 @@
1+import { join } from "node:path";
2+import ignore from "ignore";
3+
4+export async function loadGitignore(
5+ cwd: string,
6+): Promise<ReturnType<typeof ignore> | null> {
7+ try {
8+ const gitignore = await Bun.file(join(cwd, ".gitignore")).text();
9+ return ignore().add(gitignore);
10+ } catch {
11+ return null;
12+ }
13+}