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

Bun throws error when copying file to a folder that does not exist #11260

Closed
birkskyum opened this issue May 22, 2024 · 12 comments · Fixed by #11433
Closed

Bun throws error when copying file to a folder that does not exist #11260

birkskyum opened this issue May 22, 2024 · 12 comments · Fixed by #11433
Labels
bug Something isn't working

Comments

@birkskyum
Copy link
Collaborator

birkskyum commented May 22, 2024

What version of Bun is running?

1.1.10-canary.1+cc0bce62e

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

I experience that I can make a clean basic example (bun create solid), dump a few files in the public folder, like .glb 3d models, and then the bun --bun run build command will break with:

 ERROR  { [ENOENT: No such file or directory]                                                                                                            9:06:21 AM
  code: 'ENOENT',
  path:
   '/Users/admin/repos/bun-kitchensink/solid/wefwef/public/3dmodels/giantherb/giantherb.glb',
  syscall: 'copyfile',
  errno: -2 } 



 ERROR  No such file or directory                                                                                                                        9:06:21 AM

error: script "build" exited with code 1

It's worth noting that it's not a requirement to use these files anywhere in the code for this to break

What is the expected behavior?

When running pnpm run build or npm, then the build completes

What do you see instead?

No response

Additional information

Related to

@birkskyum birkskyum added the bug Something isn't working label May 22, 2024
@paulo-assoc
Copy link

Confirmed, @birkskyum. I have the same issue (used Bun v1.1.9 on Windows 11):

image

@zoto-ff
Copy link

zoto-ff commented May 24, 2024

the error occurs when working with nitropack (vinxi/nitro component). something is wrong with fs.promises.cp(). this happens even if all copies are executed sequentially (for (...) { await ... })

IMG_6463

https://github.com/unjs/nitro/blob/61f2079a605fca81b578894edfdbb0c328db12a3/src/build.ts#L87

@sirenkovladd
Copy link
Contributor

sirenkovladd commented May 25, 2024

Smaller reproduce

import { promises } from 'node:fs';

const src = 'newFile.txt';
const folder = 'folder-not-exist'
const dst = `${folder}/${src}`;

await promises.writeFile(src, 'A'.repeat(131073))

await promises.rm(folder, { recursive: true, force: true })
await promises.cp(src, dst);
console.log('Success')
❯ bun test.js 
ENOENT: No such file or directory
   errno: -2
 syscall: "copyfile"
   path: "newFile.txt"


Bun v1.1.10 (macOS arm64)

❯ node test.js
Success

If this works for you, you can try increasing the repetition value

@birkskyum
Copy link
Collaborator Author

birkskyum commented May 25, 2024

Now that it's much more narrowed down - What would be a more accurate title for this issue?

Any pointers to wether the issue is in .repeat() or in the .writeFile?

@paulo-assoc
Copy link

I didn't use .repeat when I got the error (unless SolidStart is using it internally, which I doubt.).

@Huliiiiii
Copy link

Huliiiiii commented May 25, 2024

If folder doesn't exist, cp in node will create folder, but bun won't.

import { promises } from 'node:fs';

const src = 'newFile.txt';
const folder = 'folder-not-exist'
const dst = `${folder}/${src}`;

await promises.writeFile(src, 'A'.repeat(131073))

await promises.rm(folder, { recursive: true, force: true })
await promises.mkdir(folder);
await promises.cp(src, dst);
console.log('Success')

Bun run this => Success

@sirenkovladd
Copy link
Contributor

Bun error occurs if you need to copy a large file to a folder that does not exist
The problem is precisely only in the large file, since everything is fine with small files

@paulo-assoc
Copy link

So @birkskyum, new issue title could be, "Bun throws error when copying large file to a folder that does not exist"?

@birkskyum birkskyum changed the title SolidStart - Issues with asset loading of files in public folder. Bun throws error when copying large file to a folder that does not exist May 26, 2024
@paulo-assoc
Copy link

Thanks to @sirenkovladd for supplying the "smaller reproduce"!

@Huliiiiii
Copy link

如果您需要将large文件复制到不存在的文件夹,则会出现 Bun 错误 问题恰恰只出现在大文件中,因为小文件一切正常

await promises.writeFile(src, 'A')

On my computer, this also causes the error, so I don't think the file size is the issue.

@Huliiiiii
Copy link

Huliiiiii commented May 26, 2024

const fs = $zig("node_fs_binding.zig", "createBinding");

// attempt to use the native code version if possible
// and on MacOS, simple cases of recursive directory trees can be done in a single `clonefile()`
// using filter and other options uses a lazily loaded js fallback ported from node.js
function cp(src, dest, options) {
if (!options) return fs.cp(src, dest);
if (typeof options !== "object") {
throw new TypeError("options must be an object");
}
if (options.dereference || options.filter || options.preserveTimestamps || options.verbatimSymlinks) {
return require("../internal/fs/cp")(src, dest, options);
}
return fs.cp(src, dest, options.recursive, options.errorOnExist, options.force ?? true, options.mode);
}

Set any option that makes this function return the js fallback ported from node will make the program succeed.

import { promises } from "node:fs"

const src = "newFile.txt"
const folder = "folder-not-exist"
const dst = `${folder}/${src}`

await promises.writeFile(src, "A")

await promises.rm(folder, { recursive: true, force: true })
await promises.cp(src, dst, { dereference : true })
await promises.rm(folder, { recursive: true, force: true })
await promises.cp(src, dst, { preserveTimestamps: true })
await promises.rm(folder, { recursive: true, force: true })
await promises.cp(src, dst, { verbatimSymlinks: true })
console.log("Success")

So I think the problem is in zig's implementation.

@birkskyum birkskyum changed the title Bun throws error when copying large file to a folder that does not exist Bun throws error when copying file to a folder that does not exist May 26, 2024
@birkskyum
Copy link
Collaborator Author

birkskyum commented May 26, 2024

Nice find with the repro @Huliiiiii . Seems like some zig expertise is due at this point. cc @paperdave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants