Skip to content

Commit 01e89eb

Browse files
mizdraclaude
andauthored
fix(codegen): ignore node_modules and .git directories in watch mode (#335)
Prevent EMFILE (too many open files) errors by ignoring node_modules and .git directories in chokidar's watch, matching TypeScript's default ignored directories behavior. Closes #321 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9ca81da commit 01e89eb

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

.changeset/fix-emfile-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/codegen': patch
3+
---
4+
5+
fix(codegen): ignore `node_modules` and `.git` directories in watch mode to prevent EMFILE errors

packages/codegen/src/runner.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import type { Stats } from 'node:fs';
22
import { rm } from 'node:fs/promises';
3+
import { basename } from '@css-modules-kit/core';
34
import chokidar, { type FSWatcher } from 'chokidar';
45
import { CMKDisabledError } from './error.js';
56
import type { Logger } from './logger/logger.js';
67
import { createProject, type Project } from './project.js';
78

9+
// The CSS Modules Kit also ignores the minimum set of directories that TypeScript typically ignores.
10+
// ref: https://github.com/microsoft/TypeScript/blob/87aa917befa8f6182f5535df3c77287209692a04/src/compiler/sys.ts#L562
11+
// ref: https://github.com/microsoft/TypeScript/blob/caf1aee269d1660b4d2a8b555c2d602c97cb28d7/src/compiler/utilities.ts#L9506
12+
// ref: https://github.com/mizdra/css-modules-kit/issues/321
13+
const ignoredDirname: readonly string[] = ['node_modules', '.git'];
14+
815
interface RunnerArgs {
916
project: string;
1017
clean: boolean;
@@ -78,6 +85,8 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
7885
chokidar
7986
.watch(wildcardDirectory.fileName, {
8087
ignored: (fileName: string, stats?: Stats) => {
88+
if (ignoredDirname.includes(basename(fileName))) return true;
89+
8190
// The ignored function is called twice for the same path. The first time with stats undefined,
8291
// and the second time with stats provided.
8392
// In the first call, we can't determine if the path is a directory or file.

0 commit comments

Comments
 (0)