Skip to content

Commit 3b0e78a

Browse files
robhoganfacebook-github-bot
authored andcommittedOct 5, 2022
Use per-watch Watchman subscription names
Summary: Updates the Watchman subscription name from `sane-sub` (a legacy of [`sane`](https://github.com/amasad/sane)), to a unique string per simultanous subscription, incorporating a more identifiable `metro-file-map` prefix, the PID, and the watched directory. Reviewed By: huntie Differential Revision: D40023944 fbshipit-source-id: 41ab74c13055455405fe6a670481d5347aa88320
1 parent 4bace2a commit 3b0e78a

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed
 

‎packages/metro-file-map/src/watchers/WatchmanWatcher.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {Stats} from 'fs';
2424
import * as common from './common';
2525
import RecrawlWarning from './RecrawlWarning';
2626
import assert from 'assert';
27+
import {createHash} from 'crypto';
2728
import EventEmitter from 'events';
2829
import watchman from 'fb-watchman';
2930
import * as fs from 'graceful-fs';
@@ -36,7 +37,7 @@ const CHANGE_EVENT = common.CHANGE_EVENT;
3637
const DELETE_EVENT = common.DELETE_EVENT;
3738
const ADD_EVENT = common.ADD_EVENT;
3839
const ALL_EVENT = common.ALL_EVENT;
39-
const SUB_NAME = 'sane-sub';
40+
const SUB_PREFIX = 'metro-file-map';
4041

4142
/**
4243
* Watches `dir`.
@@ -48,6 +49,7 @@ export default class WatchmanWatcher extends EventEmitter {
4849
globs: $ReadOnlyArray<string>;
4950
hasIgnore: boolean;
5051
root: string;
52+
subscriptionName: string;
5153
watchProjectInfo: ?$ReadOnly<{
5254
relativePath: string,
5355
root: string,
@@ -56,8 +58,17 @@ export default class WatchmanWatcher extends EventEmitter {
5658

5759
constructor(dir: string, opts: WatcherOptions) {
5860
super();
61+
5962
common.assignOptions(this, opts);
6063
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+
6172
this._init();
6273
}
6374

@@ -141,7 +152,7 @@ export default class WatchmanWatcher extends EventEmitter {
141152
}
142153

143154
self.client.command(
144-
['subscribe', getWatchRoot(), SUB_NAME, options],
155+
['subscribe', getWatchRoot(), self.subscriptionName, options],
145156
onSubscribe,
146157
);
147158
}
@@ -165,14 +176,20 @@ export default class WatchmanWatcher extends EventEmitter {
165176
*/
166177
_handleChangeEvent(resp: WatchmanSubscriptionEvent) {
167178
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)',
169180
resp.subscription,
170181
resp.is_fresh_instance,
171182
resp.files?.length,
172183
resp['state-enter'],
173184
resp['state-leave'],
174185
);
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+
176193
if (resp.is_fresh_instance) {
177194
this.emit('fresh_instance');
178195
}

0 commit comments

Comments
 (0)
Please sign in to comment.