Skip to content

Commit 8c8e03e

Browse files
fix(filesystem): normalize watcher paths to forward slashes on Windows
@parcel/watcher emits paths with OS-native separators, so on Windows the callback receives backslash paths (e.g. `C:\foo\bar.txt`). The watcher published these verbatim, but the rest of the codebase compares and stores paths with forward slashes — causing path mismatches and inconsistent behavior (file operations, @file mentions, cache keys) on Windows. Normalize `update.path` to forward slashes before publishing (mirroring the `storagePath` normalization in database/path.ts). The option is a no-op on non-Windows platforms. Test changes: - Add a regression test asserting emitted paths contain no backslashes. - Wrap expected paths in the existing live tests with a `toPosix` helper so they compare correctly now that emitted paths are forward slashes (previously both sides used backslashes on Windows, so the comparison passed by coincidence without actually exercising normalization). Closes anomalyco#35329
1 parent e357c90 commit 8c8e03e

2 files changed

Lines changed: 47 additions & 16 deletions

File tree

‎packages/core/src/filesystem/watcher.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ function getBackend() {
4141
if (process.platform === "linux") return "inotify"
4242
}
4343

44+
// @parcel/watcher emits OS-native separators, so on Windows `update.path`
45+
// arrives with backslashes (e.g. `C:\foo\bar.txt`). The rest of the codebase
46+
// compares and stores paths with forward slashes, so unnormalized backslash
47+
// paths cause mismatches and inconsistent behavior. Mirror the `storagePath`
48+
// normalization used in database/path.ts. (#35329)
49+
function normalizeWatchPath(input: string): string {
50+
if (process.platform !== "win32") return input
51+
return input.replaceAll("\\", "/")
52+
}
53+
4454
function protecteds(dir: string) {
4555
return Protected.paths().filter((item) => {
4656
const relative = path.relative(dir, item)
@@ -85,9 +95,10 @@ const layer = Layer.effect(
8595

8696
const callback: ParcelWatcher.SubscribeCallback = (_error, updates) => {
8797
for (const update of updates) {
88-
if (update.type === "create") runFork(events.publish(Event.Updated, { file: update.path, event: "add" }))
89-
if (update.type === "update") runFork(events.publish(Event.Updated, { file: update.path, event: "change" }))
90-
if (update.type === "delete") runFork(events.publish(Event.Updated, { file: update.path, event: "unlink" }))
98+
const file = normalizeWatchPath(update.path)
99+
if (update.type === "create") runFork(events.publish(Event.Updated, { file, event: "add" }))
100+
if (update.type === "update") runFork(events.publish(Event.Updated, { file, event: "change" }))
101+
if (update.type === "delete") runFork(events.publish(Event.Updated, { file, event: "unlink" }))
91102
}
92103
}
93104

‎packages/core/test/filesystem/watcher.test.ts‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import { testEffect } from "../lib/effect"
1717

1818
const describeWatcher = Watcher.hasNativeBinding() && !process.env.CI ? describe : describe.skip
1919

20+
// The watcher normalizes emitted paths to forward slashes (see normalizeWatchPath
21+
// in filesystem/watcher.ts). On Windows, path.join produces backslashes, so
22+
// expected paths used in event comparisons must be normalized the same way.
23+
function toPosix(p: string): string {
24+
return process.platform === "win32" ? p.replaceAll("\\", "/") : p
25+
}
26+
2027
type WatcherEvent = { file: string; event: "add" | "change" | "unlink" }
2128

2229
const it = testEffect(AppNodeBuilder.build(LayerNode.group([FSUtil.node, EventV2.node])))
@@ -133,7 +140,7 @@ function noUpdate<E>(check: (event: WatcherEvent) => boolean, trigger: Effect.Ef
133140
}
134141

135142
function ready(directory: string) {
136-
const file = path.join(directory, `.watcher-${Math.random().toString(36).slice(2)}`)
143+
const file = toPosix(path.join(directory, `.watcher-${Math.random().toString(36).slice(2)}`))
137144
return Effect.gen(function* () {
138145
const fs = yield* FSUtil.Service
139146
yield* eventuallyUpdate(
@@ -149,7 +156,7 @@ describeWatcher("Watcher", () => {
149156
(directory) =>
150157
Effect.gen(function* () {
151158
const fs = yield* FSUtil.Service
152-
const file = path.join(directory, "watch.txt")
159+
const file = toPosix(path.join(directory, "watch.txt"))
153160
yield* ready(directory)
154161
for (const item of [
155162
{ event: "add" as const, trigger: fs.writeFileString(file, "a") },
@@ -172,7 +179,7 @@ describeWatcher("Watcher", () => {
172179
withTmp((directory) =>
173180
Effect.gen(function* () {
174181
const fs = yield* FSUtil.Service
175-
const file = path.join(directory, "plain.txt")
182+
const file = toPosix(path.join(directory, "plain.txt"))
176183
yield* noUpdate((event) => event.file === file, fs.writeFileString(file, "plain"))
177184
}),
178185
),
@@ -186,11 +193,8 @@ describeWatcher("Watcher", () => {
186193
Effect.promise(() => tmpdir()),
187194
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
188195
)
189-
yield* ready(tmp.path).pipe(
190-
provide(tmp.path, { type: "git", store: AbsolutePath.make(path.join(tmp.path, ".git")) }),
191-
Effect.scoped,
192-
)
193-
const file = path.join(tmp.path, "after-dispose.txt")
196+
yield* ready(tmp.path).pipe(provide(tmp.path), Effect.scoped)
197+
const file = toPosix(path.join(tmp.path, "after-dispose.txt"))
194198
yield* noUpdate((event) => event.file === file, fs.writeFileString(file, "gone")).pipe(
195199
Effect.provideService(EventV2.Service, events),
196200
)
@@ -202,7 +206,7 @@ describeWatcher("Watcher", () => {
202206
(directory) =>
203207
Effect.gen(function* () {
204208
const fs = yield* FSUtil.Service
205-
const index = path.join(directory, ".git", "index")
209+
const index = toPosix(path.join(directory, ".git", "index"))
206210
yield* ready(directory)
207211
yield* noUpdate(
208212
(event) => event.file === index,
@@ -220,7 +224,7 @@ describeWatcher("Watcher", () => {
220224
(directory) =>
221225
Effect.gen(function* () {
222226
const fs = yield* FSUtil.Service
223-
const head = path.join(directory, ".git", "HEAD")
227+
const head = toPosix(path.join(directory, ".git", "HEAD"))
224228
const branch = `watch-${Math.random().toString(36).slice(2)}`
225229
yield* ready(directory)
226230
yield* Effect.promise(() => $`git branch ${branch}`.cwd(directory).quiet())
@@ -242,15 +246,15 @@ describeWatcher("Watcher", () => {
242246
const actual = path.join(directory, "..", `actual_${path.basename(directory)}`)
243247
yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(actual, { recursive: true, force: true })))
244248
yield* ready(directory)
245-
const head = path.join(directory, ".git", "HEAD")
249+
const head = toPosix(path.join(directory, ".git", "HEAD"))
246250
const branch = `watch-${Math.random().toString(36).slice(2)}`
247251
yield* Effect.promise(() => $`git branch ${branch}`.cwd(directory).quiet())
248252
expect(
249253
yield* nextUpdate(
250-
(event) => event.file === path.join(actual, "HEAD"),
254+
(event) => event.file === toPosix(path.join(actual, "HEAD")),
251255
afs.writeFileString(head, `ref: refs/heads/${branch}\n`),
252256
),
253-
).toEqual({ file: path.join(actual, "HEAD"), event: "change" })
257+
).toEqual({ file: toPosix(path.join(actual, "HEAD")), event: "change" })
254258
}),
255259
{
256260
git: true,
@@ -263,4 +267,20 @@ describeWatcher("Watcher", () => {
263267
),
264268
)
265269
})
270+
271+
// Regression for #35329: @parcel/watcher emits OS-native separators, so on
272+
// Windows the raw path has backslashes. The watcher must normalize them to
273+
// forward slashes before publishing, or path comparisons elsewhere break.
274+
it.live("publishes forward-slash paths (no backslashes) on Windows", () =>
275+
withTmp((directory) =>
276+
Effect.gen(function* () {
277+
const fs = yield* FSUtil.Service
278+
const file = toPosix(path.join(directory, "sep.txt"))
279+
yield* ready(directory)
280+
const event = yield* nextUpdate((e) => e.event === "add", fs.writeFileString(file, "x"))
281+
expect(event.file).toBe(file)
282+
expect(event.file).not.toContain("\\")
283+
}),
284+
),
285+
)
266286
})

0 commit comments

Comments
 (0)