diff options
author | 2023-09-16 05:21:15 +0100 | |
---|---|---|
committer | 2023-09-15 21:21:15 -0700 | |
commit | 43c463f07803f40f4733dbc208609d1f102c26d0 (patch) | |
tree | c3f9e89604fbd4a63e22b60ebf32dd6fb1c5c49c | |
parent | 787281ee5e752267f25922b8886700eeadd48c3a (diff) | |
download | bun-43c463f07803f40f4733dbc208609d1f102c26d0.tar.gz bun-43c463f07803f40f4733dbc208609d1f102c26d0.tar.zst bun-43c463f07803f40f4733dbc208609d1f102c26d0.zip |
fix(node/fs.watch): Check first char before trimming event filenames (#5505)
* Add failing test
* fix(node/fs.watch): Don't lose first char in event
* run prettier
-rw-r--r-- | src/bun.js/node/path_watcher.zig | 13 | ||||
-rw-r--r-- | test/js/node/watch/fs.watch.test.ts | 38 |
2 files changed, 36 insertions, 15 deletions
diff --git a/src/bun.js/node/path_watcher.zig b/src/bun.js/node/path_watcher.zig index 4f44a68ff..e00451a38 100644 --- a/src/bun.js/node/path_watcher.zig +++ b/src/bun.js/node/path_watcher.zig @@ -286,17 +286,8 @@ pub const PathWatcherManager = struct { if (!(path.len == 1 and entry_point[0] == '/')) { path = path[entry_point.len..]; - if (path.len == 0) { - while (path.len > 0) { - if (bun.strings.startsWithChar(path, '/')) { - path = path[1..]; - break; - } else { - path = path[1..]; - } - } - } else { - // Skip forward slash + // Skip leading slash + if (bun.strings.startsWithChar(path, '/')) { path = path[1..]; } } diff --git a/test/js/node/watch/fs.watch.test.ts b/test/js/node/watch/fs.watch.test.ts index 5086ae1d8..10fc754d2 100644 --- a/test/js/node/watch/fs.watch.test.ts +++ b/test/js/node/watch/fs.watch.test.ts @@ -67,7 +67,7 @@ describe("fs.watch", () => { const root = path.join(testDir, "add-directory"); try { fs.mkdirSync(root); - } catch {} + } catch { } let err: Error | undefined = undefined; const watcher = fs.watch(root, { signal: AbortSignal.timeout(3000) }); watcher.on("change", (event, filename) => { @@ -102,7 +102,7 @@ describe("fs.watch", () => { const root = path.join(testDir, "add-subdirectory"); try { fs.mkdirSync(root); - } catch {} + } catch { } const subfolder = path.join(root, "subfolder"); fs.mkdirSync(subfolder); const watcher = fs.watch(root, { recursive: true, signal: AbortSignal.timeout(3000) }); @@ -165,6 +165,36 @@ describe("fs.watch", () => { }); }); + // https://github.com/oven-sh/bun/issues/5442 + test("should work with paths with trailing slashes", done => { + const testsubdir = tempDirWithFiles("subdir", { + "trailing.txt": "hello", + }); + const filepath = path.join(testsubdir, "trailing.txt"); + let err: Error | undefined = undefined; + const watcher = fs.watch(testsubdir + "/", function (event, filename) { + try { + expect(event).toBe("rename"); + expect(filename).toBe("trailing.txt"); + } catch (e: any) { + err = e; + } finally { + clearInterval(interval); + watcher.close(); + } + }); + + watcher.once("close", () => { + done(err); + }); + + const interval = repeat(() => { + fs.rmSync(filepath, { force: true }); + const fd = fs.openSync(filepath, "w"); + fs.closeSync(fd); + }); + }); + test("should emit 'change' event when file is modified", done => { const filepath = path.join(testDir, "watch.txt"); @@ -408,7 +438,7 @@ describe("fs.promises.watch", () => { const root = path.join(testDir, "add-promise-directory"); try { fs.mkdirSync(root); - } catch {} + } catch { } let success = false; let err: Error | undefined = undefined; try { @@ -450,7 +480,7 @@ describe("fs.promises.watch", () => { const root = path.join(testDir, "add-promise-subdirectory"); try { fs.mkdirSync(root); - } catch {} + } catch { } const subfolder = path.join(root, "subfolder"); fs.mkdirSync(subfolder); let success = false; |