* The open CodeGraph for an index root the up-walk resolved: the default * instance, a cached one, or a newly opened (and cached) one.
(resolvedRoot: string, canonicalRoot: string)
| 2511 | * instance, a cached one, or a newly opened (and cached) one. |
| 2512 | */ |
| 2513 | private openProjectRoot(resolvedRoot: string, canonicalRoot: string): CodeGraph { |
| 2514 | // If the path resolves to the default project, reuse the already-open |
| 2515 | // default instance rather than opening a SECOND connection to the same DB. |
| 2516 | // A duplicate connection serializes reads against the watcher's auto-sync |
| 2517 | // writes; when WAL isn't in effect (e.g. a filesystem without shared-memory |
| 2518 | // support) that surfaces as intermittent |
| 2519 | // "database is locked" on concurrent tool calls. See issue #238. The |
| 2520 | // default instance is owned/closed by the server, so it's never cached. |
| 2521 | // Another spelling of the same root counts too (#1057). |
| 2522 | if (this.cg && isSameIndexRoot(this.cg.getProjectRoot(), resolvedRoot)) { |
| 2523 | return this.freshen(this.cg); |
| 2524 | } |
| 2525 | |
| 2526 | // Cache the open DB connection by CANONICAL ROOT only — never by the input |
| 2527 | // path. One key per instance means closeAll() closes each exactly once, and |
| 2528 | // a changed resolution maps to a different entry instead of a stale hit. |
| 2529 | const cached = this.projectCache.get(canonicalRoot); |
| 2530 | if (cached) { |
| 2531 | // Refresh LRU position. |
| 2532 | this.projectCache.delete(canonicalRoot); |
| 2533 | this.projectCache.set(canonicalRoot, cached); |
| 2534 | this.projectUsedAt.set(canonicalRoot, Date.now()); |
| 2535 | return this.freshen(cached); |
| 2536 | } |
| 2537 | |
| 2538 | // Compare current identities on every cache miss: a previously seen alias |
| 2539 | // may have been retargeted or recreated since the last call (#1057). |
| 2540 | for (const [root, open] of this.projectCache) { |
| 2541 | if (isSameIndexRoot(root, resolvedRoot)) { |
| 2542 | this.projectCache.delete(root); |
| 2543 | this.projectCache.set(root, open); |
| 2544 | this.projectUsedAt.set(root, Date.now()); |
| 2545 | return this.freshen(open); |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | // Indexes an interrupted bulk load dropped are rebuilt off this thread, |
| 2550 | // before the project is served (awaitIndexRecovery, #1887). |
| 2551 | const open = () => loadCodeGraph().openSync(canonicalRoot, { deferIndexRecovery: true }); |
| 2552 | const cg = this.projectLifecycle?.open(canonicalRoot, open) ?? open(); |
| 2553 | this.projectCache.set(canonicalRoot, cg); |
| 2554 | this.projectUsedAt.set(canonicalRoot, Date.now()); |
| 2555 | this.trimProjects(); |
| 2556 | return cg; |
| 2557 | } |
| 2558 | |
| 2559 | /** |
| 2560 | * The nested git repository `projectPath` lives in when the index the |
no test coverage detected