* 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)
| 2696 | * instance, a cached one, or a newly opened (and cached) one. |
| 2697 | */ |
| 2698 | private openProjectRoot(resolvedRoot: string, canonicalRoot: string): CodeGraph { |
| 2699 | // If the path resolves to the default project, reuse the already-open |
| 2700 | // default instance rather than opening a SECOND connection to the same DB. |
| 2701 | // A duplicate connection serializes reads against the watcher's auto-sync |
| 2702 | // writes; when WAL isn't in effect (e.g. a filesystem without shared-memory |
| 2703 | // support) that surfaces as intermittent |
| 2704 | // "database is locked" on concurrent tool calls. See issue #238. The |
| 2705 | // default instance is owned/closed by the server, so it's never cached. |
| 2706 | // Another spelling of the same root counts too (#1057). |
| 2707 | if (this.cg && isSameIndexRoot(this.cg.getProjectRoot(), resolvedRoot)) { |
| 2708 | return this.freshen(this.cg); |
| 2709 | } |
| 2710 | |
| 2711 | // Cache the open DB connection by CANONICAL ROOT only — never by the input |
| 2712 | // path. One key per instance means closeAll() closes each exactly once, and |
| 2713 | // a changed resolution maps to a different entry instead of a stale hit. |
| 2714 | const cached = this.projectCache.get(canonicalRoot); |
| 2715 | if (cached) { |
| 2716 | // Refresh LRU position. |
| 2717 | this.projectCache.delete(canonicalRoot); |
| 2718 | this.projectCache.set(canonicalRoot, cached); |
| 2719 | this.projectUsedAt.set(canonicalRoot, Date.now()); |
| 2720 | return this.freshen(cached); |
| 2721 | } |
| 2722 | |
| 2723 | // Compare current identities on every cache miss: a previously seen alias |
| 2724 | // may have been retargeted or recreated since the last call (#1057). |
| 2725 | for (const [root, open] of this.projectCache) { |
| 2726 | if (isSameIndexRoot(root, resolvedRoot)) { |
| 2727 | this.projectCache.delete(root); |
| 2728 | this.projectCache.set(root, open); |
| 2729 | this.projectUsedAt.set(root, Date.now()); |
| 2730 | return this.freshen(open); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | // Indexes an interrupted bulk load dropped are rebuilt off this thread, |
| 2735 | // before the project is served (awaitIndexRecovery, #1887). |
| 2736 | const open = () => loadCodeGraph().openSync(canonicalRoot, { deferIndexRecovery: true }); |
| 2737 | const cg = this.projectLifecycle?.open(canonicalRoot, open) ?? open(); |
| 2738 | this.projectCache.set(canonicalRoot, cg); |
| 2739 | this.projectUsedAt.set(canonicalRoot, Date.now()); |
| 2740 | this.trimProjects(); |
| 2741 | return cg; |
| 2742 | } |
| 2743 | |
| 2744 | /** |
| 2745 | * The nested git repository `projectPath` lives in when the index the |
no test coverage detected