1- import { existsSync , watch , type FSWatcher } from 'node:fs'
1+ import { existsSync , watchFile } from 'node:fs'
22import { readFile } from 'node:fs/promises'
33import { homedir } from 'node:os'
44import path from 'node:path'
@@ -21,12 +21,8 @@ const CONFIG_FILENAMES = [
2121
2222const PYPROJECT_FILENAME = 'pyproject.toml'
2323
24- /** Basenames that change which configuration applies when created or changed. */
25- const CONFIG_BASENAMES = new Set ( [
26- '.tombi.toml' ,
27- 'tombi.toml' ,
28- PYPROJECT_FILENAME ,
29- ] )
24+ /** How often discovered config files are polled (in milliseconds). */
25+ const WATCH_INTERVAL = 500
3026
3127/** Project level candidates, for a directory and each of its ancestors. */
3228const getProjectCandidates = ( directory : string ) : string [ ] => {
@@ -132,7 +128,7 @@ const projectCache = new Map<
132128> ( )
133129let globalConfigCache : Promise < DiscoveredTombiConfig | undefined > | undefined
134130
135- const configWatchers = new Map < string , FSWatcher > ( )
131+ const watchedConfigs = new Set < string > ( )
136132
137133/**
138134 * Drop the cached project results whose search traverses `directory`: the
@@ -152,37 +148,26 @@ const invalidateGlobalConfig = () => {
152148}
153149
154150/**
155- * Best effort watch of a config file so long-lived consumers (an editor or a
156- * daemon) pick up changes without restarting. The directory is watched rather
157- * than the file because editors usually save atomically by renaming a temporary
158- * file over the target. Watchers stay alive for the process lifetime and are
159- * unref'd so they never keep it running.
151+ * Best effort watch of a discovered config file so long-lived consumers (an
152+ * editor or a daemon) pick up changes without restarting. `fs.watchFile` polls
153+ * the file stat instead of using `fs.watch`, which is unreliable for the atomic
154+ * saves editors perform and crashes the process on Windows.
160155 */
161156const watchConfig = ( configPath : string , onConfigChange : ( ) => void ) => {
162- if ( configWatchers . has ( configPath ) ) {
157+ if ( watchedConfigs . has ( configPath ) ) {
163158 return
164159 }
160+ watchedConfigs . add ( configPath )
165161
166- const directory = path . dirname ( configPath )
167- const basename = path . basename ( configPath )
168-
169- try {
170- const watcher = watch ( directory , ( _event , filename ) => {
171- const changed = filename == null ? undefined : path . basename ( filename )
172- if (
173- changed == null ||
174- changed === basename ||
175- CONFIG_BASENAMES . has ( changed )
176- ) {
162+ watchFile (
163+ configPath ,
164+ { interval : WATCH_INTERVAL , persistent : false } ,
165+ ( curr , prev ) => {
166+ if ( curr . mtimeMs !== prev . mtimeMs || curr . size !== prev . size ) {
177167 onConfigChange ( )
178168 }
179- } )
180- watcher . on ( 'error' , onConfigChange )
181- watcher . unref ( )
182- configWatchers . set ( configPath , watcher )
183- } catch {
184- // Watching is best effort, ignore runtimes that do not support it.
185- }
169+ } ,
170+ )
186171}
187172
188173const getProjectConfig = ( directory : string ) => {
0 commit comments