fix: (regression 3.0.4) it could happen that the config looked empty/reset on start, until restart

Massimo Melina committed Mar 1, 2026 at 20:10 UTC 48e82d254bbd5ef766a20a922a146439c592149b
1 file changed +4 -2
src/watchLoad.ts
+4 -2
@@ -19,7 +19,6 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
19 let retry: NodeJS.Timeout
20 let last: string | undefined
21 const emitter = new BetterEventEmitter()
22 - install(true)
22 const save = debounceAsync(async (data: string, { reparse=false }={}) => {
23 await fs.writeFile(path, data, 'utf8')
24 last = data
@@ -27,6 +26,7 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
26 await parser(data)
27 emitter.emit('change', last)
28 })
29 + install(true)
30 return { unwatch, save, emitter, getText: () => last, getPath: () => path }
31
32 function install(first=false) {
@@ -56,10 +56,12 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
56 if (doing) return
57 doing = true
58 try {
59 + await save.flush() // apply pending saves first
60 const text = await readFileWithBusyRetry(path).catch(e => { // ignore read errors
61 if (e.code === 'EPERM')
62 console.error("missing permissions on file", path) // warn user, who could be clueless about this problem
62 - return ''
63 + // keep the last good content on transient read failures so we don't apply accidental "empty file" state
64 + return e.code === 'ENOENT' ? '' : last
65 })
66 if (text === last)
67 return