Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime/ops): Fix watchfs remove event #27041

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions runtime/ops/fs_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ fn starts_with_canonicalized(path: &Path, prefix: &str) -> bool {
}
}

fn is_file_removed(event_path: &PathBuf) -> bool {
let exists_path = std::fs::exists(event_path);
match exists_path {
Ok(res) => !res,
Err(_) => false,
}
}

#[derive(Debug, thiserror::Error)]
pub enum FsEventsError {
#[error(transparent)]
Expand Down Expand Up @@ -150,6 +158,13 @@ fn start_watcher(
})
}) {
let _ = sender.try_send(Ok(event.clone()));
} else if event.paths.iter().any(is_file_removed) {
let remove_event = FsEvent {
kind: "remove",
paths: event.paths.clone(),
flag: None,
};
let _ = sender.try_send(Ok(remove_event));
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/fs_events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ async function makeTempDir(): Promise<string> {
return testDir;
}

async function makeTempFile(): Promise<string> {
const testFile = await Deno.makeTempFile();
// The watcher sometimes witnesses the creation of it's own root
// directory. Delay a bit.
await delay(100);
return testFile;
}

Deno.test(
{ permissions: { read: true, write: true } },
async function watchFsBasic() {
Expand Down Expand Up @@ -155,3 +163,25 @@ Deno.test(
assert(done);
},
);

Deno.test(
{ permissions: { read: true, write: true } },
async function watchFsRemove() {
const testFile = await makeTempFile();
using watcher = Deno.watchFs(testFile);
async function waitForRemove() {
for await (const event of watcher) {
if (event.kind === "remove") {
return event;
}
}
}
const eventPromise = waitForRemove();

await Deno.remove(testFile);

// Expect zero events.
const event = await eventPromise;
assertEquals(event!.kind, "remove");
},
);