Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Reports precise or coarse host filesystem changes through platform
* watchers. Each handle owns and disposes its watcher. Bound at App scope.
* On Windows, drive-root watch paths such as `D:` / `/D:` are normalized to
* `D:\` before `fs.watch`, avoiding ENOENT when the process CWD is a drive root.
*/

import { watch as fsWatch } from 'node:fs';
Expand Down Expand Up @@ -237,13 +239,23 @@ export class HostFsWatchService implements IHostFsWatchService {
constructor(private readonly runtime: HostFsWatchRuntime = NODE_HOST_FS_WATCH_RUNTIME) {}

watch(path: string, options?: HostFsWatchOptions): IHostFsWatchHandle {
const root = normalizeWatchRoot(path, this.runtime.platform);
if (useNativeRecursive(options, this.runtime.platform)) {
return new SignalWatchHandle(path, options, this.runtime);
return new SignalWatchHandle(root, options, this.runtime);
}
return new HostFsWatchHandle(path, options);
return new HostFsWatchHandle(root, options);
}
}

function normalizeWatchRoot(path: string, platform: NodeJS.Platform): string {
if (platform !== 'win32') return path;
const driveOnly = path.match(/^\/?([A-Za-z]):\/?$/);
if (driveOnly) {
return `${driveOnly[1].toUpperCase()}:\\`;
}
return path;
}

function useNativeRecursive(
options: HostFsWatchOptions | undefined,
platform: NodeJS.Platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ describe('host filesystem change notifications', () => {
expect(events).toHaveLength(0);
});


it('normalizes Windows drive-root watch paths before calling watchNative', () => {
const watched: string[] = [];
const runtime: HostFsWatchRuntime = {
platform: 'win32',
watchNative: (root, _listener) => {
watched.push(root);
return new TestNativeWatcher();
},
scheduleRetry: () => ({ dispose: () => undefined }),
};
const service = new HostFsWatchService(runtime);
handle = service.watch('/D:', { signal: true });
expect(watched).toEqual(['D:\\']);
handle.dispose();
handle = undefined;

handle = service.watch('E:', { signal: true });
expect(watched).toEqual(['D:\\', 'E:\\']);
});
it.skipIf(process.platform !== 'darwin')(
'signal mode keeps the fd footprint bounded on a fat subtree',
async () => {
Expand Down