Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix watcher path normalisation with Windows and Watchman #1423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ if (process.platform === 'win32') {
'packages/metro-config/src/__tests__/loadConfig-test.js',
'packages/metro-symbolicate/src/__tests__/symbolicate-test.js',
'packages/metro-file-map/src/__tests__/index-test.js',
'packages/metro-file-map/src/watchers/__tests__/WatchmanWatcher-test.js',
'packages/metro-file-map/src/crawlers/__tests__/node-test.js',
'packages/metro-file-map/src/watchers/__tests__/integration-test.js',

// resolveModulePath failed
'packages/metro-cache/src/stores/__tests__/FileStore-test.js',
Expand Down
15 changes: 12 additions & 3 deletions packages/metro-file-map/src/watchers/WatchmanWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
WatchmanWatchResponse,
} from 'fb-watchman';

import normalizePathSeparatorsToSystem from '../lib/normalizePathSeparatorsToSystem';
import {AbstractWatcher} from './AbstractWatcher';
import * as common from './common';
import RecrawlWarning from './RecrawlWarning';
Expand Down Expand Up @@ -105,9 +106,13 @@ export default class WatchmanWatcher extends AbstractWatcher {

handleWarning(resp);

// NB: Watchman outputs posix-separated paths even on Windows, convert
// them to system-native separators.
self.watchProjectInfo = {
relativePath: resp.relative_path ? resp.relative_path : '',
root: resp.watch,
relativePath: resp.relative_path
? normalizePathSeparatorsToSystem(resp.relative_path)
: '',
root: normalizePathSeparatorsToSystem(resp.watch),
};

self.client.command(['clock', getWatchRoot()], onClock);
Expand Down Expand Up @@ -231,14 +236,18 @@ export default class WatchmanWatcher extends AbstractWatcher {
);

const {
name: relativePath,
name: relativePosixPath,
new: isNew = false,
exists = false,
type,
mtime_ms,
size,
} = changeDescriptor;

// Watchman emits posix-separated paths on Windows, which is inconsistent
// with other watchers. Normalize to system-native separators.
const relativePath = normalizePathSeparatorsToSystem(relativePosixPath);

debug(
'Handling change to: %s (new: %s, exists: %s, type: %s)',
relativePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@ const cmdCallback = <T>(err: ?Error, result: Partial<T>): void => {
mockClient.command.mock.lastCall[1](err, result);
};

// Convenience function to write paths with posix separators but convert them
// to system separators, and prepend a mock drive letter to absolute paths on
// Windows.
const p: string => string = filePath =>
process.platform === 'win32'
? filePath.replaceAll('/', '\\').replace(/^\\/, 'C:\\')
: filePath;

// Format a posix path as a Watchman-native path on the current platform, i.e.,
// on Windows, drive letters on absolute paths, but posix-style separators.
// This should be used for mocking Watchman *output*.
const wp: string => string = filePath =>
process.platform === 'win32' ? filePath.replace(/^\//, 'C:/') : filePath;

jest.mock('fb-watchman', () => ({
Client: jest.fn().mockImplementation(() => mockClient),
}));

describe('WatchmanWatcher', () => {
test('initializes with watch-project, clock, subscribe', () => {
const watchmanWatcher = new WatchmanWatcher('/project/subdir/js', {
const watchmanWatcher = new WatchmanWatcher(p('/project/subdir/js'), {
dot: true,
ignored: null,
globs: ['**/*.js'],
Expand All @@ -46,16 +60,16 @@ describe('WatchmanWatcher', () => {
.finally(() => (isSettled = true));

expect(mockClient.command).toHaveBeenCalledWith(
['watch-project', '/project/subdir/js'],
['watch-project', p('/project/subdir/js')],
expect.any(Function),
);
cmdCallback<WatchmanWatchResponse>(null, {
watch: '/project',
relative_path: 'subdir/js',
watch: wp('/project'),
relative_path: wp('subdir/js'),
});

expect(mockClient.command).toHaveBeenCalledWith(
['clock', '/project'],
['clock', p('/project')],
expect.any(Function),
);
cmdCallback<WatchmanClockResponse>(null, {
Expand All @@ -65,12 +79,12 @@ describe('WatchmanWatcher', () => {
expect(mockClient.command).toHaveBeenCalledWith(
[
'subscribe',
'/project',
p('/project'),
watchmanWatcher.subscriptionName,
{
defer: ['busy'],
fields: ['name', 'exists', 'new', 'type', 'size', 'mtime_ms'],
relative_root: 'subdir/js',
relative_root: p('subdir/js'),
since: 'c:1629095304.251049',
},
],
Expand All @@ -91,15 +105,20 @@ describe('WatchmanWatcher', () => {
let startPromise: Promise<void>;

beforeEach(async () => {
watchmanWatcher = new WatchmanWatcher('/project/subdir/js', {
watchmanWatcher = new WatchmanWatcher(p('/project/subdir/js'), {
dot: true,
ignored: null,
globs: ['**/*.js'],
watchmanDeferStates: ['busy'],
});
startPromise = watchmanWatcher.startWatching();
cmdCallback<WatchmanWatchResponse>(null, {});
cmdCallback<WatchmanClockResponse>(null, {});
cmdCallback<WatchmanWatchResponse>(null, {
watch: wp('/project'),
relative_path: wp('subdir/js'),
});
cmdCallback<WatchmanClockResponse>(null, {
clock: 'c:123',
});
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe.each(Object.keys(WATCHERS))(

// If all tests are skipped, Jest will not run before/after hooks either.
const maybeTest = WATCHERS[watcherName] ? test : test.skip;
const maybeTestOn = (...platforms: $ReadOnlyArray<string>) =>
platforms.includes(os.platform()) && WATCHERS[watcherName]
? test
: test.skip;

beforeAll(async () => {
watchRoot = await createTempWatchRoot(watcherName);
Expand Down Expand Up @@ -233,7 +237,9 @@ describe.each(Object.keys(WATCHERS))(
});
});

maybeTest(
/* FIXME: Disabled on Windows and Darwin due to flakiness (occasional
timeouts) - see history. */
maybeTestOn('darwin')(
'emits deletion for all files when a directory is deleted',
async () => {
await eventHelpers.allEvents(
Expand Down Expand Up @@ -267,8 +273,6 @@ describe.each(Object.keys(WATCHERS))(
{rejectUnexpected: true},
);
},
// We see occasional failures in CI with default 5s timeout.
45000,
);
},
);
Loading