Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No output from std::process::Command

I wanted to run clippy using process::Command but it doesn't seem to be working.

I ran cargo build on this:

use std::env;
use std::process::Command;
use std::io;
use std::io::Write;

fn main() {
    let pwd = env::current_dir();
    match pwd {
        Ok(data) => {
            println!("{}", &data.display());
            let output = Command::new("cargo")
            .arg("clippy")
            .output()
            .expect("there was an error");

            io::stdout().write_all(&output.stdout).unwrap();
        },
        Err(_) => (),
    }
}

than executed the binary in the root of another rust project. But I don't seem to be getting any output. I've tried replacing cargo clippy with ls and that ran normally. Properly listing all files in that directory.

Any ideas?

like image 905
someone_else Avatar asked Nov 07 '25 08:11

someone_else


1 Answers

Like mcarton pointed out cargo clippy does not print anything to stdout. Only to stderr. So adding the output of stderr prints more text:

use std::env;
use std::process::Command;
use std::io;
use std::io::Write;

fn main() {
    let pwd = env::current_dir();
    match pwd {
        Ok(data) => {
            println!("{}", &data.display());
            let output = Command::new("cargo")
                .arg("clippy")
                .output()
                .expect("there was an error");

            io::stdout().write_all(&output.stdout).unwrap();
            // add this line to print stderr output too
            io::stderr().write_all(&output.stderr).unwrap();
        },
        Err(_) => (),
    }
}

In the example above we wrote the output of stderr of output to our application stderr.

output.stderr -> app.stderr:

io::stderr().write_all(&output.stderr).unwrap();

If we want to write it to stdout instead we can use.

output.stderr -> app.stdout:

io::stdout().write_all(&output.stderr).unwrap();
like image 193
Ralph Bisschops Avatar answered Nov 11 '25 06:11

Ralph Bisschops



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!