为什么我不能在 Rust 中将 `Stdio::piped()` 与 windows `cmd.exe` 一起使用?

12t*_*all 5 windows cmd process rust

当我尝试cmd.exe通过以下代码开始时:

use std::process::{Command, Stdio};
use std::io::{BufRead, Write, BufReader};

fn main() {
    let mut child_cmd = Command::new("cmd.exe")  
        // seems it work well with `/bin/bash` in Linux
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())   // Error emitted here. 
        .spawn()
        .unwrap();   
    // do sth. else
}
Run Code Online (Sandbox Code Playgroud)

我想将输出重定向到管道,但它总是报告The process tried to write to a nonexistent pipe;当我删除时.stdout(Stdio::piped()),没有抛出错误。为什么会发生这种情况?

IIn*_*ble 5

EvilTak 的评论很到位。当您将 STDOUT 重定向到管道时,您还必须将 STDERR 重定向到管道。以下代码不再出错:

use std::process::{Command, Stdio};

fn main() {
    let mut child_cmd = Command::new("cmd.exe")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())  // Required when redirecting stdout
        .spawn()
        .unwrap();
    // do sth. else
}
Run Code Online (Sandbox Code Playgroud)

虽然这解决了眼前的问题,但我不确定到底是什么原因造成的。查看CreateProcessW函数及其接受的STARTUPINFOW结构,看起来标准 I/O 重定向是一个由标志指定的全有或全无选项STARTF_USESTDHANDLES

Python 表现出相同行为的事实表明,这实际上是 Windows API 或cmd.exe实现的特性。

不管怎样,我还没有进行任何广泛的研究来弄清楚这里到底发生了什么。