Skip to content

Bun.write do short write when copying file #22456

Closed as duplicate
Closed as duplicate

Description

@terrywh

What version of Bun is running?

1.2.21

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What steps can reproduce the bug?

const content1 = "this is a long long long long line";
const file1 = Bun.file("./file1.txt"); // not exists yet
await Bun.write(file1, content1);
const file2 = Bun.file("./file2.txt"); // not exists yet
await Bun.write(file2, "short line");
const file3 = Bun.file("./file3.txt"); // not exists yet

// comment these 3 lines everything works
if (await file2.exists()) {
    await Bun.write(file3, file2); // backup
}

await Bun.write(file2, file1);
await file1.delete();
const content2 = await file2.text();
if (await file3.exists()) {
    await Bun.write(file2, file3);
    await file3.delete();
}

console.log(content1); // this is a long long long long line
console.log(content2); // this is a

What is the expected behavior?

write to file2 with the whole content of file1.

What do you see instead?

file 2 only contains a partial of file1.

Additional information

2025-09-07:
tried in WSL ubuntu, this same problem:
Image

2025-09-08:
tried in MacOS, this same problem:
Image

Activity

  1. linear commented on Sep 7, 2025

    @linear
  2. changed the title [-]Bun.write do short write[/-] [+]Bun.write do short write when copying file[/+] on Sep 7, 2025
  3. terrywh commented on Sep 18, 2025

    @terrywh
    Author

    the problem still exists in bun v1.2.22:

    > bun run test.js
    this is a long long long long line
    this is a 
    > bun --version
    1.2.22
    > uname -a
    Linux VM-193-107-tencentos 6.6.80-29.tl4.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Mar 24 21:16:21 CST 2025 x86_64 x86_64 x86_64 GNU/Linux
    
  4. terrywh commented on Oct 9, 2025

    @terrywh
    Author

    bun/1.2.23, problem persists:
    Image

  5. terrywh commented on Oct 11, 2025

    @terrywh
    Author

    bun/v1.3.0 problem persists:

    Image
  6. terrywh commented on Nov 11, 2025

    @terrywh
    Author

    bun/v1.3.2 problem persists:
    Image

  7. terrywh commented on Nov 28, 2025

    @terrywh
    Author

    bun/v1.3.3:

    Image
  8. terrywh commented on Dec 8, 2025

    @terrywh
    Author

    bun/v1.3.4:
    Image

  9. added 4 commits that reference this issue on Aug 4, 2026
    54f8516
    31c657b
    83df194
    be367ef
  10. Jarred-Sumner commented on Aug 13, 2026

    @Jarred-Sumner
    Collaborator

    #4930 is the same bug: once a BunFile's size has been resolved (.exists(), .size, or being used as a copy source), Bun.write(thatFile, otherBunFile) uses the cached size as the copy limit on the file-to-file path, which is why file2 ended up with exactly the 10 bytes of its previous contents. #4930 is the older report and was re-confirmed on 1.3.14 recently, so I'm closing this as a duplicate. Until it's fixed, use a fresh Bun.file(path) or a string path as the destination.

    Duplicate of #4930

    Please add a 👍 or any extra details over there so it all stays in one place. If this is actually a different problem, say so here and we'll reopen.

    (This comment was written by Claude, on behalf of the Bun team.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbun:fs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions