diff --git a/docs/runtime/file-io.mdx b/docs/runtime/file-io.mdx index 3192d31a1f..6eb942454f 100644 --- a/docs/runtime/file-io.mdx +++ b/docs/runtime/file-io.mdx @@ -154,6 +154,8 @@ await Bun.write("index.html", response); ## Renaming files (`Bun.rename()`) +`Bun.rename(from, to, conflict?): Promise` + Atomically rename or move files and directories. Similar to Node.js `fs.promises.rename()` but with an optional third parameter for conflict resolution. ```ts @@ -167,6 +169,14 @@ await Bun.rename("file1.txt", "file2.txt", "swap"); await Bun.rename("source.txt", "dest.txt", "no-replace"); ``` +The `conflict` parameter controls behavior when the destination already exists: + +| Mode | Description | +| ---- | ----------- | +| `"replace"` | Replace destination if it exists (default) | +| `"swap"` | Atomically swap the two files (Linux/macOS only, falls back to `"replace"` on Windows) | +| `"no-replace"` | Fail with an error if destination exists | + --- ## Incremental writing with `FileSink` diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 1781174f65..53465f07ed 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -840,10 +840,10 @@ const RenameJob = struct { .err => |*err| { defer err.deinit(); const error_instance = globalThis.createErrorInstance("rename failed: errno {d}", .{err.errno}); - promise.reject(globalThis, error_instance); + promise.reject(globalThis, error_instance) catch {}; }, .result => { - promise.resolve(globalThis, jsc.JSValue.js_undefined); + promise.resolve(globalThis, jsc.JSValue.js_undefined) catch {}; }, }