oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.23k stars 2.69k forks source link

Bun.write() sometimes writes empty files (Windows) #13224

Open GustavBW opened 1 month ago

GustavBW commented 1 month ago

What version of Bun is running?

1.1.22

What platform is your computer?

Microsoft Windows NT 10.0.22621.0 x64

What steps can reproduce the bug?

Write any series of array buffers with data to disk, and sometimes, the resulting file will be empty:

const blobs: Blob[] = [...];
for (const blob of blobs) {
    const data = await blob.arrayBuffer();
    await Bun.write(<some file to output to>, data);
}

What is the expected behavior?

That the file contains the data in the array buffer.

What do you see instead?

When inspecting the files in the File Explorer, they're completely empty. Also, when trying to open the files with the normal image viewer, they're corrupted or "something went wrong when trying to open..."

Additional information

Bun reports the amount of bytes written to equal that of the byteLength of the arrayBuffer:

const blobs: Blob[] = [...];
for (const blob of blobs) {
    const data = await blob.arrayBuffer();
    const bytesWritten = await Bun.write(<some file to output to>, data);
    expect(bytesWritten).toEqual(data.byteLength); //resolves to true consistently
}

So it may be a case of Windows doing Windows things.

Jarred-Sumner commented 1 month ago

Can you run bun upgrade? The current version of Bun is v1.1.22

Also, note that this will need an await Promise.all:

const blobs: Blob[] = [...];
blobs.map(b => {
    const data = await blob.arrayBuffer();
    await Bun.write(<some file to output to>, data);
})
GustavBW commented 1 month ago

Can you run bun upgrade? The current version of Bun is v1.1.22

I tried before posting, but I'm getting a funky error:

E:\GitHub\devour> bun upgrade
Bun v1.1.22 is out! You're on v1.1.4

uh-oh: attempt to unwrap error
bun will crash now 😭😭😭

----- bun meta -----
Bun v1.1.4 (fbe2fe0c) Windows x64 (baseline)
UpgradeCommand:
Elapsed: 1563ms | User: 0ms | Sys: 0ms
RSS: 79.50MB | Peak: 79.50MB | Commit: 0.14GB | Faults: 19612
----- bun meta -----

0   00007FF7040A0508
1   ???
2   ???
3   ???
4   ???
5   ???
6   ???
7   ???
8   ???
9   ???

Search GitHub issues https://bun.sh/issues or join in #windows channel in https://bun.sh/discord

thread 21324 panic: attempt to unwrap error
???:?:?: 0x7ff702cb4d51 in ??? (bun.exe)
???:?:?: 0x7ff702cb293d in ??? (bun.exe)
???:?:?: 0x7ff702cb53b6 in ??? (bun.exe)
???:?:?: 0x7ff702cc18e4 in ??? (bun.exe)
???:?:?: 0x7ff7029f15fe in ??? (bun.exe)
???:?:?: 0x7ff70559d943 in ??? (bun.exe)
???:?:?: 0x7ffd2d07257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffd2edcaf27 in ??? (ntdll.dll)

Also, note that this will need an await Promise.all:

I'm aware. In my impl. I'm using a standard for loop. Don't know why I went with Array.map() here - editing that real quick.

Jarred-Sumner commented 1 month ago

Can you reinstall from curl? Some of those early versions would repeatedly crash in bun upgrade on Windows

GustavBW commented 1 month ago

Can you reinstall from curl? Some of those early versions would repeatedly crash in bun upgrade on Windows

Good idea. Just did, now on v. 1.1.22, but observing the same behavior. Ill update the issue