File tree 2 files changed +38
-3
lines changed
2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -157,11 +157,16 @@ class FSWatcher extends EventEmitter {
157
157
persistent : this . #options. persistent ,
158
158
} , ( eventType , filename ) => {
159
159
const existingStat = this . #files. get ( file ) ;
160
- const currentStats = statSync ( file ) ;
160
+ let currentStats ;
161
161
162
- this . #files. set ( file , currentStats ) ;
162
+ try {
163
+ currentStats = statSync ( file ) ;
164
+ this . #files. set ( file , currentStats ) ;
165
+ } catch {
166
+ // This happens if the file was removed
167
+ }
163
168
164
- if ( currentStats . birthtimeMs === 0 && existingStat . birthtimeMs !== 0 ) {
169
+ if ( currentStats === undefined || ( currentStats . birthtimeMs === 0 && existingStat . birthtimeMs !== 0 ) ) {
165
170
// The file is now deleted
166
171
this . #files. delete ( file ) ;
167
172
this . #watchers. delete ( file ) ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const common = require ( '../common' ) ;
4
+ const tmpdir = require ( '../common/tmpdir' ) ;
5
+ const fs = require ( 'fs' ) ;
6
+
7
+ if ( common . isSunOS )
8
+ common . skip ( 'SunOS behaves differently' ) ;
9
+
10
+ tmpdir . refresh ( ) ;
11
+
12
+ fs . mkdirSync ( tmpdir . resolve ( './parent/child' ) , { recursive : true } ) ;
13
+
14
+ fs . writeFileSync ( tmpdir . resolve ( './parent/child/test.tmp' ) , 'test' ) ;
15
+
16
+ const toWatch = tmpdir . resolve ( './parent' ) ;
17
+
18
+ const onFileUpdate = common . mustCallAtLeast ( ( eventType , filename ) => {
19
+ // We are only checking for the filename to avoid having Windows, Linux and Mac specific assertions
20
+ if ( fs . readdirSync ( tmpdir . resolve ( './parent' ) ) . length === 0 ) {
21
+ watcher . close ( ) ;
22
+ }
23
+ } , 1 ) ;
24
+
25
+ const watcher = fs . watch ( toWatch , { recursive : true } , onFileUpdate ) ;
26
+
27
+ // We must wait a bit `fs.rm()` to let the watcher be set up properly
28
+ setTimeout ( ( ) => {
29
+ fs . rm ( tmpdir . resolve ( './parent/child' ) , { recursive : true } , common . mustCall ( ) ) ;
30
+ } , common . platformTimeout ( 500 ) ) ;
You can’t perform that action at this time.
0 commit comments