Skip to content

Conversation

@mogzol
Copy link

@mogzol mogzol commented Apr 2, 2025

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
Copy link
Author

mogzol commented May 15, 2025

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
Copy link
Author

mogzol commented May 20, 2025

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 /**)

Fixes privatenumber#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.
@rhlin
Copy link

rhlin commented Aug 19, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for watching hidden files

2 participants