* Get CodeGraph instance for a project * * If projectPath is provided, opens that project's CodeGraph (cached). * Otherwise returns the default CodeGraph instance. * * Walks up parent directories to find the nearest .codegraph/ folder, * similar to how git finds .git/ directories.
(projectPath?: string)
| 2481 | * similar to how git finds .git/ directories. |
| 2482 | */ |
| 2483 | private getCodeGraph(projectPath?: string): CodeGraph { |
| 2484 | if (!projectPath) { |
| 2485 | if (!this.cg) { |
| 2486 | if (this.defaultOpenFailure) throw this.defaultOpenFailure; |
| 2487 | const searched = this.defaultProjectHint ?? process.cwd(); |
| 2488 | // A refused default is the same "pass projectPath" state, success-shaped |
| 2489 | // like the rest: the agent can still query every other project. |
| 2490 | const why = this.defaultRootRefusal |
| 2491 | ? `${this.defaultRootRefusal}. CodeGraph never uses a credential, config or system folder ` + |
| 2492 | 'as a project, so this session has no default project. To query a project outside such ' + |
| 2493 | 'folders, target it explicitly:\n' |
| 2494 | : `Searched for a .codegraph/ directory starting from: ${searched}\n` + |
| 2495 | this.formatKnownSubprojects() + |
| 2496 | 'Either the server root has no index of its own (e.g. a monorepo where only ' + |
| 2497 | "sub-projects are indexed), or the MCP client launched the server outside your " + |
| 2498 | 'project without reporting the workspace root. Either way, target the project ' + |
| 2499 | 'explicitly:\n'; |
| 2500 | throw new NotIndexedError( |
| 2501 | 'No CodeGraph project is loaded for this session.\n' + |
| 2502 | why + |
| 2503 | ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' + |
| 2504 | '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' + |
| 2505 | ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' + |
| 2506 | 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' + |
| 2507 | "project (the user can run 'codegraph init' there to enable it) — you can still query " + |
| 2508 | 'other indexed projects by projectPath in the same session.' |
| 2509 | ); |
| 2510 | } |
| 2511 | return this.freshen(this.cg); |
| 2512 | } |
| 2513 | |
| 2514 | // Reject sensitive system directories before opening. Only validate a |
| 2515 | // path that actually exists — a nested or not-yet-created sub-path of a |
| 2516 | // real project must still be allowed to resolve UP to its .codegraph/ |
| 2517 | // root below (issue #238), so we don't run the existence-checking |
| 2518 | // validator on paths that are meant to walk up. |
| 2519 | if (existsSync(projectPath)) { |
| 2520 | const pathError = validateProjectPath(projectPath); |
| 2521 | if (pathError) { |
| 2522 | throw new PathRefusalError(pathError); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk |
| 2527 | // is cheap (a few existsSync up the tree) and is the only thing that |
| 2528 | // notices a path whose index root CHANGED since it was first seen — most |
| 2529 | // importantly a git worktree that gained its own .codegraph/ after the |
| 2530 | // (long-lived) server first resolved it up to the parent checkout. We used |
| 2531 | // to short-circuit on a `projectCache[projectPath]` entry before resolving, |
| 2532 | // which pinned that first resolution for the server's whole lifetime, so a |
| 2533 | // worktree kept being served the parent checkout's index until restart |
| 2534 | // (#926). The DB connection itself is still cached (by resolved root, |
| 2535 | // below), so re-resolving costs only the stat walk, never a reopen. |
| 2536 | const resolvedRoot = findNearestCodeGraphRoot(projectPath); |
| 2537 | // Two spellings of one root (a symlinked checkout, `/tmp` vs |
| 2538 | // `/private/tmp`) must share one connection and one watcher (#1835). |
| 2539 | const canonicalRoot = resolvedRoot ? canonicalPath(resolvedRoot) : null; |
| 2540 |