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

node:fs/promises.cp should make path if dest does not exist #11433

Merged
merged 3 commits into from
May 30, 2024
Merged
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
@@ -6627,7 +6627,12 @@ pub const NodeFS = struct {
mode_ |= C.darwin.COPYFILE_EXCL;
}

return ret.errnoSysP(C.copyfile(src, dest, null, mode_), .copyfile, src) orelse ret.success;
const first_try = ret.errnoSysP(C.copyfile(src, dest, null, mode_), .copyfile, src) orelse return ret.success;
nektro marked this conversation as resolved.
Show resolved Hide resolved
if (first_try == .err and first_try.err.errno == @intFromEnum(C.E.NOENT)) {
bun.makePath(std.fs.cwd(), bun.path.dirname(dest, .auto)) catch {};
return ret.errnoSysP(C.copyfile(src, dest, null, mode_), .copyfile, src) orelse ret.success;
}
return first_try;
}

if (Environment.isLinux) {
15 changes: 15 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
@@ -3216,3 +3216,18 @@ it("promises.fdatasync with a bad fd should include that in the error thrown", a
}
expect.unreachable();
});

it("promises.cp should work even if dest does not exist", async () => {
const x_dir = tmpdirSync();
let src = "package-lock.json";
let folder = "folder-not-exist";
let dst = join(folder, src);

src = join(x_dir, src);
folder = join(x_dir, folder);
dst = join(x_dir, dst);

await promises.writeFile(src, "A".repeat(131073));
await promises.rm(folder, { recursive: true, force: true });
await promises.cp(src, dst);
nektro marked this conversation as resolved.
Show resolved Hide resolved
});