Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(gitignore): root-anchor the graft/ ignore entry (#79)
Shrish Dwivedi7ba085f264ba
diff --git a/src/context/node-file.ts b/src/context/node-file.tsindex a4e9d49f..dfd9f2d3 100644--- a/src/context/node-file.ts+++ b/src/context/node-file.ts@@ -117,13 +117,20 @@ export function contextDirFor(root: string, override?: string): string {export function ensureGitignored(root: string, contextDir: string): void {const rel = relPosix(root, contextDir);if (rel === "" || rel.startsWith("..")) return; // dir is at/above the repo root — nothing sane to ignore- const entry = `${stripTrailingSlashes(rel)}/`;+ const bare = stripTrailingSlashes(rel); // "graft" (or a `--dir` subpath like "tools/ctx")+ // Root-ANCHORED, so it ignores exactly this repo's `graft/` and not a directory named+ // `graft` at any depth. An unanchored `graft/` also matched `.claude/skills/graft/`, so+ // committing the skill graft just wrote was silently dropped (#79). `rel` is always+ // repo-relative here, so a leading `/` is always safe (incl. a `--dir` subpath).+ const entry = `/${bare}/`;const path = join(root, ".gitignore");let current = "";try { current = readFileSync(path, "utf8"); } catch { /* no .gitignore yet — we create one */ }+ // Accept the anchored form AND the older unanchored `graft/` / `graft`, so existing+ // repos aren't double-appended and a hand-anchored entry survives the next build.const present = current.split("\n").some((l) => {const t = l.trim();- return t === entry || t === rel;+ return t === entry || t === `${bare}/` || t === bare;});if (present) return;const gap = current === "" ? "" : current.endsWith("\n") ? "\n" : "\n\n";diff --git a/test/context.test.ts b/test/context.test.tsindex b54ca2a3..0bcf73d2 100644--- a/test/context.test.ts+++ b/test/context.test.ts@@ -290,7 +290,9 @@ test("ensureGitignored: creates .gitignore with the graft/ entry when none existtry {ensureGitignored(dir, contextDirFor(dir));const gi = readFileSync(join(dir, ".gitignore"), "utf8");- assert.match(gi, /^graft\/$/m);+ // root-ANCHORED (#79): an unanchored `graft/` also matched `.claude/skills/graft/`+ assert.match(gi, /^\/graft\/$/m);+ assert.doesNotMatch(gi, /^graft\/$/m, "must not write the unanchored form");assert.match(gi, /regenerable, not committed/);} finally {rmSync(dir, { recursive: true, force: true });@@ -305,7 +307,7 @@ test("ensureGitignored: appends to an existing .gitignore without clobbering it"const gi = readFileSync(join(dir, ".gitignore"), "utf8");assert.match(gi, /node_modules\//);assert.match(gi, /dist\//);- assert.match(gi, /^graft\/$/m);+ assert.match(gi, /^\/graft\/$/m);} finally {rmSync(dir, { recursive: true, force: true });}@@ -319,7 +321,35 @@ test("ensureGitignored: idempotent — a second build adds nothing", () => {ensureGitignored(dir, contextDirFor(dir));const twice = readFileSync(join(dir, ".gitignore"), "utf8");assert.equal(once, twice);- assert.equal((twice.match(/^graft\/$/gm) ?? []).length, 1);+ assert.equal((twice.match(/^\/graft\/$/gm) ?? []).length, 1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test("ensureGitignored: an existing unanchored `graft/` or hand-anchored `/graft/` is NOT re-appended (#79)", () => {+ for (const existing of ["graft/", "graft", "/graft/"]) {+ const dir = mkdtempSync(join(tmpdir(), "ctxgi-"));+ try {+ writeFileSync(join(dir, ".gitignore"), `node_modules/\n${existing}\n`);+ ensureGitignored(dir, contextDirFor(dir));+ const gi = readFileSync(join(dir, ".gitignore"), "utf8");+ // the presence check recognizes all three spellings → no duplicate graft line added+ const graftLines = gi.split("\n").filter((l) => /^\/?graft\/?$/.test(l.trim()));+ assert.equal(graftLines.length, 1, `existing "${existing}" must not be double-appended (got ${graftLines.length})`);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ }+});++test("ensureGitignored: a `--dir` subpath is root-anchored too (#79)", () => {+ const dir = mkdtempSync(join(tmpdir(), "ctxgi-"));+ try {+ // a graft dir nested under the repo (e.g. --dir tools/ctx) still gets a repo-root anchor+ ensureGitignored(dir, join(dir, "tools", "ctx"));+ const gi = readFileSync(join(dir, ".gitignore"), "utf8");+ assert.match(gi, /^\/tools\/ctx\/$/m);} finally {rmSync(dir, { recursive: true, force: true });}