command-stream 1.4.0

Modern shell command execution library with streaming, async iteration, and event support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! External command execution: Bun's `ShellSubprocess`
//! (`src/runtime/shell/subproc.rs`) and the subprocess half of the `Cmd`
//! state (`states/Cmd.rs`) (MIT, Copyright (c) Oven-sh / Jarred Sumner),
//! ported by way of `js/src/bun-shell/subprocess.mjs`.
//!
//! The child gets real OS handles wherever possible: redirect files and the
//! process stdout/stderr are passed as duplicated handles, everything else
//! goes through OS pipes that this module drains:
//!
//! - an `Fd` output with a `captured` buffer (the non-quiet root
//!   stdout/stderr) is teed: every chunk goes to the writer and to a buffer
//!   that is appended to `captured` when the child is done;
//! - an `Fd` output whose writer is a pipeline [`Channel`](super::io::Channel)
//!   is pumped into the channel with backpressure; once the reader is gone
//!   the pipe is closed, so the child's next write raises SIGPIPE (exit code
//!   141) like it would in Bun;
//! - a `Pipe` output is buffered and appended to the shell env's buffered
//!   stdout/stderr;
//! - `> ${buf}` fills the [`OutBuffer`] and silently drops the overflow;
//! - `2>&1` / `1>&2` without a file give both fds the same pipe writer, so
//!   the interleaving is exactly the child's.
//!
//! Stdin comes from a file handle, the inherited process stdin, `< ${bytes}`
//! (fed through a pipe) or a pipeline channel (pumped until the child exits).

use std::fs::File;
use std::io;
use std::process::Stdio;
use std::sync::Arc;

use tokio::io::AsyncWriteExt;
use tokio::process::{ChildStdin, Command};

use super::io::{
    dup_process_stdio, redirects_elsewhere, InKind, OutKind, Reader, ReaderSource, SharedBuf,
    ShellIO, ShellSysError, Which, Writer,
};
use super::OutBuffer;

/// A stdin redirect of the command.
#[derive(Debug)]
pub(crate) enum InOverride {
    /// `< file`: an opened file.
    File(Arc<File>),
    /// `< ${bytes}` / `< ${blob}`.
    Bytes(Vec<u8>),
}

/// A stdout/stderr redirect of the command.
#[derive(Debug)]
pub(crate) enum OutOverride {
    /// `> file` / `>> file`: an opened file.
    File(Arc<File>),
    /// `> ${buf}`: a fixed-size buffer; the overflow is dropped.
    Buffer(OutBuffer),
}

/// The command's own redirect targets (the JS `redirectOverridesForFd`).
#[derive(Debug, Default)]
pub(crate) struct Overrides {
    pub(crate) stdin: Option<InOverride>,
    pub(crate) stdout: Option<OutOverride>,
    pub(crate) stderr: Option<OutOverride>,
}

/// `2>&1` / `1>&2` without a file (Bun's `Stdio::Dup2`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Dup {
    StderrToStdout,
    StdoutToStderr,
}

/// Everything [`run_subprocess`] needs.
pub(crate) struct SubprocessOptions<'a> {
    /// `args[0]` is the resolved executable.
    pub(crate) args: Vec<String>,
    pub(crate) cwd: &'a str,
    /// The complete child environment (see `ShellExecEnv::child_env`).
    pub(crate) env: Vec<(String, String)>,
    /// The command's IO.
    pub(crate) io: &'a ShellIO,
    /// The command's `RedirectFlags`.
    pub(crate) flags: u8,
    pub(crate) overrides: Overrides,
    pub(crate) dup: Option<Dup>,
    /// The shell env's buffered stdout/stderr (targets of `Pipe` outputs).
    pub(crate) buffered_stdout: SharedBuf,
    pub(crate) buffered_stderr: SharedBuf,
}

/// How a subprocess ended.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum SubprocessResult {
    /// The exit code: the child's, `128 + signal` when it was killed, or the
    /// errno of a failed write to a teed output.
    Exited(i32),
    /// The child could not be started; the error's path is `args[0]`.
    SpawnError(ShellSysError),
}

// ---------------------------------------------------------------------------
// Plans
// ---------------------------------------------------------------------------

