xonecas.io
64875
Mc

mini-coder / commit 45cc9a6

Fix glob/grep ignore handling for absolute and out-of-cwd paths

xonecas2026-03-08 45cc9a66192fd41205be8885c94550921f9f02b8 parent 35a2337

5 files changed

src/tools/glob.test.ts+57−0
@@ -0,0 +1,57 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { dirname, join } from "node:path";
5+import { globTool } from "./glob.ts";
6+
7+describe("globTool", () => {
8+ let root: string;
9+
10+ beforeEach(() => {
11+ root = mkdtempSync(join(tmpdir(), "mc-glob-test-"));
12+ mkdirSync(join(root, "src"));
13+ writeFileSync(join(root, ".gitignore"), "");
14+ writeFileSync(join(root, "src", "a.ts"), "export const ok = true;\n");
15+ writeFileSync(join(root, "src", "a.md"), "note\n");
16+ });
17+
18+ afterEach(() => {
19+ rmSync(root, { recursive: true, force: true });
20+ });
21+
22+ test("normalizes absolute patterns to cwd-relative results", async () => {
23+ const result = await globTool.execute({
24+ cwd: root,
25+ pattern: join(root, "**", "*.ts"),
26+ });
27+
28+ expect(result.files).toContain("src/a.ts");
29+ expect(
30+ result.files.every((p) => !p.startsWith("/") && !p.startsWith("..")),
31+ ).toBe(true);
32+ });
33+
34+ test("ignores default-ignore directories outside cwd", async () => {
35+ const outside = mkdtempSync(join(dirname(root), "mc-glob-outside-"));
36+ mkdirSync(join(outside, ".git"));
37+ writeFileSync(join(outside, "ok.txt"), "ok\n");
38+ writeFileSync(join(outside, ".git", "ignored.txt"), "ignore\n");
39+
40+ try {
41+ const result = await globTool.execute({
42+ cwd: root,
43+ pattern: join(outside, "**", "*"),
44+ });
45+
46+ expect(
47+ result.files.some((file) => file.endsWith("ok.txt")),
48+ "Expected outside allowed file to be discovered",
49+ ).toBe(true);
50+ expect(
51+ result.files.some((file) => file.endsWith(".git/ignored.txt")),
52+ ).toBe(false);
53+ } finally {
54+ rmSync(outside, { recursive: true, force: true });
55+ }
56+ });
57+});
src/tools/glob.ts+16−9
@@ -1,7 +1,8 @@
1-import { join, relative } from "node:path";
1+import { resolve } from "node:path";
22 import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
44 import { loadGitignore } from "./ignore.ts";
5+import { getScannedPathInfo } from "./scan-path.ts";
56
67 const GlobSchema = z.object({
78 pattern: z
@@ -39,21 +40,27 @@ export const globTool: ToolDef<GlobInput, GlobOutput> = {
3940 const matches: Array<{ path: string; mtime: number }> = [];
4041
4142 for await (const file of glob.scan({ cwd, onlyFiles: true, dot: true })) {
43+ const { relativePath, ignoreTargets } = getScannedPathInfo(cwd, file);
44+ const firstSegment = relativePath.split("/")[0] ?? "";
45+
4246 // Check if ignored by .gitignore
43- if (ig?.ignores(file)) continue;
47+ if (ignoreTargets.some((path) => ig?.ignores(path))) continue;
4448
4549 // Check explicit ignore patterns
46- const firstSegment = file.split("/")[0] ?? "";
47- const ignored = ignoreGlobs.some(
48- (g) => g.match(file) || g.match(firstSegment),
50+ const ignored = ignoreTargets.some((candidate) =>
51+ ignoreGlobs.some((g) => g.match(candidate) || g.match(firstSegment)),
4952 );
5053 if (ignored) continue;
54+
5155 try {
52- const fullPath = join(cwd, file);
56+ const fullPath = resolve(cwd, relativePath);
5357 const stat = (await Bun.file(fullPath).stat?.()) ?? null;
54- matches.push({ path: file, mtime: stat?.mtime?.getTime() ?? 0 });
58+ matches.push({
59+ path: relativePath,
60+ mtime: stat?.mtime?.getTime() ?? 0,
61+ });
5562 } catch {
56- matches.push({ path: file, mtime: 0 });
63+ matches.push({ path: relativePath, mtime: 0 });
5764 }
5865
5966 if (matches.length >= MAX_RESULTS + 1) break;
@@ -65,7 +72,7 @@ export const globTool: ToolDef<GlobInput, GlobOutput> = {
6572 // Sort by mtime descending (most recently modified first)
6673 matches.sort((a, b) => b.mtime - a.mtime);
6774
68- const files = matches.map((m) => relative(cwd, join(cwd, m.path)));
75+ const files = matches.map((m) => m.path);
6976
7077 return { files, count: files.length, truncated };
7178 },
src/tools/grep.test.ts+71−0
@@ -0,0 +1,71 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { dirname, join } from "node:path";
5+import { grepTool } from "./grep.ts";
6+
7+describe("grepTool", () => {
8+ let root: string;
9+
10+ beforeEach(() => {
11+ root = mkdtempSync(join(tmpdir(), "mc-grep-test-"));
12+ mkdirSync(join(root, "src"));
13+ writeFileSync(join(root, ".gitignore"), "");
14+ writeFileSync(join(root, "src", "notes.md"), "OpenAI auth token\n");
15+ writeFileSync(
16+ join(root, "src", "ignore.txt"),
17+ "openai should be skipped by extension\n",
18+ );
19+ });
20+
21+ afterEach(() => {
22+ rmSync(root, { recursive: true, force: true });
23+ });
24+
25+ test("normalizes absolute include patterns and returns relative file paths", async () => {
26+ const result = await grepTool.execute({
27+ cwd: root,
28+ pattern: "OpenAI",
29+ include: join(root, "**", "*.md"),
30+ caseSensitive: false,
31+ contextLines: 2,
32+ maxResults: 50,
33+ });
34+
35+ expect(result.matches).toHaveLength(1);
36+ expect(result.matches[0]?.file).toBe("src/notes.md");
37+ expect(result.matches[0]?.file.startsWith("/")).toBe(false);
38+ expect(result.matches[0]?.file.startsWith("..")).toBe(false);
39+ });
40+
41+ test("ignores default-ignore directories outside cwd", async () => {
42+ const outside = mkdtempSync(join(dirname(root), "mc-grep-outside-"));
43+ mkdirSync(join(outside, ".git"));
44+ writeFileSync(join(outside, "allowed.txt"), "OpenAI inside allowed\n");
45+ writeFileSync(
46+ join(outside, ".git", "secrets.txt"),
47+ "OpenAI hidden token\n",
48+ );
49+
50+ try {
51+ const result = await grepTool.execute({
52+ cwd: root,
53+ pattern: "OpenAI",
54+ include: join(outside, "**", "*.txt"),
55+ caseSensitive: false,
56+ contextLines: 2,
57+ maxResults: 50,
58+ });
59+
60+ expect(result.matches).toHaveLength(1);
61+ expect(
62+ result.matches.some((match) => match.file.endsWith("allowed.txt")),
63+ ).toBe(true);
64+ expect(
65+ result.matches.some((match) => match.file.includes(".git/secrets.txt")),
66+ ).toBe(false);
67+ } finally {
68+ rmSync(outside, { recursive: true, force: true });
69+ }
70+ });
71+});
src/tools/grep.ts+20−12
@@ -1,8 +1,8 @@
1-import { join } from "node:path";
21 import { z } from "zod";
32 import type { ToolDef } from "../llm-api/types.ts";
43 import { formatHashLine } from "./hashline.ts";
54 import { loadGitignore } from "./ignore.ts";
5+import { getScannedPathInfo } from "./scan-path.ts";
66
77 const GrepSchema = z.object({
88 pattern: z.string().describe("Regular expression to search for"),
@@ -39,9 +39,9 @@ export interface GrepOutput {
3939 }
4040
4141 const DEFAULT_IGNORE = [
42- "node_modules",
43- ".git",
44- "dist",
42+ "node_modules/**",
43+ ".git/**",
44+ "dist/**",
4545 "*.db",
4646 "*.db-shm",
4747 "*.db-wal",
@@ -69,19 +69,27 @@ export const grepTool: ToolDef<GrepInput, GrepOutput> = {
6969 let truncated = false;
7070 const ig = await loadGitignore(cwd);
7171
72- outer: for await (const relPath of fileGlob.scan({
72+ outer: for await (const fromPath of fileGlob.scan({
7373 cwd,
7474 onlyFiles: true,
7575 dot: true,
7676 })) {
77- if (ig?.ignores(relPath)) continue;
78-
79- const firstSegment = relPath.split("/")[0] ?? "";
80- // Skip ignored
81- if (ignoreGlob.some((g) => g.match(relPath) || g.match(firstSegment))) {
77+ const { absolute, relativePath, ignoreTargets } = getScannedPathInfo(
78+ cwd,
79+ fromPath,
80+ );
81+ const firstSegment = relativePath.split("/")[0] ?? "";
82+
83+ if (ignoreTargets.some((path) => ig?.ignores(path))) continue;
84+ if (
85+ ignoreTargets.some((candidate) =>
86+ ignoreGlob.some((g) => g.match(candidate) || g.match(firstSegment)),
87+ )
88+ ) {
8289 continue;
8390 }
84- const fullPath = join(cwd, relPath);
91+
92+ const fullPath = absolute;
8593 let text: string;
8694 try {
8795 text = await Bun.file(fullPath).text();
@@ -112,7 +120,7 @@ export const grepTool: ToolDef<GrepInput, GrepOutput> = {
112120 }
113121
114122 allMatches.push({
115- file: relPath,
123+ file: relativePath,
116124 line: i + 1,
117125 column: match.index + 1,
118126 text: formatHashLine(i + 1, line),
src/tools/scan-path.ts+45−0
@@ -0,0 +1,45 @@
1+import { relative, resolve, sep } from "node:path";
2+
3+export interface ScanPathInfo {
4+ absolute: string;
5+ relativePath: string;
6+ ignoreTargets: readonly string[];
7+ inCwd: boolean;
8+}
9+
10+const LEADING_PARENT_SEGMENTS = /^(?:\.\.\/)+/;
11+
12+export function getScannedPathInfo(
13+ cwd: string,
14+ scanPath: string,
15+): ScanPathInfo {
16+ const cwdAbsolute = resolve(cwd);
17+ const absolute = resolve(cwdAbsolute, scanPath);
18+ const relativePath = relative(cwdAbsolute, absolute).replaceAll("\\", "/");
19+ const inCwd =
20+ absolute === cwdAbsolute ||
21+ absolute.startsWith(cwdAbsolute === sep ? sep : `${cwdAbsolute}${sep}`);
22+ const ignoreTargets = getIgnoreTargets(relativePath, inCwd);
23+
24+ return {
25+ absolute,
26+ relativePath,
27+ ignoreTargets,
28+ inCwd,
29+ };
30+}
31+
32+function getIgnoreTargets(path: string, inCwd: boolean): string[] {
33+ if (inCwd) return [path];
34+
35+ const normalized = path.replace(LEADING_PARENT_SEGMENTS, "");
36+ const segments = normalized.split("/").filter(Boolean);
37+ if (segments.length === 0) return [normalized];
38+
39+ const targets = new Set<string>([normalized]);
40+ for (let i = 1; i < segments.length; i++) {
41+ targets.add(segments.slice(i).join("/"));
42+ }
43+
44+ return [...targets];
45+}