Skip to content

fix(db): derive rootDir from customDbPath for engine config in openRepo/openReadonlyWithNative#1606

Merged
carlos-alm merged 2 commits into
mainfrom
chore/issue-resolution-baseline
Jun 18, 2026
Merged

fix(db): derive rootDir from customDbPath for engine config in openRepo/openReadonlyWithNative#1606
carlos-alm merged 2 commits into
mainfrom
chore/issue-resolution-baseline

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • openRepo() and openReadonlyWithNative() previously resolved the engine from process.env.CODEGRAPH_ENGINE directly — a process-wide value that ignores the target repo's .codegraphrc.json when customDbPath points to a different repo.
  • Now both functions derive rootDir from customDbPath using the established convention (<rootDir>/.codegraph/graph.db) and call loadConfig(rootDir) to get config.build.engine for that repo.
  • CODEGRAPH_ENGINE env still wins because applyEnvOverrides() already overwrites config.build.engine when the env var is set.
  • opts.engine (explicit CLI flag) still takes precedence in openRepo() via ??.

Test plan

  • Full test suite passes (npm test: 3120 tests, 185 files)
  • Lint clean (npm run lint)

Closes #1605

…openReadonlyWithNative

When a caller passes customDbPath pointing to a different repo, both
openRepo() and openReadonlyWithNative() previously resolved the engine
from process.env.CODEGRAPH_ENGINE (process-wide). They now derive the
rootDir from the customDbPath convention (<rootDir>/.codegraph/graph.db)
and call loadConfig(rootDir) to pick up that repo's build.engine setting.

CODEGRAPH_ENGINE env still wins because applyEnvOverrides() overwrites
config.build.engine when the env var is set, and opts.engine still takes
explicit precedence in openRepo().

Closes #1605
@greptile-apps

greptile-apps Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes engine-selection in openRepo and openReadonlyWithNative so each call reads the .codegraphrc.json belonging to the target repository, rather than always resolving config from process.cwd(). The fix derives rootDir from customDbPath/customPath by ascending two directory levels before calling loadConfig(rootDir).

  • openRepo: adds rootDir derivation from customDbPath and passes it to loadConfig; opts.engine still takes the highest priority via ??.
  • openReadonlyWithNative: same derivation from customPath, mirroring openRepo behavior consistently.
  • When neither path is provided, rootDir is undefined and loadConfig(undefined) falls back to process.cwd(), preserving the original behavior.

Confidence Score: 5/5

This is a safe, focused fix that correctly scopes config loading to the target repo's directory in both affected functions.

The change is minimal and well-reasoned: two symmetric two-liner additions, both guarded against undefined input, with a clear fallback to process.cwd() behavior when no path is provided. The priority chain (opts.engine → config.build.engine → 'auto') is unchanged and CODEGRAPH_ENGINE env continues to win via applyEnvOverrides. No error-path regressions are introduced.

No files require special attention.

Important Files Changed

Filename Overview
src/db/connection.ts Derives rootDir from customDbPath/customPath via 2-level dirname ascent before calling loadConfig, ensuring the correct per-repo engine config is used instead of always reading the process-level cwd config.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Caller
    participant openRepo/openReadonlyWithNative
    participant loadConfig
    participant fs as .codegraphrc.json

    Caller->>openRepo/openReadonlyWithNative: "customDbPath = "/proj/.codegraph/graph.db""
    openRepo/openReadonlyWithNative->>openRepo/openReadonlyWithNative: "rootDir = dirname(dirname(resolve(customDbPath))) → "/proj""
    openRepo/openReadonlyWithNative->>loadConfig: loadConfig("/proj")
    loadConfig->>fs: read /proj/.codegraphrc.json
    fs-->>loadConfig: "{ build: { engine: "native" } }"
    loadConfig-->>openRepo/openReadonlyWithNative: config
    openRepo/openReadonlyWithNative->>openRepo/openReadonlyWithNative: "engine = opts.engine ?? config.build.engine ?? "auto""
    openRepo/openReadonlyWithNative-->>Caller: "{ repo, close() }"
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Caller
    participant openRepo/openReadonlyWithNative
    participant loadConfig
    participant fs as .codegraphrc.json

    Caller->>openRepo/openReadonlyWithNative: "customDbPath = "/proj/.codegraph/graph.db""
    openRepo/openReadonlyWithNative->>openRepo/openReadonlyWithNative: "rootDir = dirname(dirname(resolve(customDbPath))) → "/proj""
    openRepo/openReadonlyWithNative->>loadConfig: loadConfig("/proj")
    loadConfig->>fs: read /proj/.codegraphrc.json
    fs-->>loadConfig: "{ build: { engine: "native" } }"
    loadConfig-->>openRepo/openReadonlyWithNative: config
    openRepo/openReadonlyWithNative->>openRepo/openReadonlyWithNative: "engine = opts.engine ?? config.build.engine ?? "auto""
    openRepo/openReadonlyWithNative-->>Caller: "{ repo, close() }"
Loading

Reviews (2): Last reviewed commit: "fix: resolve merge conflicts with main" | Re-trigger Greptile

Comment thread src/db/connection.ts
const engine = opts.engine || process.env.CODEGRAPH_ENGINE || 'auto';
// Derive rootDir from customDbPath so loadConfig reads the right project config.
// Convention: customDbPath = <rootDir>/.codegraph/graph.db
const rootDir = customDbPath ? path.dirname(path.dirname(path.resolve(customDbPath))) : undefined;

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 Hardcoded 2-level ascent assumes default dbPath depth

path.dirname(path.dirname(...)) works when customDbPath follows the default <rootDir>/.codegraph/graph.db convention (2 levels deep). If a project has overridden build.dbPath to a shallower or deeper relative path (e.g. "graph.db" at root, or "data/db/graph.db" 3 levels deep), the derived rootDir will point to the wrong directory. loadConfig would then silently return defaults (or even pick up an unrelated config), causing the wrong engine to be selected for that repo. The same pattern appears in openReadonlyWithNative at line 432.

Fix in Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

2 functions changed36 callers affected across 27 files

  • openRepo in src/db/connection.ts:370 (28 transitive callers)
  • openReadonlyWithNative in src/db/connection.ts:430 (8 transitive callers)

@carlos-alm carlos-alm merged commit 6dbb307 into main Jun 18, 2026
22 checks passed
@carlos-alm carlos-alm deleted the chore/issue-resolution-baseline branch June 18, 2026 19:07
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 18, 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.

connection: loadConfig() uses process.cwd() instead of DB root when customDbPath is set

1 participant