summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Hagemeister <marvin@deno.com>2024-08-07 18:15:57 +0200
committerGitHub <noreply@github.com>2024-08-07 18:15:57 +0200
commit9d6da1036d80a29862f6bdfb51e6f51eee235c35 (patch)
tree4797a9b4e31337ae171f6c6de83c6db2d15da976
parent59c9bd08000b72e01b1a4506edc9954183e886a0 (diff)
fix: `rename` watch event missing (#24893)
This PR ensures that we forward a `rename` event in our file watcher. The rust lib we use combines that with the `modify` event. This fixes a compatibility issue with Node too, which sends the `rename` event as well. Fixes https://github.com/denoland/deno/issues/24880
-rw-r--r--cli/tsc/dts/lib.deno.ns.d.ts9
-rw-r--r--ext/node/polyfills/_fs/_fs_watch.ts2
-rw-r--r--runtime/ops/fs_events.rs9
-rw-r--r--tests/unit/fs_events_test.ts20
4 files changed, 38 insertions, 2 deletions
diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts
index 084dc1b76..0cc96d542 100644
--- a/cli/tsc/dts/lib.deno.ns.d.ts
+++ b/cli/tsc/dts/lib.deno.ns.d.ts
@@ -4149,7 +4149,14 @@ declare namespace Deno {
* @category File System */
export interface FsEvent {
/** The kind/type of the file system event. */
- kind: "any" | "access" | "create" | "modify" | "remove" | "other";
+ kind:
+ | "any"
+ | "access"
+ | "create"
+ | "modify"
+ | "rename"
+ | "remove"
+ | "other";
/** An array of paths that are associated with the file system event. */
paths: string[];
/** Any additional flags associated with the event. */
diff --git a/ext/node/polyfills/_fs/_fs_watch.ts b/ext/node/polyfills/_fs/_fs_watch.ts
index acdaff800..eb2cd8bfa 100644
--- a/ext/node/polyfills/_fs/_fs_watch.ts
+++ b/ext/node/polyfills/_fs/_fs_watch.ts
@@ -368,6 +368,8 @@ function convertDenoFsEventToNodeFsEvent(
): NodeFsEventType {
if (kind === "create" || kind === "remove") {
return "rename";
+ } else if (kind === "rename") {
+ return "rename";
} else {
return "change";
}
diff --git a/runtime/ops/fs_events.rs b/runtime/ops/fs_events.rs
index 367866162..58fe9d5fd 100644
--- a/runtime/ops/fs_events.rs
+++ b/runtime/ops/fs_events.rs
@@ -14,6 +14,7 @@ use deno_core::op2;
use deno_permissions::PermissionsContainer;
use notify::event::Event as NotifyEvent;
+use notify::event::ModifyKind;
use notify::Error as NotifyError;
use notify::EventKind;
use notify::RecommendedWatcher;
@@ -71,7 +72,13 @@ impl From<NotifyEvent> for FsEvent {
EventKind::Any => "any",
EventKind::Access(_) => "access",
EventKind::Create(_) => "create",
- EventKind::Modify(_) => "modify",
+ EventKind::Modify(modify_kind) => match modify_kind {
+ ModifyKind::Name(_) => "rename",
+ ModifyKind::Any
+ | ModifyKind::Data(_)
+ | ModifyKind::Metadata(_)
+ | ModifyKind::Other => "modify",
+ },
EventKind::Remove(_) => "remove",
EventKind::Other => "other",
};
diff --git a/tests/unit/fs_events_test.ts b/tests/unit/fs_events_test.ts
index 4f7cdc4d5..3a867f07e 100644
--- a/tests/unit/fs_events_test.ts
+++ b/tests/unit/fs_events_test.ts
@@ -70,6 +70,26 @@ Deno.test(
},
);
+Deno.test(
+ { permissions: { read: true, write: true } },
+ async function watchFsRename() {
+ const testDir = await makeTempDir();
+ const watcher = Deno.watchFs(testDir);
+ async function waitForRename() {
+ for await (const event of watcher) {
+ if (event.kind === "rename") {
+ break;
+ }
+ }
+ }
+ const eventPromise = waitForRename();
+ const file = testDir + "/file.txt";
+ await Deno.writeTextFile(file, "hello");
+ await Deno.rename(file, testDir + "/file2.txt");
+ await eventPromise;
+ },
+);
+
// TODO(kt3k): This test is for the backward compatibility of `.return` method.
// This should be removed at 2.0
Deno.test(