/// Where a piped output's bytes go.
enum Sink {
    /// Bun's `BufferedOutput::Bytelist`.
    Buffer(Vec<u8>),
    /// Bun's `BufferedOutput::ArrayBuffer`: truncates on overflow.
    View { buf: OutBuffer, pos: usize },
    /// Bun's `Capture`: buffered and teed to `writer`; a failed write is
    /// reported as the exit code.
    Tee {
        writer: Writer,
        buf: Vec<u8>,
        err: Option<ShellSysError>,
    },
    /// A pipeline channel, with backpressure.
    Writer(Writer),
}

impl Sink {
    /// What is appended to the capture buffer once the child is done.
    fn slice(&self) -> Option<Vec<u8>> {
        match self {
            Sink::Buffer(b) | Sink::Tee { buf: b, .. } => Some(b.clone()),
            Sink::View { buf, .. } => Some(buf.contents()),
            Sink::Writer(_) => None,
        }
    }

    fn err(&self) -> Option<&ShellSysError> {
        match self {
            Sink::Tee { err, .. } => err.as_ref(),
            _ => None,
        }
    }
}

enum OutPlan {
    File(File),
    Inherit,
    Null,
    Pipe {
        sink: Sink,
        capture: Option<SharedBuf>,
    },
}

enum InPlan {
    File(File),
    Inherit,
    Null,
    Bytes(Vec<u8>),
    Reader(Reader),
}

fn plan_out(
    out: &OutKind,
    ov: Option<OutOverride>,
    buffered: &SharedBuf,
    elsewhere: bool,
) -> io::Result<OutPlan> {
    match ov {
        Some(OutOverride::File(f)) => return Ok(OutPlan::File(f.try_clone()?)),
        Some(OutOverride::Buffer(buf)) => {
            let capture = match out {
                _ if elsewhere => None,
                OutKind::Fd { captured, .. } => captured.clone(),
                OutKind::Pipe => Some(buffered.clone()),
                OutKind::Ignore => None,
            };
            return Ok(OutPlan::Pipe {
                sink: Sink::View { buf, pos: 0 },
                capture,
            });
        }
        None => {}
    }
    Ok(match out {
        OutKind::Fd {
            writer,
            captured: Some(captured),
        } => OutPlan::Pipe {
            sink: Sink::Tee {
                writer: writer.clone(),
                buf: Vec::new(),
                err: None,
            },
            capture: (!elsewhere).then(|| captured.clone()),
        },
        OutKind::Fd {
            writer,
            captured: None,
        } => match writer.try_clone_file() {
            Some(Ok(f)) => OutPlan::File(f),
            Some(Err(_)) => OutPlan::Inherit,
            None => OutPlan::Pipe {
                sink: Sink::Writer(writer.clone()),
                capture: None,
            },
        },
        OutKind::Pipe => OutPlan::Pipe {
            sink: Sink::Buffer(Vec::new()),
            capture: (!elsewhere).then(|| buffered.clone()),
        },
        OutKind::Ignore => OutPlan::Null,
    })
}

fn plan_in(input: &InKind, ov: Option<InOverride>) -> io::Result<InPlan> {
    match ov {
        Some(InOverride::File(f)) => return Ok(InPlan::File(f.try_clone()?)),
        Some(InOverride::Bytes(b)) if b.is_empty() => return Ok(InPlan::Null),
        Some(InOverride::Bytes(b)) => return Ok(InPlan::Bytes(b)),
        None => {}
    }
    let InKind::Fd(reader) = input else {
        return Ok(InPlan::Null);
    };
    Ok(match reader.source() {
        ReaderSource::File(_) => match reader.try_clone_file() {
            Some(r) => InPlan::File(r?),
            None => InPlan::Null,
        },
        ReaderSource::Stdin => InPlan::Inherit,
        ReaderSource::Channel(_) => InPlan::Reader(reader.clone()),
    })
}

/// A piped output being drained.
struct Drain {
    pipe: PipeReader,
    sink: Sink,
    capture: Option<SharedBuf>,
}

/// Turn an output plan into the child's stdio (and a drain for pipes).
fn materialize_out(plan: OutPlan) -> io::Result<(Stdio, Option<Drain>, Option<io::PipeWriter>)> {
    Ok(match plan {
        OutPlan::File(f) => (Stdio::from(f), None, None),
        OutPlan::Inherit => (Stdio::inherit(), None, None),
        OutPlan::Null => (Stdio::null(), None, None),
        OutPlan::Pipe { sink, capture } => {
            let (r, w) = io::pipe()?;
            let for_dup = w.try_clone()?;
            (
                Stdio::from(w),
                Some(Drain {
                    pipe: PipeReader::new(r)?,
                    sink,
                    capture,
                }),
                Some(for_dup),
            )
        }
    })
}

