* 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)
| 2207 | * similar to how git finds .git/ directories. |
| 2208 | */ |
| 2209 | private getCodeGraph(projectPath?: string): CodeGraph { |
| 2210 | if (!projectPath) { |
| 2211 | if (!this.cg) { |
| 2212 | if (this.defaultOpenFailure) throw this.defaultOpenFailure; |
| 2213 | const searched = this.defaultProjectHint ?? process.cwd(); |
| 2214 | throw new NotIndexedError( |
| 2215 | 'No CodeGraph project is loaded for this session.\n' + |
| 2216 | `Searched for a .codegraph/ directory starting from: ${searched}\n` + |
| 2217 | this.formatKnownSubprojects() + |
| 2218 | 'Either the server root has no index of its own (e.g. a monorepo where only ' + |
| 2219 | "sub-projects are indexed), or the MCP client launched the server outside your " + |
| 2220 | 'project without reporting the workspace root. Either way, target the project ' + |
| 2221 | 'explicitly:\n' + |
| 2222 | ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' + |
| 2223 | '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' + |
| 2224 | ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' + |
| 2225 | 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' + |
| 2226 | "project (the user can run 'codegraph init' there to enable it) — you can still query " + |
| 2227 | 'other indexed projects by projectPath in the same session.' |
| 2228 | ); |
| 2229 | } |
| 2230 | return this.freshen(this.cg); |
| 2231 | } |
| 2232 | |
| 2233 | // Reject sensitive system directories before opening. Only validate a |
| 2234 | // path that actually exists — a nested or not-yet-created sub-path of a |
| 2235 | // real project must still be allowed to resolve UP to its .codegraph/ |
| 2236 | // root below (issue #238), so we don't run the existence-checking |
| 2237 | // validator on paths that are meant to walk up. |
| 2238 | if (existsSync(projectPath)) { |
| 2239 | const pathError = validateProjectPath(projectPath); |
| 2240 | if (pathError) { |
| 2241 | throw new PathRefusalError(pathError); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk |
| 2246 | // is cheap (a few existsSync up the tree) and is the only thing that |
| 2247 | // notices a path whose index root CHANGED since it was first seen — most |
| 2248 | // importantly a git worktree that gained its own .codegraph/ after the |
| 2249 | // (long-lived) server first resolved it up to the parent checkout. We used |
| 2250 | // to short-circuit on a `projectCache[projectPath]` entry before resolving, |
| 2251 | // which pinned that first resolution for the server's whole lifetime, so a |
| 2252 | // worktree kept being served the parent checkout's index until restart |
| 2253 | // (#926). The DB connection itself is still cached (by resolved root, |
| 2254 | // below), so re-resolving costs only the stat walk, never a reopen. |
| 2255 | const resolvedRoot = findNearestCodeGraphRoot(projectPath); |
| 2256 | // Two spellings of one root (a symlinked checkout, `/tmp` vs |
| 2257 | // `/private/tmp`) must share one connection and one watcher (#1835). |
| 2258 | const canonicalRoot = resolvedRoot ? canonicalPath(resolvedRoot) : null; |
| 2259 | |
| 2260 | if (!resolvedRoot || !canonicalRoot) { |
| 2261 | throw new NotIndexedError( |
| 2262 | `The project at ${projectPath} isn't indexed with codegraph (no .codegraph/ directory found ` + |
| 2263 | 'walking up from it), so codegraph cannot query it. Use your built-in tools (Read/Grep/Glob) ' + |
| 2264 | "for that codebase instead, and don't call codegraph for it again this session. " + |
| 2265 | "Indexing is the user's decision — they can run 'codegraph init' in that project to enable it." |
| 2266 | ); |