@@ -24,6 +24,7 @@ import type {Stats} from 'fs';
24
24
import * as common from './common' ;
25
25
import RecrawlWarning from './RecrawlWarning' ;
26
26
import assert from 'assert' ;
27
+ import { createHash } from 'crypto' ;
27
28
import EventEmitter from 'events' ;
28
29
import watchman from 'fb-watchman' ;
29
30
import * as fs from 'graceful-fs' ;
@@ -36,7 +37,7 @@ const CHANGE_EVENT = common.CHANGE_EVENT;
36
37
const DELETE_EVENT = common . DELETE_EVENT ;
37
38
const ADD_EVENT = common . ADD_EVENT ;
38
39
const ALL_EVENT = common . ALL_EVENT ;
39
- const SUB_NAME = 'sane-sub ' ;
40
+ const SUB_PREFIX = 'metro-file-map ' ;
40
41
41
42
/**
42
43
* Watches `dir`.
@@ -48,6 +49,7 @@ export default class WatchmanWatcher extends EventEmitter {
48
49
globs : $ReadOnlyArray < string > ;
49
50
hasIgnore : boolean ;
50
51
root : string ;
52
+ subscriptionName : string ;
51
53
watchProjectInfo : ?$ReadOnly < {
52
54
relativePath : string ,
53
55
root : string ,
@@ -56,8 +58,17 @@ export default class WatchmanWatcher extends EventEmitter {
56
58
57
59
constructor ( dir : string , opts : WatcherOptions ) {
58
60
super ( ) ;
61
+
59
62
common . assignOptions ( this , opts ) ;
60
63
this . root = path . resolve ( dir ) ;
64
+
65
+ // Use a unique subscription name per process per watched directory
66
+ const watchKey = createHash ( 'md5' ) . update ( this . root ) . digest ( 'hex' ) ;
67
+ const readablePath = this . root
68
+ . replace ( / [ \/ \\ ] / g, '-' ) // \ and / to -
69
+ . replace ( / [ ^ \- \w ] / g, '' ) ; // Remove non-word/hyphen
70
+ this . subscriptionName = `${ SUB_PREFIX } -${ process . pid } -${ readablePath } -${ watchKey } ` ;
71
+
61
72
this . _init ( ) ;
62
73
}
63
74
@@ -141,7 +152,7 @@ export default class WatchmanWatcher extends EventEmitter {
141
152
}
142
153
143
154
self . client . command (
144
- [ 'subscribe' , getWatchRoot ( ) , SUB_NAME , options ] ,
155
+ [ 'subscribe' , getWatchRoot ( ) , self . subscriptionName , options ] ,
145
156
onSubscribe ,
146
157
) ;
147
158
}
@@ -165,14 +176,20 @@ export default class WatchmanWatcher extends EventEmitter {
165
176
*/
166
177
_handleChangeEvent ( resp : WatchmanSubscriptionEvent ) {
167
178
debug (
168
- 'Received subscription response: %s (fresh: %s} , files: %s, enter: %s, leave: %s)' ,
179
+ 'Received subscription response: %s (fresh: %s, files: %s, enter: %s, leave: %s)' ,
169
180
resp . subscription ,
170
181
resp . is_fresh_instance ,
171
182
resp . files ?. length ,
172
183
resp [ 'state-enter' ] ,
173
184
resp [ 'state-leave' ] ,
174
185
) ;
175
- assert . equal ( resp . subscription , SUB_NAME , 'Invalid subscription event.' ) ;
186
+
187
+ assert . equal (
188
+ resp . subscription ,
189
+ this . subscriptionName ,
190
+ 'Invalid subscription event.' ,
191
+ ) ;
192
+
176
193
if ( resp . is_fresh_instance ) {
177
194
this . emit ( 'fresh_instance' ) ;
178
195
}
0 commit comments