/// The stdio of the dup source (`from`) given the target's plan.
fn dup_stdio(
    target: &OutPlan,
    target_which: Which,
    shared: Option<io::PipeWriter>,
) -> io::Result<Stdio> {
    Ok(match target {
        OutPlan::Pipe { .. } => match shared {
            Some(w) => Stdio::from(w),
            None => Stdio::null(),
        },
        OutPlan::File(f) => Stdio::from(f.try_clone()?),
        OutPlan::Inherit => match dup_process_stdio(target_which) {
            Ok(f) => Stdio::from(f),
            Err(_) => Stdio::inherit(),
        },
        OutPlan::Null => Stdio::null(),
    })
}

// ---------------------------------------------------------------------------
// Pipe reading
// ---------------------------------------------------------------------------

/// The read end of an output pipe, read asynchronously. Dropping it closes
/// the pipe (a child writing to it then gets SIGPIPE / EPIPE).
enum PipeReader {
    #[cfg(unix)]
    Async(tokio::net::unix::pipe::Receiver),
    /// Read by a helper thread (Windows; also used on unix when the pipe
    /// cannot be registered with the reactor).
    Thread(tokio::sync::mpsc::Receiver<io::Result<Vec<u8>>>),
}

impl PipeReader {
    fn new(r: io::PipeReader) -> io::Result<Self> {
        #[cfg(unix)]
        let reader = {
            use std::os::fd::OwnedFd;
            let fd = OwnedFd::from(r);
            match tokio::net::unix::pipe::Receiver::from_owned_fd(fd.try_clone()?) {
                Ok(rx) => Ok(PipeReader::Async(rx)),
                Err(_) => Self::threaded(File::from(fd)),
            }
        };
        #[cfg(not(unix))]
        let reader = Self::threaded(r);
        reader
    }

    fn threaded(mut r: impl io::Read + Send + 'static) -> io::Result<Self> {
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        std::thread::Builder::new()
            .name("bun-shell-pipe".into())
            .spawn(move || loop {
                let mut buf = vec![0u8; super::io::READ_CHUNK];
                let res = match r.read(&mut buf) {
                    Ok(0) => return,
                    Ok(n) => {
                        buf.truncate(n);
                        Ok(buf)
                    }
                    Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
                    Err(e) => Err(e),
                };
                let failed = res.is_err();
                if tx.blocking_send(res).is_err() || failed {
                    return;
                }
            })?;
        Ok(PipeReader::Thread(rx))
    }

    /// The next chunk; `None` at EOF (a read error also ends the stream,
    /// Windows reports the end of a pipe as a broken pipe).
    async fn next(&mut self) -> Option<Vec<u8>> {
        match self {
            #[cfg(unix)]
            PipeReader::Async(rx) => {
                use tokio::io::AsyncReadExt;
                let mut buf = vec![0u8; super::io::READ_CHUNK];
                loop {
                    match rx.read(&mut buf).await {
                        Ok(0) => return None,
                        Ok(n) => {
                            buf.truncate(n);
                            return Some(buf);
                        }
                        Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                        Err(_) => return None,
                    }
                }
            }
            PipeReader::Thread(rx) => rx.recv().await.and_then(Result::ok),
        }
    }
}

/// Drain one output pipe into its sink until EOF.
async fn drain(mut d: Drain) -> Drain {
    while let Some(chunk) = d.pipe.next().await {
        match &mut d.sink {
            Sink::Buffer(b) => b.extend_from_slice(&chunk),
            Sink::View { buf, pos } => buf.with(|view| {
                if *pos < view.len() {
                    let n = (view.len() - *pos).min(chunk.len());
                    view[*pos..*pos + n].copy_from_slice(&chunk[..n]);
                    *pos += n;
                }
            }),
            Sink::Tee { writer, buf, err } => {
                buf.extend_from_slice(&chunk);
                if let Err(e) = writer.write(&chunk, None).await {
                    err.get_or_insert(e);
                }
            }
            Sink::Writer(w) => {
                if w.write(&chunk, None).await.is_err() {
                    // The reader is gone: close our end so the child's next
                    // write fails with SIGPIPE, like a real pipeline.
                    d.pipe = PipeReader::Thread(tokio::sync::mpsc::channel(1).1);
                    break;
                }
            }
        }
    }
    d
}

