Skip to content

Commit 37ff9c3

Browse files
authored
Merge pull request microsoft#792 from Microsoft/watchOptions
Add documentation for environment variables to configure watching of files and directories
2 parents cec2e03 + bf0e3c2 commit 37ff9c3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pages/Compiler Options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Option | Type | Default
8585
`--types` | `string[]`| | List of names of type definitions to include. See [@types, --typeRoots and --types](./tsconfig.json.md#types-typeroots-and-types) for more details.
8686
`--typeRoots` | `string[]`| | List of folders to include type definitions from. See [@types, --typeRoots and --types](./tsconfig.json.md#types-typeroots-and-types) for more details.
8787
`--version`<br/>`-v` | | | Print the compiler's version.
88-
`--watch`<br/>`-w` | | | Run the compiler in watch mode. Watch input files and trigger recompilation on changes.
88+
`--watch`<br/>`-w` | | | Run the compiler in watch mode. Watch input files and trigger recompilation on changes. The implementation of watching files and directories can be configured using environment variable. See [configuring watch](./Configuring%20Watch.md) for more details.
8989

9090
* <sup>[1]</sup> These options are experimental.
9191
* <sup>[2]</sup> These options are only allowed in `tsconfig.json`, and not through command-line switches.

pages/Configuring Watch.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Compiler supports configuring how to watch files and directories using the environment variables.
2+
3+
## Configuring file watching using environment variable `TSC_WATCHFILE`
4+
5+
Option | Description
6+
-----------------------------------------------|----------------------------------------------------------------------
7+
`PriorityPollingInterval` | Use `fs.watchFile` but use different polling intervals for source files, config files and missing files
8+
`DynamicPriorityPolling` | Use a dynamic queue where in the frequently modified files will be polled at shorter interval and the files unchanged will be polled less frequently
9+
`UseFsEvents` | Use `fs.watch` which uses file system events (but might not be accurate on different OS) to get the notifications for the file changes/creation/deletion. Note that few OS eg. linux has limit on number of watches and failing to create watcher using `fs.watch` will result it in creating using `fs.watchFile`
10+
`UseFsEventsWithFallbackDynamicPolling` | This option is similar to `UseFsEvents` except on failing to create watch using `fs.watch`, the fallback watching happens through dynamic polling queues (as explained in `DynamicPriorityPolling`)
11+
`UseFsEventsOnParentDirectory` | This option watches parent directory of the file with `fs.watch` (using file system events) thus being low on CPU but can compromise accuracy.
12+
default (no value specified) | If environment variable `TSC_NONPOLLING_WATCHER` is set to true, watches parent directory of files (just like `UseFsEventsOnParentDirectory`). Otherwise watch files using `fs.watchFile` with `250ms` as the timeout for any file
13+
14+
## Configuring directory watching using environment variable `TSC_WATCHDIRECTORY`
15+
16+
The watching of directory on platforms that dont support recursive directory watching natively in node, is supported through recursively creating directory watcher for the child directories using different options selected by `TSC_WATCHDIRECTORY`. Note that on platforms that support native recursive directory watching (e.g windows) the value of this environment variable is ignored.
17+
18+
Option | Description
19+
-----------------------------------------------|----------------------------------------------------------------------
20+
`RecursiveDirectoryUsingFsWatchFile` | Use `fs.watchFile` to watch the directories and child directories which is a polling watch (consuming CPU cycles)
21+
`RecursiveDirectoryUsingDynamicPriorityPolling`| Use dynamic polling queue to poll changes to the directory and child directories.
22+
default (no value specified) | Use `fs.watch` to watch directories and child directories
23+
24+
## Background
25+
26+
`--watch` implementation of the compiler relies on `fs.watch` and `fs.watchFile` provided by node, both of these methods have pros and cons.
27+
28+
`fs.watch` uses file system events to notify the changes in the file/directory. But this is OS dependent and the notification is not completely reliable and does not work as expected on many OS. Also there could be limit on number of watches that can be created, eg. linux and we could exhaust it pretty quickly with programs that include large number of files. But because this uses file system events, there is not much CPU cycle involved. Compiler typically uses `fs.watch` to watch directories (eg. source directories included by config file, directories in which module resolution failed etc) These can handle the missing precision in notifying about the changes. But recursive watching is supported on only Windows and OSX. That means we need something to replace the recursive nature on other OS.
29+
30+
`fs.watchFile` uses polling and thus involves CPU cycles. But this is the most reliable mechanism to get the update on the status of file/directory. Compiler typically uses `fs.watchFile` to watch source files, config files and missing files (missing file references) that means the CPU usage depends on number of files in the program.

0 commit comments

Comments
 (0)