Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: rename watch event missing #24893

Merged
merged 7 commits into from
Aug 7, 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
9 changes: 8 additions & 1 deletion cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 2 additions & 0 deletions ext/node/polyfills/_fs/_fs_watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ function convertDenoFsEventToNodeFsEvent(
): NodeFsEventType {
if (kind === "create" || kind === "remove") {
return "rename";
} else if (kind === "rename") {
return "rename";
} else {
return "change";
}
Expand Down
9 changes: 8 additions & 1 deletion runtime/ops/fs_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
};
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/fs_events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down