Skip to content

Commit 1953b77

Browse files
yangching2claude
andcommitted
fix(server): keep livereload alive on permission errors
A permission error (EACCES/EPERM) on a single sub-directory is non-fatal — chokidar keeps watching everything else — but the watcher's error handler treated it as fatal: it tore down the whole watcher, disabled livereload, and printed the raw error with a stack trace. Detect permission errors, log a single friendly line, and leave the watcher running. Genuinely fatal errors (e.g. EMFILE) keep the existing teardown and guidance. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent bc1d572 commit 1953b77

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎src/server/watcher.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ const attachTreeReloadHandlers = (watcher: FSWatcher, wss: WebSocketServer): voi
6767
});
6868
};
6969

70+
// A permission error on a single path is non-fatal: chokidar keeps watching
71+
// everything else, so we just skip that path instead of tearing down the watcher.
72+
const isPermissionError = (error: unknown): boolean => {
73+
return Boolean(
74+
error &&
75+
typeof error === 'object' &&
76+
'code' in error &&
77+
['EACCES', 'EPERM'].includes((error as NodeJS.ErrnoException).code as string),
78+
);
79+
};
80+
81+
const describeWatchTarget = (error: unknown, directory: string): string => {
82+
const errorPath = (error as NodeJS.ErrnoException)?.path;
83+
if (typeof errorPath !== 'string') return 'a path';
84+
return errorPath.startsWith(`${directory}/`)
85+
? errorPath.slice(directory.length + 1)
86+
: errorPath;
87+
};
88+
7089
const setupDirectoryWatcher = (directory: string, wss: WebSocketServer): FSWatcher | null => {
7190
try {
7291
const watcher = chokidar.watch(directory, {
@@ -89,6 +108,15 @@ const setupDirectoryWatcher = (directory: string, wss: WebSocketServer): FSWatch
89108
attachTreeReloadHandlers(watcher, wss);
90109

91110
watcher.on('error', (error) => {
111+
if (isPermissionError(error)) {
112+
const target = describeWatchTarget(error, directory);
113+
logger.log(
114+
'Livereload',
115+
`🔒 Can't watch "${target}" (permission denied); live-reload skipped for it, others still watched.`,
116+
);
117+
return;
118+
}
119+
92120
watcher.unwatch(directory);
93121
watcher.close()
94122
.then(() => logger.log('Livereload', 'Directory watcher closed'));
@@ -119,6 +147,14 @@ const setupFilePatternWatcher = (context: ServerContext, wss: WebSocketServer):
119147
attachTreeReloadHandlers(watcher, wss);
120148

121149
watcher.on('error', (error) => {
150+
if (isPermissionError(error)) {
151+
const target = describeWatchTarget(error, directory);
152+
logger.log(
153+
'Livereload',
154+
`🔒 Can't watch "${target}" (permission denied); live-reload skipped for it, others still watched.`,
155+
);
156+
return;
157+
}
122158
watcher.close()
123159
.then(() => logger.log('Livereload', 'File pattern watcher closed'));
124160
logger.error('🚫 Error watching files:', error);

‎test/unit/server/watcher.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,26 @@ describe('watcher.ts unit tests', () => {
269269
'This error is likely caused by too many open files. Try increasing the ulimit.',
270270
);
271271
});
272+
273+
it('should log a friendly message and keep watching on permission error (EACCES)', async () => {
274+
const onErrorCallback = (mockDirectoryWatcher.on as jest.Mock).mock.calls.find(call => call[0] === 'error')[1];
275+
const error = Object.assign(new Error('permission denied'), {
276+
code: 'EACCES',
277+
path: '/mock/directory/secret',
278+
});
279+
280+
onErrorCallback(error);
281+
await Promise.resolve();
282+
283+
// Permission errors are non-fatal: do not tear down the watcher.
284+
expect(mockDirectoryWatcher.unwatch).not.toHaveBeenCalled();
285+
expect(mockDirectoryWatcher.close).not.toHaveBeenCalled();
286+
expect(logger.log).toHaveBeenCalledWith(
287+
'Livereload',
288+
'🔒 Can\'t watch "secret" (permission denied); live-reload skipped for it, others still watched.',
289+
);
290+
expect(logger.error).not.toHaveBeenCalled();
291+
});
272292
});
273293

274294
describe('chokidar ignored option', () => {
@@ -402,5 +422,30 @@ describe('watcher.ts unit tests', () => {
402422
expect(logger.error).toHaveBeenCalledWith('🚫 Error watching files:', expect.any(Error));
403423
expect(logger.error).toHaveBeenCalledWith('Livereload will be disabled');
404424
});
425+
426+
it('should log a friendly message and keep watching on permission error (EACCES)', async () => {
427+
setupWatcher(
428+
{ directory: '/mock/directory', filePatterns: ['a.md'] },
429+
mockServer,
430+
3000,
431+
);
432+
433+
const onErrorCallback = (mockPatternWatcher.on as jest.Mock).mock.calls
434+
.find(call => call[0] === 'error')[1];
435+
const error = Object.assign(new Error('permission denied'), {
436+
code: 'EACCES',
437+
path: '/mock/directory/a.md',
438+
});
439+
440+
onErrorCallback(error);
441+
await Promise.resolve();
442+
443+
expect(mockPatternWatcher.close).not.toHaveBeenCalled();
444+
expect(logger.log).toHaveBeenCalledWith(
445+
'Livereload',
446+
'🔒 Can\'t watch "a.md" (permission denied); live-reload skipped for it, others still watched.',
447+
);
448+
expect(logger.error).not.toHaveBeenCalled();
449+
});
405450
});
406451
});

0 commit comments

Comments
 (0)