fix(fs:watch) fix missing char in filename (#11693)

Co-authored-by: cirospaciari <cirospaciari@users.noreply.github.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
This commit is contained in:
Ciro Spaciari
2024-06-08 02:46:35 -03:00
committed by GitHub
parent d92ebf2a99
commit 8ac8e4dc5f
5 changed files with 52 additions and 23 deletions

View File

@@ -374,23 +374,19 @@ pub const FSEventsLoop = struct {
path = path[handle_path.len..];
// Ignore events with path equal to directory itself
if (path.len <= 1 and is_file) {
if (path.len <= 1 and !is_file) {
continue;
}
if (path.len == 0) {
// Since we're using fsevents to watch the file itself, path == handle_path, and we now need to get the basename of the file back
while (path.len > 0) {
if (bun.strings.startsWithChar(path, '/')) {
path = path[1..];
break;
} else {
path = path[1..];
}
}
if (path.len == 0) {
// Since we're using fsevents to watch the file itself handle_path == path, and we now need to get the basename of the file back
const basename = bun.strings.lastIndexOfChar(handle_path, '/') orelse handle_path.len;
path = handle_path[basename..];
// Created and Removed seem to be always set, but don't make sense
flags &= ~kFSEventsRenamed;
} else {
}
if (bun.strings.startsWithChar(path, '/')) {
// Skip forward slash
path = path[1..];
}

View File

@@ -239,16 +239,8 @@ pub const PathWatcherManager = struct {
if (path.len <= 1) {
continue;
}
if (path.len == 0) {
while (path.len > 0) {
if (bun.strings.startsWithChar(path, '/')) {
path = path[1..];
break;
} else {
path = path[1..];
}
}
} else {
if (bun.strings.startsWithChar(path, '/')) {
// Skip forward slash
path = path[1..];
}

View File

@@ -3,7 +3,7 @@ try {
const watcher = fs.watch("relative.txt", { signal: AbortSignal.timeout(2000) });
watcher.on("change", function (event, filename) {
if (filename !== "relative.txt" && event !== "change") {
if (filename !== "relative.txt" || event !== "change") {
console.error("fail");
clearInterval(interval);
watcher.close();

View File

@@ -0,0 +1,28 @@
import fs from "fs";
try {
const watcher = fs.watch("./myrelativedir/", { signal: AbortSignal.timeout(2000) });
watcher.on("change", function (event, filename) {
if (filename !== "relative.txt") {
console.error("fail", filename, event);
clearInterval(interval);
watcher.close();
process.exit(1);
} else {
clearInterval(interval);
watcher.close();
}
});
watcher.on("error", err => {
clearInterval(interval);
console.error(err.message);
process.exit(1);
});
const interval = setInterval(() => {
fs.writeFileSync("./myrelativedir/relative.txt", "world");
}, 10);
} catch (err) {
console.error(err.message);
process.exit(1);
}

View File

@@ -64,6 +64,19 @@ describe("fs.watch", () => {
}
});
test("should work with relative dirs", done => {
try {
const myrelativedir = path.join(testDir, "myrelativedir");
try {
fs.mkdirSync(myrelativedir);
} catch {}
fs.writeFileSync(path.join(myrelativedir, "relative.txt"), "hello");
bunRunAsScript(testDir, path.join(import.meta.dir, "fixtures", "relative_dir.js"));
done();
} catch (e: any) {
done(e);
}
});
test("add file/folder to folder", done => {
let count = 0;
const root = path.join(testDir, "add-directory");