fix(desktop): keep a failed watch snapshot distinct from an empty directory

The storm-breaker poll mapped a throwing snapshot to '', which is also the
valid readdir snapshot of an empty directory, so an empty watched directory
vanishing after a trip was never reported. Failures are now null.
This commit is contained in:
Austin Pickett
2026-10-01 09:50:47 -04:00
parent 4d2dfe5261
commit 8a0aaf9966
2 changed files with 27 additions and 2 deletions

View File

@@ -179,4 +179,24 @@ describe('guardedWatch', () => {
expect(onPollChange).toHaveBeenCalledTimes(1)
})
test('an empty directory vanishing is still a change (error is not the empty snapshot)', () => {
let gone = false
const { fw, onPollChange } = setup('win32', {
snapshot: () => {
if (gone) {
throw new Error('ENOENT')
}
return ''
}
})
fw.emit(WATCH_STORM_MAX_EVENTS + 1)
gone = true
vi.advanceTimersByTime(WATCH_STORM_POLL_MS)
expect(onPollChange).toHaveBeenCalledTimes(1)
})
})

View File

@@ -55,11 +55,16 @@ export function stormBreakerArmed(platform: string): boolean {
return platform === 'win32'
}
function safeSnapshot(snapshot: () => string): string {
/**
* `null` = the snapshot failed (target removed or unreadable). Kept distinct
* from the empty string, which is the valid snapshot of an empty directory, so
* an empty directory vanishing still compares as a change.
*/
function safeSnapshot(snapshot: () => string): string | null {
try {
return snapshot()
} catch {
return ''
return null
}
}