diff options
author | 2023-05-17 20:19:31 -0700 | |
---|---|---|
committer | 2023-05-17 20:19:31 -0700 | |
commit | 4f7198f780fa5e345c6eeed3f98f30b03f2a6d32 (patch) | |
tree | 24e5e3d4e239b8b4387c234ca0ad7f3c0795fb55 | |
parent | 21086c3a788dca6bc4abbb585ff519f759a4f1b5 (diff) | |
download | bun-4f7198f780fa5e345c6eeed3f98f30b03f2a6d32.tar.gz bun-4f7198f780fa5e345c6eeed3f98f30b03f2a6d32.tar.zst bun-4f7198f780fa5e345c6eeed3f98f30b03f2a6d32.zip |
Fixes https://github.com/oven-sh/bun/issues/2931
-rw-r--r-- | src/bun.js/node/node_fs.zig | 5 | ||||
-rw-r--r-- | test/js/node/fs/fs.test.ts | 26 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index f7300f28e..f1a0e6a82 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -3502,7 +3502,10 @@ pub const NodeFS = struct { } } - _ = ftruncateSync(.{ .fd = fd, .len = @truncate(JSC.WebCore.Blob.SizeType, written) }); + // https://github.com/oven-sh/bun/issues/2931 + if ((@enumToInt(args.flag) & std.os.O.APPEND) == 0) { + _ = ftruncateSync(.{ .fd = fd, .len = @truncate(JSC.WebCore.Blob.SizeType, written) }); + } return Maybe(Return.WriteFile).success; } diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 03742d843..1edbfa11c 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -47,6 +47,32 @@ function mkdirForce(path: string) { if (!existsSync(path)) mkdirSync(path, { recursive: true }); } +it("writeFileSync in append should not truncate the file", () => { + const path = join(tmpdir(), "writeFileSync-should-not-append-" + (Date.now() * 10000).toString(16)); + var str = ""; + writeFileSync(path, "---BEGIN---"); + str += "---BEGIN---"; + for (let i = 0; i < 10; i++) { + const line = "Line #" + i; + str += line; + writeFileSync(path, line, { flag: "a" }); + } + expect(readFileSync(path, "utf8")).toBe(str); +}); + +it("writeFileSync NOT in append SHOULD truncate the file", () => { + const path = join(tmpdir(), "writeFileSync-should-not-append-" + (Date.now() * 10000).toString(16)); + writeFileSync(path, "---BEGIN---"); + var str = "---BEGIN---"; + expect(readFileSync(path, "utf8")).toBe(str); + for (let i = 0; i < 10; i++) { + const line = "Line #" + i; + str = line; + writeFileSync(path, line); + expect(readFileSync(path, "utf8")).toBe(str); + } +}); + describe("copyFileSync", () => { it("should work for files < 128 KB", () => { const tempdir = `${tmpdir()}/fs.test.js/${Date.now()}/1234/hi`; |