fix(db): resolve directory paths in findDbPath to .codegraph/graph.db#1706
Conversation
When --db is passed a directory (the documented usage), findDbPath returned the directory verbatim. SQLite then threw "disk I/O error" trying to open a directory as a database file — breaking every read command (stats, query, map, cycles, triage, etc.) while build was unaffected (pipeline.ts appends the path itself). Fix: detect if the resolved path is an existing directory and append .codegraph/graph.db, matching the layout that `build` creates. Paths that are explicit file paths or don't exist yet are returned unchanged. Closes #1705
| // inside it — matching the layout that `build` creates. | ||
| try { | ||
| if (fs.statSync(resolved).isDirectory()) { | ||
| return path.join(resolved, '.codegraph', 'graph.db'); |
There was a problem hiding this comment.
Raw Directory Drives Config Root
When a native-backed read command runs with --db /path/to/repo, this branch resolves the database to /path/to/repo/.codegraph/graph.db, but the native open path still derives rootDir from the original customPath. That computes the parent of the repo and can load the wrong .codegraphrc.json, so repo-local settings are ignored even though the database path is found.
There was a problem hiding this comment.
Fixed. The root cause was that openRepo and openReadonlyWithNative derived rootDir from path.resolve(customDbPath) directly, which gives the wrong parent when customDbPath is a directory. Both functions now call findDbPath(customDbPath) first to get the canonical .codegraph/graph.db path, then derive rootDir = path.dirname(path.dirname(resolvedDbPath)) — so the correct repo directory is always used when loading .codegraphrc.json. Commit: 1cdd766.
| // inside it — matching the layout that `build` creates. | ||
| try { | ||
| if (fs.statSync(resolved).isDirectory()) { | ||
| return path.join(resolved, '.codegraph', 'graph.db'); |
There was a problem hiding this comment.
Codegraph Directory Double-Appends
When the caller passes the existing .codegraph directory itself, for example --db /path/to/repo/.codegraph, this branch treats it as a repo root and returns /path/to/repo/.codegraph/.codegraph/graph.db. The command then reports that no database exists even though /path/to/repo/.codegraph/graph.db is present.
There was a problem hiding this comment.
Fixed. Added a check in findDbPath at the directory-is-.codegraph branch: when path.basename(resolved) === '.codegraph', we return path.join(resolved, 'graph.db') directly instead of re-appending .codegraph. Added a unit test covering this exact case (findDbPath('.codegraph dir passed directly')). Commit: 1cdd766.
Codegraph Impact Analysis3 functions changed → 133 callers affected across 83 files
|
…1706) Two bugs in directory-path handling for --db: 1. When the caller passes the .codegraph directory itself (e.g. --db /repo/.codegraph), findDbPath was treating it as a repo root and returning /repo/.codegraph/.codegraph/graph.db. Now detects basename === '.codegraph' and appends only graph.db. 2. rootDir derivation in openRepo/openReadonlyWithNative was computing path.dirname(path.dirname(path.resolve(customDbPath))) on the raw input, so --db /path/to/repo stripped two levels off the repo dir (yielding /path) instead of the repo root. Fixed by deriving rootDir from the already-resolved findDbPath output. Add two unit tests covering both edge cases. Impact: 3 functions changed, 133 affected

Summary
findDbPath()returned a directory path verbatim when--db <dir>was passeddisk I/O errortrying to open a directory as a database filestats,query,map,cycles,triage,fn-impact,context,audit,deps,diff-impact,export, etc.)buildwas unaffected becausepipeline.tsappends.codegraph/graph.dbindependentlyFix: check if the resolved path is an existing directory; if so, append
.codegraph/graph.db. Explicit file paths and non-existent paths (future custom DB locations) pass through unchanged.Found during
Dogfooding v3.15.0 — see #1705
Test plan
codegraph stats --db /path/to/repono longer throws disk I/O errorcodegraph stats --db /path/to/repo/.codegraph/graph.dbstill works (explicit file path)codegraph stats --db /new/path/graph.dbstill works (non-existent path returned as-is)