Skip to content

Commit 658048e

Browse files
committed
fix(prettier-plugin-toml): watch configs with fs.watchFile
`fs.watch` on Windows aborts the process with a libuv assertion (`src/win/fs-event.c:72`) and is unreliable for the atomic saves editors perform. `fs.watchFile` polls the file stat instead, which is portable and also detects newly created config files.
1 parent 92ea975 commit 658048e

3 files changed

Lines changed: 22 additions & 36 deletions

File tree

‎.changeset/tombi-toml-formatter.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The parser/printer is now powered by `@tombi-toml/wasm-lib`, which supports TOML
2424
- Node.js `v18.0.0` or later is required.
2525

2626
Tombi's schema lookup is always disabled, and discovered configuration files are
27-
watched so edits are picked up without restarting the process.
27+
watched so edits and newly created files are picked up without restarting the
28+
process.
2829

2930
See the `prettier-plugin-toml` README for the full list of supported options.

‎packages/toml/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ The rules are resolved in order, each step overriding the previous one:
7474

7575
- Tombi's schema lookup is always disabled, so formatting stays offline and
7676
deterministic.
77-
- Discovered configuration files are cached per directory and watched (best
78-
effort), so edits apply without restarting the process. A configuration
79-
created in a directory that did not exist yet applies on the next run.
77+
- Discovered configuration files are cached per directory and polled (best
78+
effort), so edits and newly created files apply without restarting the
79+
process.
8080

8181
## Parser Options
8282

‎packages/toml/src/discover.ts‎

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, watch, type FSWatcher } from 'node:fs'
1+
import { existsSync, watchFile } from 'node:fs'
22
import { readFile } from 'node:fs/promises'
33
import { homedir } from 'node:os'
44
import path from 'node:path'
@@ -21,12 +21,8 @@ const CONFIG_FILENAMES = [
2121

2222
const PYPROJECT_FILENAME = 'pyproject.toml'
2323

24-
/** Basenames that change which configuration applies when created or changed. */
25-
const CONFIG_BASENAMES = new Set([
26-
'.tombi.toml',
27-
'tombi.toml',
28-
PYPROJECT_FILENAME,
29-
])
24+
/** How often discovered config files are polled (in milliseconds). */
25+
const WATCH_INTERVAL = 500
3026

3127
/** Project level candidates, for a directory and each of its ancestors. */
3228
const getProjectCandidates = (directory: string): string[] => {
@@ -132,7 +128,7 @@ const projectCache = new Map<
132128
>()
133129
let globalConfigCache: Promise<DiscoveredTombiConfig | undefined> | undefined
134130

135-
const configWatchers = new Map<string, FSWatcher>()
131+
const watchedConfigs = new Set<string>()
136132

137133
/**
138134
* Drop the cached project results whose search traverses `directory`: the
@@ -152,37 +148,26 @@ const invalidateGlobalConfig = () => {
152148
}
153149

154150
/**
155-
* Best effort watch of a config file so long-lived consumers (an editor or a
156-
* daemon) pick up changes without restarting. The directory is watched rather
157-
* than the file because editors usually save atomically by renaming a temporary
158-
* file over the target. Watchers stay alive for the process lifetime and are
159-
* unref'd so they never keep it running.
151+
* Best effort watch of a discovered config file so long-lived consumers (an
152+
* editor or a daemon) pick up changes without restarting. `fs.watchFile` polls
153+
* the file stat instead of using `fs.watch`, which is unreliable for the atomic
154+
* saves editors perform and crashes the process on Windows.
160155
*/
161156
const watchConfig = (configPath: string, onConfigChange: () => void) => {
162-
if (configWatchers.has(configPath)) {
157+
if (watchedConfigs.has(configPath)) {
163158
return
164159
}
160+
watchedConfigs.add(configPath)
165161

166-
const directory = path.dirname(configPath)
167-
const basename = path.basename(configPath)
168-
169-
try {
170-
const watcher = watch(directory, (_event, filename) => {
171-
const changed = filename == null ? undefined : path.basename(filename)
172-
if (
173-
changed == null ||
174-
changed === basename ||
175-
CONFIG_BASENAMES.has(changed)
176-
) {
162+
watchFile(
163+
configPath,
164+
{ interval: WATCH_INTERVAL, persistent: false },
165+
(curr, prev) => {
166+
if (curr.mtimeMs !== prev.mtimeMs || curr.size !== prev.size) {
177167
onConfigChange()
178168
}
179-
})
180-
watcher.on('error', onConfigChange)
181-
watcher.unref()
182-
configWatchers.set(configPath, watcher)
183-
} catch {
184-
// Watching is best effort, ignore runtimes that do not support it.
185-
}
169+
},
170+
)
186171
}
187172

188173
const getProjectConfig = (directory: string) => {

0 commit comments

Comments
 (0)