/// Feed the child's stdin; returns when the input is exhausted (the pipe is
/// then closed) or the child stopped reading.
async fn feed_stdin(stdin: Option<ChildStdin>, plan: Option<InPlan>) {
    let Some(mut stdin) = stdin else {
        return;
    };
    match plan {
        Some(InPlan::Bytes(bytes)) => {
            let _ = stdin.write_all(&bytes).await;
        }
        Some(InPlan::Reader(reader)) => {
            while let Ok(Some(chunk)) = reader.read_chunk().await {
                if stdin.write_all(&chunk).await.is_err() {
                    return;
                }
            }
        }
        _ => {}
    }
    let _ = stdin.shutdown().await;
}

// ---------------------------------------------------------------------------
// Spawning
// ---------------------------------------------------------------------------

/// Whether JavaScript's `\s` matches `c`.
#[cfg_attr(not(any(windows, test)), allow(dead_code))]
fn is_js_space(c: char) -> bool {
    matches!(
        c,
        '\t' | '\n' | '\u{0b}' | '\u{0c}' | '\r' | ' ' | '\u{a0}' | '\u{1680}' | '\u{2000}'
            ..='\u{200a}'
                | '\u{2028}'
                | '\u{2029}'
                | '\u{202f}'
                | '\u{205f}'
                | '\u{3000}'
                | '\u{feff}'
    )
}

/// Quote an argument for a `cmd.exe /s /c "..."` line (the JS
/// `quoteWindowsArg`).
#[cfg_attr(not(any(windows, test)), allow(dead_code))]
pub(crate) fn quote_windows_arg(arg: &str) -> String {
    if !arg.is_empty() && !arg.chars().any(|c| is_js_space(c) || c == '"') {
        return arg.to_string();
    }
    let mut out = String::from("\"");
    let mut backslashes = 0;
    for c in arg.chars() {
        match c {
            '\\' => {
                backslashes += 1;
                continue;
            }
            '"' => {
                out.push_str(&"\\".repeat(backslashes * 2));
                out.push_str("\\\"");
            }
            _ => {
                out.push_str(&"\\".repeat(backslashes));
                out.push(c);
            }
        }
        backslashes = 0;
    }
    out.push_str(&"\\".repeat(backslashes * 2));
    out.push('"');
    out
}

/// Whether `argv0` is a batch file (run through `cmd.exe` on Windows).
pub(crate) fn is_batch_file(argv0: &str) -> bool {
    let lower = argv0.to_ascii_lowercase();
    lower.ends_with(".bat") || lower.ends_with(".cmd")
}

/// The `cmd.exe` line for a batch file: `/d /s /c "<quoted argv>"`.
#[cfg_attr(not(any(windows, test)), allow(dead_code))]
pub(crate) fn batch_command_line(args: &[String]) -> String {
    let line: Vec<String> = args.iter().map(|a| quote_windows_arg(a)).collect();
    format!("\"{}\"", line.join(" "))
}

fn build_command(args: &[String]) -> Command {
    #[cfg(windows)]
    {
        if is_batch_file(&args[0]) {
            let comspec = std::env::var("ComSpec")
                .ok()
                .filter(|s| !s.is_empty())
                .unwrap_or_else(|| "cmd.exe".to_string());
            let mut cmd = Command::new(comspec);
            cmd.raw_arg("/d")
                .raw_arg("/s")
                .raw_arg("/c")
                .raw_arg(batch_command_line(args));
            cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
            return cmd;
        }
        let mut cmd = Command::new(&args[0]);
        cmd.args(&args[1..]);
        cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
        cmd
    }
    #[cfg(not(windows))]
    {
        let mut cmd = Command::new(&args[0]);
        cmd.args(&args[1..]);
        cmd
    }
}

fn exit_code_of(status: io::Result<std::process::ExitStatus>) -> i32 {
    let Ok(status) = status else {
        return 1;
    };
    if let Some(code) = status.code() {
        return code;
    }
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(sig) = status.signal() {
            return 128 + sig;
        }
    }
    1
}

