tsx icon indicating copy to clipboard operation
tsx copied to clipboard

feat(watch): prefer includes over excludes

Open mogzol opened this issue 9 months ago • 3 comments

Fixes #210

This change makes any explicitly included files in watch mode never get excluded, even if they otherwise would be. Particularly, this is useful for watching files that are excluded by the default excludes, like ".env" files, which were previously impossible to watch.

In making these changes, chokidar has also been upgraded to v4, which no longer supports glob patterns. Glob functionality is now achieved with the 'glob' library. The windowsPathsNoEscape glob option is enabled in order to keep the behavior the same as it was with chokidar v3, where backslash cannot be used as a glob escape character, and only as a directory separator.

Example

Consider the command:

tsx watch --exclude="./lib" --include="./lib/special.ts" index.ts

Previously, changes to ./lib/special.ts would not trigger a restart, despite the fact that it was explicitly watched, because it's containing folder was excluded. With these changes, a restart will be triggered, but changes to any other files in ./lib will still be ignored.

This is also true for included globs. For example, this command:

tsx watch --exclude="./lib" --include="./lib/dir/**" index.ts

Will restart if any files under ./lib/dir are modified, but will not restart for any other changes in the lib directory.

mogzol avatar Apr 02 '25 07:04 mogzol

I rebased this onto the latest commit to resolve the conflicts in pnpm-lock.yaml

EDIT: Also, the tests seem to hang when running on Node 20.0.0, but that is also happening without these changes, so it isn't caused by this. Testing locally, Node 20.5.0 is the earliest 20.x version which successfully runs the tests.

mogzol avatar May 15 '25 00:05 mogzol

Updated this to fix a minor issue where the windowsPathsNoEscape option wasn't passed to hasMagic(). Also removed the redundant **/.* default ignore pattern, since it is already covered by the **/.*/** pattern (a glob ending in /** also matches the path without the /**)

mogzol avatar May 20 '25 17:05 mogzol

I tried adding ".env" to the watch include list, but it’s still ignored by the built‑in “Ignore Hidden Files” rule.

I recommend the following precedence: built-in rules < include files < exclude files

By the way, tsconfig.json has "include" and "exclude", and "exclude" takes precedence over "include".

It bothers me that the built‑in rules prevent ".env" files from being watched. Any better solution without patching tsx?

rhlin avatar Aug 19 '25 08:08 rhlin