Skip to content

zig: fix bun.platformIOVecConstCreate args - #23480

Closed
nektro wants to merge 3 commits into
mainfrom
nektro-patch-763
Closed

nektro wants to merge 3 commits into
mainfrom
nektro-patch-763

Conversation

@nektro

@nektro nektro commented Oct 11, 2025

Copy link
Copy Markdown
Contributor

addresses a TODO

@robobun

robobun commented Oct 11, 2025 •

Copy link
Copy Markdown
Collaborator
Updated 3:35 PM PT - Oct 14th, 2025

❌ @nektro, your commit 8b9b4fb has some failures in Build #29179 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 23480

That installs a local version of the PR into your bun-23480 executable, so you can run:

bun-23480 --bun

@nektro
nektro marked this pull request as ready for review October 14, 2025 21:54
@coderabbitai

coderabbitai Bot commented Oct 14, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces a const buffer type for libuv (uv_buf_t_const), updates extern signatures (uv_write, uv_fs_write), and adjusts callers across Windows IPC, pipe writers, Node fs, and web blob writing. Refactors PlatformIOVec creation and writev signatures to const variants, and updates buffer initializations to .init(...) patterns.

Changes

Cohort / File(s) Summary
Libuv const buffer introduction and API updates
src/deps/libuv.zig
Adds uv_buf_t_const (read-only), updates uv_buf_t init and adds slice(). Changes extern signatures: uv_write/uv_fs_write now take [ * ]const uv_buf_t_const. Adjusts internal write paths to pass input[0..1] and updates related structs/methods to use const buffers.
Windows writers and IPC buffer type switch
src/io/PipeWriter.zig, src/bun.js/ipc.zig
Replaces uv.uv_buf_t with uv.uv_buf_t_const for write buffers and switches initializations to .init(...). Updates all assignments and construction sites accordingly.
Web blob write buffer constness
src/bun.js/webcore/blob/write_file.zig
Changes WriteFileWindows.uv_bufs from [1]uv.uv_buf_t to [1]uv.uv_buf_t_const.
Platform IOVec refactor
src/bun.zig
Updates platformIOVecCreate to take []u8 and set base directly; platformIOVecConstCreate sets base from input.ptr. Aligns Windows const variant to uv_buf_t_const.
UV-backed sys write helpers
src/sys_uv.zig
Switches to .init(buf) for iovec creation; changes writev signature to []const bun.PlatformIOVecConst and updates internal usages for read/pwrite/write paths.
Node fs write adjustments
src/bun.js/node/node_fs.zig
Replaces local alias with direct .init(buf); updates writev path pointer handling using @ptrCast(bufs.ptr) for multi-buffer writes.

Possibly related PRs

Suggested reviewers

  • pfgithub
  • zackradisic
  • taylordotfish

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description does not follow the repository’s template and is missing both the “What does this PR do?” and “How did you verify your code works?” sections, providing only a vague note that it “addresses a TODO.” Please update the description to include the required template headings and detailed information: explain the purpose of the changes under “What does this PR do?” and describe your testing or verification steps under “How did you verify your code works?”.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title accurately references a specific change to the bun.platformIOVecConstCreate function signature, which is indeed part of the modifications in this pull request, even though other related const buffer updates are also included. It is concise, clear, and directly reflects a real aspect of the changeset without being misleading.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (4)
src/bun.js/webcore/blob/write_file.zig (1)

593-597: Remove @constcast and use uv_buf init helper for clarity

Now that uv_buf is const, @constcast is unnecessary. Prefer the init helper for consistency with other call sites.

-        this.uv_bufs[0].base = @constCast(remain.ptr);
-        this.uv_bufs[0].len = @truncate(remain.len);
+        this.uv_bufs[0] = uv.uv_buf_t_const.init(remain);

As per coding guidelines

src/sys_uv.zig (1)

321-324: Fix compile-time assert to match const write type

pwritev works with PlatformIOVecConst; the assert still checks the mutable type. Update to reflect the const variant.

Apply this diff:

