fix(db): resolve directory paths in findDbPath to .codegraph/graph.db by carlos-alm · Pull Request #1706 · optave/ops-codegraph-tool · GitHub
Skip to content

fix(db): resolve directory paths in findDbPath to .codegraph/graph.db#1706

Merged
carlos-alm merged 2 commits into
mainfrom
fix/db-path-directory-resolution
Jun 25, 2026
Merged

fix(db): resolve directory paths in findDbPath to .codegraph/graph.db#1706
carlos-alm merged 2 commits into
mainfrom
fix/db-path-directory-resolution

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • findDbPath() returned a directory path verbatim when --db <dir> was passed
  • SQLite then threw disk I/O error trying to open a directory as a database file
  • Every read command was broken (stats, query, map, cycles, triage, fn-impact, context, audit, deps, diff-impact, export, etc.)
  • build was unaffected because pipeline.ts appends .codegraph/graph.db independently

Fix: 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

  • All 1137 unit tests pass
  • codegraph stats --db /path/to/repo no longer throws disk I/O error
  • codegraph stats --db /path/to/repo/.codegraph/graph.db still works (explicit file path)
  • codegraph stats --db /new/path/graph.db still works (non-existent path returned as-is)

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
@greptile-apps

greptile-apps Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Comment thread src/db/connection.ts
// inside it — matching the layout that `build` creates.
try {
if (fs.statSync(resolved).isDirectory()) {
return path.join(resolved, '.codegraph', 'graph.db');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Fix in Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/db/connection.ts
// inside it — matching the layout that `build` creates.
try {
if (fs.statSync(resolved).isDirectory()) {
return path.join(resolved, '.codegraph', 'graph.db');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Fix in Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

3 functions changed133 callers affected across 83 files

  • findDbPath in src/db/connection.ts:263 (122 transitive callers)
  • openRepo in src/db/connection.ts:387 (28 transitive callers)
  • openReadonlyWithNative in src/db/connection.ts:450 (8 transitive callers)

…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
@carlos-alm

Copy link
Copy Markdown
Contributor Author

@carlos-alm carlos-alm merged commit 4f15575 into main Jun 25, 2026
30 checks passed
@carlos-alm carlos-alm deleted the fix/db-path-directory-resolution branch June 25, 2026 04:05
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant