@@ -233,6 +233,10 @@ class DirectoryWatcher extends EventEmitter {
233233 this . files = new Map ( ) ;
234234 /** @type {Map<string, number> } */
235235 this . filesWithoutCase = new Map ( ) ;
236+ // whether names in this directory ignore case, learned the first time a
237+ // watched path differs from a file on disk only by case
238+ /** @type {boolean | undefined } */
239+ this . caseInsensitive = undefined ;
236240 /** @type {Map<string, Watcher<DirectoryWatcherEvents> | boolean> } */
237241 this . directories = new Map ( ) ;
238242 /** @type {Map<string, Watcher<FileWatcherEvents>> } */
@@ -1155,14 +1159,63 @@ class DirectoryWatcher extends EventEmitter {
11551159 for ( const watcher of watchers ) {
11561160 const { path } = watcher ;
11571161 if ( ! fileTimestamps . has ( path ) ) {
1158- fileTimestamps . set ( path , null ) ;
1162+ // on a case-insensitive file system a path with other casing
1163+ // is the same file and must change with it
1164+ const file = this . getFileWithOtherCase ( path ) ;
1165+ fileTimestamps . set (
1166+ path ,
1167+ ( file !== undefined && this . files . get ( file ) ) || null ,
1168+ ) ;
11591169 }
11601170 }
11611171 }
11621172 }
11631173 return safeTime ;
11641174 }
11651175
1176+ /**
1177+ * @param {string } target a watched path that is not in `files`
1178+ * @returns {string | undefined } the file it names on a case-insensitive file system
1179+ */
1180+ getFileWithOtherCase ( target ) {
1181+ if ( this . caseInsensitive === false ) return ;
1182+ const key = withoutCase ( target ) ;
1183+ if ( ! this . filesWithoutCase . has ( key ) ) return ;
1184+ let file ;
1185+ for ( const item of this . files . keys ( ) ) {
1186+ if ( withoutCase ( item ) === key ) {
1187+ file = item ;
1188+ break ;
1189+ }
1190+ }
1191+ if ( file === undefined ) return ;
1192+ if ( this . caseInsensitive === undefined ) {
1193+ let fileStats ;
1194+ try {
1195+ fileStats = fs . lstatSync ( file ) ;
1196+ } catch ( _err ) {
1197+ // changed meanwhile, the next scan updates `files`
1198+ return ;
1199+ }
1200+ try {
1201+ const targetStats = fs . lstatSync ( target ) ;
1202+ this . caseInsensitive =
1203+ targetStats . ino === fileStats . ino &&
1204+ targetStats . dev === fileStats . dev ;
1205+ } catch ( err ) {
1206+ // only a missing name proves case matters; EBUSY and the like
1207+ // leave it to the next collection
1208+ if ( /** @type {NodeJS.ErrnoException } */ ( err ) . code !== "ENOENT" ) {
1209+ return ;
1210+ }
1211+ // a file removed meanwhile says nothing about the file system
1212+ if ( ! fs . existsSync ( file ) ) return ;
1213+ this . caseInsensitive = false ;
1214+ }
1215+ }
1216+ return this . caseInsensitive ? file : undefined ;
1217+ }
1218+
11661219 close ( ) {
11671220 if ( this . closed ) return ;
11681221 this . closed = true ;
0 commit comments