-    comptime bun.assert(bun.PlatformIOVec == uv.uv_buf_t);
+    comptime bun.assert(bun.PlatformIOVecConst == uv.uv_buf_t_const);
src/deps/libuv.zig (2)

740-752: Fix const slice return type (compile-time/type-safety issue)

uv_buf_t_const.slice returns []u8 but base is [*]const u8. This discards constness and likely won’t compile. Return []const u8.

Apply this diff:

 pub const uv_buf_t_const = extern struct {
   len: ULONG,
   base: [*]const u8,

   pub fn init(input: []const u8) uv_buf_t_const {
     bun.assert(input.len <= @as(usize, std.math.maxInt(ULONG)));
     return .{ .len = @truncate(input.len), .base = input.ptr };
   }

-  pub fn slice(this: *const @This()) []u8 {
+  pub fn slice(this: *const @This()) []const u8 {
     return this.base[0..this.len];
   }
 };

1277-1295: Unify uv_write buffer passing; drop unnecessary casts

The callback branch correctly uses input[0..1]. The no-callback branch still uses @ptrCast. Prefer input[0..1] for both.

Apply this diff:

-        const rc = uv_write(req, stream, @ptrCast(input), 1, null);
+        const rc = uv_write(req, stream, input[0..1], 1, null);

Optional follow-up (outside these lines): consider updating StreamMixin.write to accept *uv_buf_t_const and use input[0..1] to avoid @ptrCast, keeping types consistent with the new extern.

Also applies to: 1297-1302

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9086b8f and 8b9b4fb.

📒 Files selected for processing (7)
  • src/bun.js/ipc.zig (2 hunks)
  • src/bun.js/node/node_fs.zig (2 hunks)
  • src/bun.js/webcore/blob/write_file.zig (1 hunks)
  • src/bun.zig (1 hunks)
  • src/deps/libuv.zig (5 hunks)
  • src/io/PipeWriter.zig (6 hunks)
  • src/sys_uv.zig (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.zig

📄 CodeRabbit inference engine (.cursor/rules/javascriptcore-class.mdc)

**/*.zig: Declare the extern C symbol in Zig and export a Zig-friendly alias for use
Wrap the Bun____toJS extern in a Zig method that takes a JSGlobalObject and returns JSC.JSValue

Files:

  • src/bun.js/ipc.zig
  • src/bun.js/webcore/blob/write_file.zig
  • src/io/PipeWriter.zig
  • src/sys_uv.zig
  • src/bun.js/node/node_fs.zig
  • src/deps/libuv.zig
  • src/bun.zig
src/bun.js/**/*.zig

📄 CodeRabbit inference engine (.cursor/rules/zig-javascriptcore-classes.mdc)

src/bun.js/**/*.zig: In Zig binding structs, expose generated bindings via pub const js = JSC.Codegen.JS and re-export toJS/fromJS/fromJSDirect
Constructors and prototype methods should return bun.JSError!JSC.JSValue to integrate Zig error handling with JS exceptions
Use parameter name globalObject (not ctx) and accept (*JSC.JSGlobalObject, *JSC.CallFrame) in binding methods/constructors
Implement getters as get(this, globalObject) returning JSC.JSValue and matching the .classes.ts interface
Provide deinit() for resource cleanup and finalize() that calls deinit(); use bun.destroy(this) or appropriate destroy pattern
Access JS call data via CallFrame (argument(i), argumentCount(), thisValue()) and throw errors with globalObject.throw(...)
For properties marked cache: true, use the generated Zig accessors (NameSetCached/GetCached) to work with GC-owned values
In finalize() for objects holding JS references, release them using .deref() before destroy

Files:

  • src/bun.js/ipc.zig
  • src/bun.js/webcore/blob/write_file.zig
  • src/bun.js/node/node_fs.zig
src/**/*.zig

📄 CodeRabbit inference engine (CLAUDE.md)

In Zig code, manage memory carefully and use defer for cleanup of allocations/resources

src/**/*.zig: Use the # prefix to declare private fields in Zig structs (e.g., struct { #foo: u32 })
Prefer decl literals when initializing values in Zig (e.g., const decl: Decl = .{ .binding = 0, .value = 0 })
Place @import directives at the bottom of Zig files
Use @import("bun") instead of @import("root").bun

When adding debug logs in Zig, create a scoped logger and log via Bun APIs: const log = bun.Output.scoped(.${SCOPE}, .hidden); then log("...", .{})

Files:

  • src/bun.js/ipc.zig
  • src/bun.js/webcore/blob/write_file.zig
  • src/io/PipeWriter.zig
  • src/sys_uv.zig
  • src/bun.js/node/node_fs.zig
  • src/deps/libuv.zig
  • src/bun.zig
🔇 Additional comments (15)
src/bun.js/webcore/blob/write_file.zig (1)

347-347: Windows const-buffer type change LGTM

Switch to [1]uv.uv_buf_t_const matches the new const write path. No further issues here.

src/bun.js/node/node_fs.zig (1)

211-211: Single-buffer write init usage is good

Using inferred .init(buf) aligns with the const uv buffer refactor and keeps the call concise.

src/bun.zig (1)

257-260: Const IOV adoption looks correct

  • PlatformIOVecConst → uv_buf_t_const on Windows is right.
  • platformIOVecCreate/ConstCreate use direct ptr with truncated len; no constCast; aligns with libuv const semantics.

Also applies to: 261-263, 265-267

src/bun.js/ipc.zig (2)

399-407: Switch to uv_buf_t_const is appropriate

write_buffer now uses the const variant with an empty init; matches the updated libuv API.


812-821: Buffer init via .init(write_req_slice) is correct

Using uv_buf_t_const.init(...) with a dup’d slice ensures stable memory until completion.

src/sys_uv.zig (4)

365-366: Use of .init for single-buffer read is fine

Correctly initializes a one-element uv buffer; consistent with the new pattern.


374-376: pwrite single-buffer init is correct

One-element const IOV via .init(buf) matches the new API.


378-381: write single-buffer init is correct

Uses const IOV and delegates to writev; consistent with pwrite/writev changes.


369-371: writev callers updated to use const IOVs Verified the inline write wrapper passes a []const bun.PlatformIOVecConst via &bufs.

src/io/PipeWriter.zig (3)

943-944: Use uv_buf_t_const for write_buffer

Type swap and .init("") default are correct and consistent with upstream libuv changes.


1061-1072: Initialize const buffers with .init(buffer)

Both fs and pipe paths use uv_buf_t_const.init(...); matches the const write semantics.


1406-1418: Streaming writer: const buffer init is correct

current_payload/outgoing ensure stable memory; initializing uv_buf_t_const with .init(bytes) is safe here.

src/deps/libuv.zig (3)

731-739: uv_buf_t helpers look correct

Length truncation and slice exposure for mutable buffers are correct. No issues.


1273-1276: write_buffer constness change is correct

Switching write_buffer to uv_buf_t_const aligns with const write paths.


2238-2238: Extern uv_write now takes const buffers — good

Signature matches the new const semantics and is ABI-compatible with libuv’s uv_buf_t.

Please also update remaining call sites to pass slices (bufs[0..1]) where applicable for consistency.

Comment thread src/bun.js/node/node_fs.zig Resolved
Comment thread src/deps/libuv.zig Resolved
@nektro
nektro requested a review from dylan-conway October 14, 2025 22:49
Comment thread src/deps/libuv.zig
Comment on lines +740 to +742
pub const uv_buf_t_const = extern struct {
len: ULONG,
base: [*]const u8,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the C standard, this type isn't guaranteed to have the same representation as uv_buf_t. So by declaring uv_write as taking [*]const uv_buf_t_const, we are declaring it with an incompatible signature that doesn't match how it is defined in C.

Maybe it happens to work in practice; I think it's better not to risk it.

@robobun

robobun commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Closing as stale: this PR predates the Rust rewrite. Every src/ file it modifies has since been removed or relocated on main (Zig sources deleted; src/bun.js/ reorganized into src/jsc/), so it can no longer merge.

If the underlying change is still wanted, it will need to be redone against the current Rust/C++ tree. Apologies for the churn, and thank you for the contribution.

@robobun robobun closed this Jun 26, 2026
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.

3 participants