/// Spawn `opts.args` and wait until it has exited and its output pipes are
/// closed, then append the captured output (stdout first) to its buffers.
pub(crate) async fn run_subprocess(opts: SubprocessOptions<'_>) -> SubprocessResult {
    let SubprocessOptions {
        args,
        cwd,
        env,
        io,
        flags,
        overrides,
        dup,
        buffered_stdout,
        buffered_stderr,
    } = opts;
    let argv0 = args.first().cloned().unwrap_or_default();
    let spawn_err =
        |e: &io::Error| SubprocessResult::SpawnError(ShellSysError::from_io(e, argv0.clone()));
    if args.is_empty() {
        return spawn_err(&io::Error::from(io::ErrorKind::NotFound));
    }

    let Overrides {
        stdin: ov_in,
        stdout: ov_out,
        stderr: ov_err,
    } = overrides;
    let plans = (|| -> io::Result<_> {
        Ok((
            plan_in(&io.stdin, ov_in)?,
            plan_out(
                &io.stdout,
                ov_out,
                &buffered_stdout,
                redirects_elsewhere(flags, Which::Stdout),
            )?,
            plan_out(
                &io.stderr,
                ov_err,
                &buffered_stderr,
                redirects_elsewhere(flags, Which::Stderr),
            )?,
        ))
    })();
    let (in_plan, out_plan, err_plan) = match plans {
        Ok(p) => p,
        Err(e) => return spawn_err(&e),
    };

    let mut cmd = build_command(&args);
    cmd.current_dir(cwd)
        .env_clear()
        .envs(env)
        .kill_on_drop(false);

    let stdin_plan = match in_plan {
        InPlan::File(f) => {
            cmd.stdin(Stdio::from(f));
            None
        }
        InPlan::Inherit => {
            cmd.stdin(Stdio::inherit());
            None
        }
        InPlan::Null => {
            cmd.stdin(Stdio::null());
            None
        }
        p @ (InPlan::Bytes(_) | InPlan::Reader(_)) => {
            cmd.stdin(Stdio::piped());
            Some(p)
        }
    };

    let outputs = (|| -> io::Result<_> {
        Ok(match dup {
            None => {
                let (out, d_out, _) = materialize_out(out_plan)?;
                let (err, d_err, _) = materialize_out(err_plan)?;
                (out, err, d_out, d_err)
            }
            // 2>&1: stderr joins stdout.
            Some(Dup::StderrToStdout) => {
                let err = dup_stdio(&out_plan, Which::Stdout, None)?;
                let (out, d_out, shared) = materialize_out(out_plan)?;
                let err = shared.map_or(err, Stdio::from);
                (out, err, d_out, None)
            }
            // 1>&2: stdout joins stderr.
            Some(Dup::StdoutToStderr) => {
                let out = dup_stdio(&err_plan, Which::Stderr, None)?;
                let (err, d_err, shared) = materialize_out(err_plan)?;
                let out = shared.map_or(out, Stdio::from);
                (out, err, None, d_err)
            }
        })
    })();
    let (out_stdio, err_stdio, d_out, d_err) = match outputs {
        Ok(o) => o,
        Err(e) => return spawn_err(&e),
    };
    cmd.stdout(out_stdio).stderr(err_stdio);

    let spawned = cmd.spawn();
    // Drop our copies of the child's pipe ends so EOF arrives when it exits.
    drop(cmd);
    let mut child = match spawned {
        Ok(c) => c,
        Err(e) => return spawn_err(&e),
    };

    let stdin = child.stdin.take();
    let wait = async {
        let feed = feed_stdin(stdin, stdin_plan);
        tokio::pin!(feed);
        tokio::select! {
            status = child.wait() => status,
            () = &mut feed => child.wait().await,
        }
    };
    let drain_opt = |d: Option<Drain>| async move {
        match d {
            Some(d) => Some(drain(d).await),
            None => None,
        }
    };
    let (status, d_out, d_err) = tokio::join!(wait, drain_opt(d_out), drain_opt(d_err));

    let mut exit_code = exit_code_of(status);
    for d in [d_out, d_err].into_iter().flatten() {
        if let Some(e) = d.sink.err() {
            exit_code = e.errno;
        }
        if let (Some(capture), Some(bytes)) = (&d.capture, d.sink.slice()) {
            capture.append(&bytes);
        }
    }
    SubprocessResult::Exited(exit_code)
}

#[cfg(test)]
mod tests;