| 1130 | } |
| 1131 | |
| 1132 | async sync(options: IndexOptions = {}): Promise<SyncResult> { |
| 1133 | const { |
| 1134 | ESM_IMPORT_LANGUAGES, |
| 1135 | aliasMayName, |
| 1136 | changedModuleConfigs, |
| 1137 | changedRoutes, |
| 1138 | createImportReader, |
| 1139 | hasSynthesisPattern, |
| 1140 | isJsxChildCandidate, |
| 1141 | moduleConfigLanguages, |
| 1142 | parseManifestStamp, |
| 1143 | parseModuleConfigSnapshot, |
| 1144 | reopenNameMatchedImports, |
| 1145 | reopenReferencesThroughAddedFiles, |
| 1146 | reopenReferencesThroughBarrels, |
| 1147 | reopenReferencesThroughChangedDefaults, |
| 1148 | reopenReferencesThroughDeclaredPackages, |
| 1149 | reopenReferencesThroughRetargetedModules, |
| 1150 | trustedManifestList, |
| 1151 | } = engine(); |
| 1152 | return this.indexMutex.withLock(async () => { |
| 1153 | const startedAt = Date.now(); |
| 1154 | try { |
| 1155 | this.fileLock.acquire(); |
| 1156 | } catch (err) { |
| 1157 | throw new LockUnavailableError( |
| 1158 | `Sync could not acquire the file lock; retry when the index is available. ${err instanceof Error ? err.message : String(err)}` |
| 1159 | ); |
| 1160 | } |
| 1161 | // A full rebuild in another process (`codegraph index` → recreate) |
| 1162 | // unlinks the database and creates a new file at the same path. A |
| 1163 | // long-lived instance — the MCP daemon's watcher — would otherwise keep |
| 1164 | // "syncing" into the dead inode, and nothing it wrote there is visible |
| 1165 | // to anyone (#1902). Follow the path before writing (one stat), and |
| 1166 | // widen a scoped sync to a full one: whatever the old handle absorbed |
| 1167 | // since the rebuild is gone, so the new file has to be reconciled whole. |
| 1168 | // If the reopen fails (the rebuild is mid-way), report lock contention |
| 1169 | // so the watcher keeps its pending files and retries. |
| 1170 | try { |
| 1171 | this.reopenReplacedDatabase(); |
| 1172 | } catch (err) { |
| 1173 | this.fileLock.release(); |
| 1174 | throw new LockUnavailableError( |
| 1175 | `Sync could not open the rebuilt index yet; retry when the rebuild finishes. ${err instanceof Error ? err.message : String(err)}` |
| 1176 | ); |
| 1177 | } |
| 1178 | if (this.pendingFullReconcile) { |
| 1179 | // `codegraph index` recreates the file, THEN takes the write lock in |
| 1180 | // indexAll. A sync landing in that gap would otherwise run a full |
| 1181 | // reconcile of the empty file and hold the lock the rebuild is about |
| 1182 | // to ask for. A fresh file with no index_state yet is that rebuild: |
| 1183 | // step aside (lock contention, the watcher retries) and reconcile in |
| 1184 | // full once it is done. Bounded, so a rebuild that died before |
| 1185 | // indexing does not park the watcher forever. |
| 1186 | if (this.getIndexState() === null && this.isFreshlyRecreated()) { |
| 1187 | this.fileLock.release(); |
| 1188 | throw new LockUnavailableError('A rebuild of this index is in progress; retry when it finishes.'); |
| 1189 | } |