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

file descriptor rewrite #18790

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

file descriptor rewrite #18790

wants to merge 2 commits into from

Conversation

paperclover
Copy link
Member

I've wanted to do this fix since Bun 1.1

FileDescriptor and FDImpl are currently two separate structures. Originally, the former was an integer type and the latter was a packed struct introduced for Windows. With improvements to the Zig language (equality operator on packed struct and decl literals), it is now time to make FDImpl the actual file descriptor struct. This PR merges the two into bun.FD

In exchange for this, bun.FD has new helpful methods:

const sub_path_fd = fd.openat("hello.txt", 0, 0).unwrap()
defer sub_path_fd.close();

Migration Guide

  • bun.STDIN_FD -> bun.FD.stdin()
  • bun.STDOUT_FD -> bun.FD.stdout()
  • bun.STDERR_FD -> bun.FD.stderr()
  • bun.toFD(value)
    • FD.fromNative(value) (i32 on posix, HANDLE on windows)
    • FD.fromUV(value) (i32 on posix, uv_file on windows)
    • FD.fromStdFile(value) from std.fs.File
    • FD.fromStdDir(value) from std.fs.Dir
  • bun.uvfdcast(fd) -> fd.uv()
  • fd.asFile() -> fd.stdFile()
  • fd.asDir() -> fd.stdDir()
  • bun.FDTag -> bun.FD.Stdio
  • bun.FDTag.get(fd) -> fd.stdioTag() or bun.FD.Stdio.fromInt(fd)
  • bun.stdio(i) / bun.{posix,win32}.stdio(i) -> bun.FD.Stdio.fromInt(i).?.fd()
  • bun.sys.close and friends
    • fd.close() to assert the fd is valid
    • fd.closeAllowingBadFileDescriptor() if EBADF is allowed

The following are deprecated but left in to reduce diff size:

  • bun.FileDescriptor -> bun.FD
  • bun.StoredFileDescriptorType -> bun.FD
  • fd.cast() -> fd.native()
  • std.fs.cwd() -> bun.FD.cwd()
  • std.fs.File and std.fs.Dir and std.posix should be avoided.
  • bun.sys.File will be deprecated in favor of bun.FD's methods. potentially add bun.FD.Dir
  • bun.invalid_fd -> bun.FD.invalid -> bun.FD.Optional.none

The following patterns are banned

  • std.fs.Dir
  • bun.invalid_fd
  • std.fs.cwd
  • std.fs.File
  • .stdFile()
  • .stdDir()

Why Now

This is a part of the usingnamespace / incremental compilation effort. Currently, bun.STD*_FD is usingnamespaced to a var declaration on windows and a const declaration on posix. This PR cleans this up to be where it should be: methods on bun.FD

CleanShot 2025-04-04 at 18 32 30@2x

@robobun
Copy link

robobun commented Apr 5, 2025

Updated 8:04 PM PT - Apr 4th, 2025

@paperclover, your commit d774ee7 has 18 failures in Build #14501:


🧪   try this PR locally:

bunx bun-pr 18790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants