Skip to content

Share subprocess stdin writer with the shell and macro-ize builtin output vtables - #37361

Closed
robobun wants to merge 11 commits into
claude/split/install-clifrom
farm/df8b29f2/split-shell
Closed

robobun wants to merge 11 commits into
claude/split/install-clifrom
farm/df8b29f2/split-shell

Conversation

@robobun

@robobun robobun commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

What this does

Two dedups. (1) shell/subproc.rs had its own copy of the Blob/ArrayBuffer stdin buffering setup from subprocess/Writable.rs; the latter now exposes buffered_stdin_writer and the shell calls it. Readable::init in subproc also collapses its windows/non-windows duplicate match arms into one match with per-arm cfg blocks. (2) Six shell builtins (basename, cp, dirname, ls, mkdir, touch) each hand-wrote the same OutputTaskVTable impl plus on_io_writer_chunk; these are now generated by impl_output_task_vtable! in interpreter.rs, parameterized on where the builtin keeps its output queue.

Split 4 of 8 from #32023, which alii asked to be broken up. Pure code motion and deletion; no intended behavior change. Stacked on claude/split/install-cli (merge order: foundations → install-cli → these, in any order among themselves).

Notes from rebasing against main

Main added Stdio::SocketFd (#32363); the arm is carried into both the unified Readable::init match and the Writable::init match. mkdir was the only builtin whose on_io_writer_chunk panicked explicitly on Idle/Done; the macro emits the if let Exec … else None shape the other five already used. Idle still panics inside next(), and Done is unreachable there because chunks are only enqueued while in Exec.

Verification

  • Compiles in isolation against the base (cargo check), and the union of all eight splits is byte-identical to Deduplicate jsc bindings, runtime api, test matchers, and shell builtins #32023's head f29ce8fe4, which CI ran at 285/286 green (the one red was the fleet-wide napi.test.ts GC-timing flake on windows x64-baseline).
  • Locally on the parent head: bunshell.test.ts (484 pass), shell/commands/* (ls permission-denied tests fail identically under the release binary because the container runs as root), spawn-streaming-stdin (pass).
  • No new tests: this adds no behavior, so the existing suites above are the regression coverage.

alii and others added 11 commits June 9, 2026 13:19
Resolved src/io/posix_event_loop.rs: #32594 added PollMemoryPressure to
is_registered(), which this branch moved into impl_file_poll_flag_methods;
carried the flag into the macro body.

Restored ArrayHashMap::get_index_mut: gained a caller in
react_compiler/collections.rs via #32504.
Resolved src/bun_core/string/mod.rs: #33909 switched return types to
crate::CrateResult<()> in write_pre_quoted_string and quote_for_json,
which this branch restructures into the const-generic
write_pre_quoted_string_inner; carried CrateResult<()> into both the
adapter and _inner signatures.

Resolved src/router/lib.rs and src/sourcemap/Chunk.rs: #33909 touched
return types inside blocks this branch deletes (orphaned router test
scaffolding, uncalled print_source_map_contents); kept the deletions
after re-verifying zero callers on current main.

Restored Node::find_last in src/collections/pool.rs: gained a test
caller via #33311.

@claude claude 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.

I reviewed this PR and didn't find any bugs. Because it restructures unsafe ownership-transfer code in subprocess stdin setup and introduces a new macro abstraction across four shell builtins, a human look would still be worthwhile.

Checked that buffered_stdin_writer preserves the ManuallyDrop/ptr::read semantics of both original call sites, and that the collapsed Writable::init/Readable::init matches in subproc.rs produce the same per-platform arms (including the new SocketFd). Verified impl_output_task_vtable! expands to the hand-written impls it replaces for cp/ls/mkdir/touch; the mkdir Idle/Done panic relaxation is noted in the description and Idle still panics via next(). Confirmed Kind::Dirname.usage_string() matches the old hardcoded literal, and the shell_cmd_args_from_js extraction is testing-API-only.

Extended reasoning...

Overview

This PR is split 4 of 8 from #32023, which a maintainer asked to be broken up. It touches 10 files across the shell interpreter, shell builtins, and subprocess stdio setup — roughly 450 lines removed and 250 added. The changes are three deduplications:

  1. buffered_stdin_writer (subprocess/Writable.rs): extracts the Blob/ArrayBuffer → StaticPipeWriter setup into a generic helper shared by Bun.spawn and the shell's Writable::init (four call sites collapsed to one helper).
  2. subproc.rs match collapse: Writable::init and Readable::init each had two near-identical #[cfg(windows)] / #[cfg(not(windows))] match blocks; these are merged into one match with per-arm cfg blocks where behavior differs.
  3. impl_output_task_vtable! (interpreter.rs): a declarative macro that stamps out the OutputTaskVTable impl and on_io_writer_chunk for cp/ls/mkdir/touch, parameterized on where the output queue lives (queue_in_exec vs queue_on_self). Separately, basename/dirname are unified into PathBuiltin<T: PathTransform>.

A minor fourth change extracts a shared argument-parsing prologue for the shell_lex/shell_parse internal-for-testing APIs in shell_body.rs.

Security risks

None identified. No user-facing input parsing changes, no auth/crypto/permissions surface. The shell_body.rs change is confined to bun:internal-for-testing entry points.

Level of scrutiny

Medium-high. While framed as pure code motion, the change:

  • Moves unsafe { core::ptr::read } + ManuallyDrop ownership-transfer logic for Stdio::Blob into a shared helper — a mistake here would double-drop or leak a Blob across every stdin-buffered spawn.
  • Restructures cross-platform cfg-gated match arms in subprocess stdio setup, where a mis-merged arm on the non-compiled platform would go unnoticed until Windows CI.
  • Introduces a ~140-line declarative macro that generates unsafe code (OutputTask::on_io_writer_chunk) at four expansion sites. The macro relies on unhygienic identifiers (State, Builtin, State::Exec, State::WaitingWriteErr) resolving in each caller's module.
  • Carries one documented behavior change: mkdir's on_io_writer_chunk no longer panics on State::Done (it now falls through to next() → Builtin::done(0)). The description argues Done is unreachable there; I agree, but it is still a change a maintainer should sign off on.

I traced each macro expansion against the deleted hand-written impl and confirmed they match (modulo the mkdir relaxation above). I also traced every arm of the collapsed Writable::init/Readable::init against both old platform blocks and confirmed identical outcomes, including the newly-added Stdio::SocketFd arm. The PathBuiltin<T> unification preserves dirname's exact usage string via Kind::Dirname.usage_string() (verified against Builtin.rs:184).

Other factors

  • Part of a stacked series a maintainer explicitly requested; that maintainer is the natural reviewer for whether the split boundaries and the macro design are what they had in mind.
  • No new tests, justified as behavior-preserving; existing coverage (bunshell.test.ts, shell/commands/*, spawn-streaming-stdin) was run locally per the description.
  • The macro is a new cross-cutting abstraction — per the repo's own review guidelines ("new cross-cutting abstractions need maintainer agreement"), that alone warrants a human look before merge.

@robobun
robobun force-pushed the claude/split/install-cli branch from 2f5dd36 to 9ae3ff5 Compare August 11, 2026 02:09
@robobun

robobun commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 11:05 PM PT - Aug 10th, 2026

@alii, your commit a573a6c is building: #91736

@robobun

robobun commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 5:05 AM PT - Aug 11th, 2026

❌ @alii, your commit a573a6c has some failures in Build #91736 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 37361

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

bun-37361 --bun

@robobun
robobun force-pushed the claude/split/install-cli branch from b5b9aaa to 23cbfb4 Compare August 16, 2026 20:16
@robobun

robobun commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as superseded: the current, main-rebased version of this change lives in #32023, which is the PR being landed (see #37358 and Jarred closing #37362 for the same disposition). Recutting the splits from #32023's head is available on request there.

@robobun robobun closed this Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants