()
| 1069 | // fs.watchFile polls stat on the libuv threadpool and only calls us when mtime |
| 1070 | // changed — a stalled stat never blocks the main thread. |
| 1071 | function startGlobalConfigFreshnessWatcher(): void { |
| 1072 | if (freshnessWatcherStarted || process.env.NODE_ENV === 'test') return |
| 1073 | freshnessWatcherStarted = true |
| 1074 | const file = getGlobalNcodeFile() |
| 1075 | watchFile( |
| 1076 | file, |
| 1077 | { interval: CONFIG_FRESHNESS_POLL_MS, persistent: false }, |
| 1078 | curr => { |
| 1079 | // Our own writes fire this too — the write-through's Date.now() |
| 1080 | // overshoot makes cache.mtime > file mtime, so we skip the re-read. |
| 1081 | // Bun/Node also fire with curr.mtimeMs=0 when the file doesn't exist |
| 1082 | // (initial callback or deletion) — the <= handles that too. |
| 1083 | if (curr.mtimeMs <= globalConfigCache.mtime) return |
| 1084 | void getFsImplementation() |
| 1085 | .readFile(file, { encoding: 'utf-8' }) |
| 1086 | .then(content => { |
| 1087 | // A write-through may have advanced the cache while we were reading; |
| 1088 | // don't regress to the stale snapshot watchFile stat'd. |
| 1089 | if (curr.mtimeMs <= globalConfigCache.mtime) return |
| 1090 | const parsed = safeParseJSON(stripBOM(content)) |
| 1091 | if (parsed === null || typeof parsed !== 'object') return |
| 1092 | globalConfigCache = { |
| 1093 | config: migrateConfigFields({ |
| 1094 | ...createDefaultGlobalConfig(), |
| 1095 | ...(parsed as Partial<GlobalConfig>), |
| 1096 | }), |
| 1097 | mtime: curr.mtimeMs, |
| 1098 | } |
| 1099 | lastReadFileStats = { mtime: curr.mtimeMs, size: curr.size } |
| 1100 | }) |
| 1101 | .catch(() => {}) |
| 1102 | }, |
| 1103 | ) |
| 1104 | registerCleanup(async () => { |
| 1105 | unwatchFile(file) |
| 1106 | freshnessWatcherStarted = false |
| 1107 | }) |
| 1108 | } |
| 1109 | |
| 1110 | // Write-through: what we just wrote IS the new config. cache.mtime overshoots |
| 1111 | // the file's real mtime (Date.now() is recorded after the write) so the |
no test coverage detected