aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/node/path_watcher.zig13
-rw-r--r--test/js/node/watch/fs.watch.test.ts38
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;