4

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?

2
  • 5
    Like most compilers, cargo clippy only outputs errors and warnings on stderr, not stdout. Commented Feb 22, 2021 at 16:36
  • If your project involves parsing the output of clippy, note that you can configure it to output diagnostics in machine-readable JSON instead Commented Feb 22, 2021 at 20:33

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. I was so focused on stdout that I forgot about stderr. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.