* 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)
| 2413 | * similar to how git finds .git/ directories. |
| 2414 | */ |
| 2415 | private getCodeGraph(projectPath?: string): CodeGraph { |
| 2416 | if (!projectPath) { |
| 2417 | if (!this.cg) { |
| 2418 | if (this.defaultOpenFailure) throw this.defaultOpenFailure; |
| 2419 | const searched = this.defaultProjectHint ?? process.cwd(); |
| 2420 | // A refused default is the same "pass projectPath" state, success-shaped |
| 2421 | // like the rest: the agent can still query every other project. |
| 2422 | const why = this.defaultRootRefusal |
| 2423 | ? `${this.defaultRootRefusal}. CodeGraph never uses a credential, config or system folder ` + |
| 2424 | 'as a project, so this session has no default project. To query a project outside such ' + |
| 2425 | 'folders, target it explicitly:\n' |
| 2426 | : `Searched for a .codegraph/ directory starting from: ${searched}\n` + |
| 2427 | this.formatKnownSubprojects() + |
| 2428 | 'Either the server root has no index of its own (e.g. a monorepo where only ' + |
| 2429 | "sub-projects are indexed), or the MCP client launched the server outside your " + |
| 2430 | 'project without reporting the workspace root. Either way, target the project ' + |
| 2431 | 'explicitly:\n'; |
| 2432 | throw new NotIndexedError( |
| 2433 | 'No CodeGraph project is loaded for this session.\n' + |
| 2434 | why + |
| 2435 | ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' + |
| 2436 | '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' + |
| 2437 | ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' + |
| 2438 | 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' + |
| 2439 | "project (the user can run 'codegraph init' there to enable it) — you can still query " + |
| 2440 | 'other indexed projects by projectPath in the same session.' |
| 2441 | ); |
| 2442 | } |
| 2443 | return this.freshen(this.cg); |
| 2444 | } |
| 2445 | |
| 2446 | // Reject sensitive system directories before opening. Only validate a |
| 2447 | // path that actually exists — a nested or not-yet-created sub-path of a |
| 2448 | // real project must still be allowed to resolve UP to its .codegraph/ |
| 2449 | // root below (issue #238), so we don't run the existence-checking |
| 2450 | // validator on paths that are meant to walk up. |
| 2451 | if (existsSync(projectPath)) { |
| 2452 | const pathError = validateProjectPath(projectPath); |
| 2453 | if (pathError) { |
| 2454 | throw new PathRefusalError(pathError); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk |
| 2459 | // is cheap (a few existsSync up the tree) and is the only thing that |
| 2460 | // notices a path whose index root CHANGED since it was first seen — most |
| 2461 | // importantly a git worktree that gained its own .codegraph/ after the |
| 2462 | // (long-lived) server first resolved it up to the parent checkout. We used |
| 2463 | // to short-circuit on a `projectCache[projectPath]` entry before resolving, |
| 2464 | // which pinned that first resolution for the server's whole lifetime, so a |
| 2465 | // worktree kept being served the parent checkout's index until restart |
| 2466 | // (#926). The DB connection itself is still cached (by resolved root, |
| 2467 | // below), so re-resolving costs only the stat walk, never a reopen. |
| 2468 | const resolvedRoot = findNearestCodeGraphRoot(projectPath); |
| 2469 | // Two spellings of one root (a symlinked checkout, `/tmp` vs |
| 2470 | // `/private/tmp`) must share one connection and one watcher (#1835). |
| 2471 | const canonicalRoot = resolvedRoot ? canonicalPath(resolvedRoot) : null; |
| 2472 |