fix(directory): self-heal a stale .codegraph/.gitignore so daemon.pid is ignored (#788) (#802)

Versions <= 0.9.9 wrote an explicit-allowlist .codegraph/.gitignore
(*.db, cache/, .dirty, ...) that never listed daemon.pid or the socket,
so the daemon's runtime pidfile got committed. The wildcard rewrite in
#654/#492/#484 fixed new inits, but the file is only written when
absent, so existing installs kept their stale file forever — the fix
never reached the people hitting it.

Make the gitignore self-heal: ensureGitignore() writes the file if
absent and upgrades a stale CodeGraph-generated default in place,
leaving a user-authored file untouched. A "stale default" is one that
carries our `# CodeGraph data files` header but predates the wildcard
ignore (no bare `*` line) — a header match heals every historical
variant (v0.7.x..0.9.9, all verified to share it) and is idempotent.
validateDirectory() runs on every open()/openSync(), so existing repos
heal on the next codegraph command after upgrading. The duplicated
template (previously inlined in two formats) is consolidated into one
GITIGNORE_CONTENT constant.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-11 11:09:05 -05:00
committed by GitHub
co-authored by Claude Opus 4.8
parent c39b4b938e
commit 9a0f144770
3 changed files with 107 additions and 21 deletions
+40
View File
@@ -159,6 +159,46 @@ describe('CodeGraph Foundation', () => {
expect(validation.valid).toBe(false);
expect(validation.errors.length).toBeGreaterThan(0);
});
it('upgrades a stale pre-wildcard .gitignore in place (issue #788)', () => {
const cg = CodeGraph.initSync(tempDir);
cg.close();
const gitignorePath = path.join(getCodeGraphDir(tempDir), '.gitignore');
// A .gitignore written by an older version (<= 0.9.9): an explicit
// allowlist that never ignored daemon.pid, so the daemon's runtime
// pidfile got committed.
const staleV099 =
'# CodeGraph data files\n' +
'# These are local to each machine and should not be committed\n\n' +
'# Database\n*.db\n*.db-wal\n*.db-shm\n\n' +
'# Cache\ncache/\n\n# Logs\n*.log\n\n# Hook markers\n.dirty\n';
fs.writeFileSync(gitignorePath, staleV099, 'utf-8');
// Opening the project runs validateDirectory, which self-heals.
const cg2 = CodeGraph.openSync(tempDir);
cg2.close();
const upgraded = fs.readFileSync(gitignorePath, 'utf-8');
expect(upgraded).toContain('\n*\n'); // wildcard ignores everything…
expect(upgraded).toContain('!.gitignore'); // …except this file
expect(upgraded).not.toContain('.dirty'); // old explicit list is gone
});
it('leaves a user-customized .codegraph/.gitignore untouched', () => {
const cg = CodeGraph.initSync(tempDir);
cg.close();
const gitignorePath = path.join(getCodeGraphDir(tempDir), '.gitignore');
// No CodeGraph header → user-authored → must not be rewritten.
const custom = '# my own rules\n*.db\n!keep-this.json\n';
fs.writeFileSync(gitignorePath, custom, 'utf-8');
const cg2 = CodeGraph.openSync(tempDir);
cg2.close();
expect(fs.readFileSync(gitignorePath, 'utf-8')).toBe(custom);
});
});
describe('Uninitialize', () => {