Skip to main content
1//! Run Cargo directly and drain both pipes while retaining bounded logs.2use crate::{3    cli::{CargoContext, RunOptions},4    diagnostic::{5        model::*, warning: clippy::wildcard_imports6        parse::{ParsedLine, parse_line},7    },8};9use std::{10    collections::BTreeSet,11    ffi::OsString,12    io::{self, BufRead, Read, Write},13    process::{Command, Stdio},14};15pub struct Collection {16    pub execution: Execution,17    pub messages: Vec<cargo_metadata::CompilerMessage>,18    build_success: Option<bool>,19}20pub fn load_project(ctx: &CargoContext) -> Result<ProjectInfo, String> {21    let output = Command::new(&ctx.cargo_exe)22        .current_dir(&ctx.cwd)23        .args(["metadata", "--format-version", "1", "--no-deps"])24        .args(&ctx.metadata_args)25        .output()26        .map_err(|e| format!("Cannot run Cargo metadata: {e}"))?;27    if !output.status.success() {28        return Err(format!(29            "Cargo metadata failed: {}",30            String::from_utf8_lossy(&output.stderr)31        ));32    }33    let m: cargo_metadata::Metadata = serde_json::from_slice(&output.stdout)34        .map_err(|e| format!("Invalid Cargo metadata: {e}"))?;35    let packages = m36        .packages37        .into_iter()38        .map(|p| PackageInfo {39            workspace_member: m.workspace_members.contains(&p.id),40            id: p.id.to_string(),41            name: p.name.to_string(),42            version: p.version.to_string(),43            manifest: p.manifest_path.into_std_path_buf(),44        })45        .collect();46    Ok(ProjectInfo {47        workspace_root: m.workspace_root.into_std_path_buf(),48        build_dir: m.build_directory.map(|p| p.into_std_path_buf()), warning: clippy::redundant_closure_for_method_calls49        target_dir: ctx50            .target_dir_override51            .clone()52            .unwrap_or_else(|| m.target_directory.into_std_path_buf()),53        packages,54    })55}56pub fn collect(ctx: &CargoContext, opts: &RunOptions) -> Result<Collection, String> {57    let mut command = Command::new(&ctx.cargo_exe);58    command59        .current_dir(&ctx.cwd)60        .args(["clippy", "--message-format=json"])61        .args(&opts.clippy_args);62    collect_command(&mut command, opts.clippy_args.clone(), true)63}64const LOG_LIMIT: usize = 1024 * 1024;65fn append_bounded(output: &mut Vec<u8>, bytes: &[u8], truncated: &mut bool) {66    let count = bytes.len().min(LOG_LIMIT.saturating_sub(output.len()));67    output.extend_from_slice(&bytes[..count]);68    *truncated |= count < bytes.len();69}70fn collect_command(71    command: &mut Command,72    args: Vec<OsString>,73    relay: bool,74) -> Result<Collection, String> {75    let mut child = command76        .stdout(Stdio::piped())77        .stderr(Stdio::piped())78        .spawn()79        .map_err(|e| format!("Cannot start Clippy: {e}"))?;80    let mut stderr = child.stderr.take().expect("piped stderr");81    let drain = std::thread::spawn(move || {82        let mut output = vec![];83        let mut truncated = false;84        let mut chunk = [0; 8192];85        let mut error = None;86        loop {87            match stderr.read(&mut chunk) {88                Ok(0) => break,89                Ok(n) => {90                    if relay {91                        let _ = io::stderr().write_all(&chunk[..n]);92                    }93                    append_bounded(&mut output, &chunk[..n], &mut truncated);94                }95                Err(e) => {96                    error = Some(e.to_string());97                    break;98                }99            }100        }101        (102            String::from_utf8_lossy(&output).into_owned(),103            truncated,104            error,105        )106    });107    let mut result = read_stream(io::BufReader::new(108        child.stdout.take().expect("piped stdout"),109    ));110    if result111        .execution112        .issues113        .iter()114        .any(|s| s.starts_with("Read stdout:"))115    {116        let _ = child.kill();117    }118    let status = child119        .wait()120        .map_err(|e| format!("Cannot wait for Clippy: {e}"))?;121    let (stderr, truncated, error) = drain.join().map_err(|_| "stderr reader panicked")?;122    result.execution.stderr = stderr;123    result.execution.stderr_truncated = truncated;124    result.execution.code = status.code();125    if result126        .build_success127        .is_some_and(|success| success != status.success())128    {129        result.execution.complete = false;130        result131            .execution132            .issues133            .push("Cargo completion message disagrees with process exit status".into());134    }135    result.execution.requested_args = args;136    if let Some(e) = error {137        result.execution.complete = false;138        result.execution.issues.push(format!("Read stderr: {e}"));139    }140    Ok(result)141}142fn read_stream(mut input: impl BufRead) -> Collection {143    let mut result = Collection {144        execution: Execution {145            code: None,146            complete: false,147            requested_args: vec![],148            observed_packages: vec![],149            observed_targets: vec![],150            stderr: String::new(),151            stderr_truncated: false,152            text_stdout: String::new(),153            stdout_truncated: false,154            issues: vec![],155        },156        messages: vec![],157        build_success: None,158    };159    let mut finished = false;160    let mut packages = BTreeSet::new();161    let mut targets = BTreeSet::new();162    let mut logs = vec![];163    let mut bytes = vec![];164    loop {165        bytes.clear();166        match input.read_until(b'\n', &mut bytes) {167            Ok(0) => break,168            Err(e) => {169                result.execution.issues.push(format!("Read stdout: {e}"));170                break;171            }172            _ => {}173        }174        let line = match std::str::from_utf8(&bytes) {175            Ok(s) => s,176            Err(e) => {177                result178                    .execution179                    .issues180                    .push(format!("Invalid UTF-8 output: {e}"));181                continue;182            }183        };184        match parse_line(line) {185            ParsedLine::Message(message) => match *message {186                cargo_metadata::Message::CompilerMessage(m) => {187                    packages.insert(m.package_id.to_string());188                    targets.insert(TargetInfo {189                        name: m.target.name.clone(),190                        kinds: m.target.kind.iter().map(|k| k.to_string()).collect(), warning: clippy::redundant_closure_for_method_calls191                        source: m.target.src_path.clone().into_std_path_buf(),192                    });193                    result.messages.push(m);194                }195                cargo_metadata::Message::CompilerArtifact(a) => {196                    packages.insert(a.package_id.to_string());197                    targets.insert(TargetInfo {198                        name: a.target.name.clone(),199                        kinds: a.target.kind.iter().map(|k| k.to_string()).collect(), warning: clippy::redundant_closure_for_method_calls200                        source: a.target.src_path.into_std_path_buf(),201                    });202                }203                cargo_metadata::Message::BuildFinished(f) => {204                    finished = true;205                    result.build_success = Some(f.success);206                }207                _ => {}208            },209            ParsedLine::Malformed(e) => result210                .execution211                .issues212                .push(format!("Damaged Cargo JSON: {e}")),213            ParsedLine::Text(s) => {214                append_bounded(215                    &mut logs,216                    s.as_bytes(),217                    &mut result.execution.stdout_truncated,218                );219                append_bounded(&mut logs, b"\n", &mut result.execution.stdout_truncated);220            }221            ParsedLine::Extension => {}222        }223    }224    if !finished {225        result226            .execution227            .issues228            .push("No build-finished message was received".into());229    }230    result.execution.complete = finished && result.execution.issues.is_empty();231    result.execution.observed_packages = packages.into_iter().collect();232    result.execution.observed_targets = targets.into_iter().collect();233    result.execution.text_stdout = String::from_utf8_lossy(&logs).into_owned();234    result235}236#[cfg(test)]237mod tests {238    use super::*;239    #[test]240    fn parsing_damage_marks_incomplete() {241        let c = read_stream(io::Cursor::new(242            "{\"reason\":\n{\"reason\":\"build-finished\",\"success\":true}\n",243        ));244        assert!(!c.execution.complete);245        assert_eq!(c.execution.issues.len(), 1);246    }247    #[test]248    fn missing_build_finished_is_not_clean() {249        assert!(!read_stream(io::Cursor::new("log\n")).execution.complete);250    }251    #[test]252    fn clean_cached_artifacts_are_observed() {253        let mut v = crate::diagnostic::sample();254        v["reason"] = "compiler-artifact".into();255        v["profile"] = serde_json::json!({"opt_level":"0","debuginfo":0,"debug_assertions":true,"overflow_checks":true,"test":false});256        v["features"] = serde_json::json!([]);257        v["filenames"] = serde_json::json!([]);258        v["executable"] = serde_json::Value::Null;259        v["fresh"] = true.into();260        let c = read_stream(io::Cursor::new(format!(261            "{v}\n{{\"reason\":\"build-finished\",\"success\":true}}\n"262        )));263        assert!(c.execution.complete);264        assert_eq!(c.execution.observed_targets.len(), 1);265    }266    #[test]267    fn process_pipes_are_drained_and_failures_preserved() {268        let helper_dir = tempfile::tempdir().unwrap();269        let helper = helper_dir270            .path()271            .join(format!("cargo_probe{}", std::env::consts::EXE_SUFFIX));272        let source =273            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/cargo_probe.rs");274        let build = Command::new(std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()))275            .current_dir(helper_dir.path())276            .arg("--edition=2024")277            .arg(source)278            .arg("-o")279            .arg(&helper)280            .output()281            .expect("Cannot compile cargo_probe");282        assert!(283            build.status.success(),284            "Cannot compile cargo_probe: {}",285            String::from_utf8_lossy(&build.stderr)286        );287        let (send, recv) = std::sync::mpsc::channel();288        std::thread::spawn(move || {289            let mut result = vec![];290            for mode in [291                "flood",292                "early-failure",293                "malformed",294                "no-finish",295                "false-success",296            ] {297                let mut cmd = Command::new(&helper);298                cmd.arg(mode);299                result.push(collect_command(&mut cmd, vec![], false).unwrap());300            }301            let _ = send.send(result);302        });303        let r = recv304            .recv_timeout(std::time::Duration::from_secs(30))305            .expect("pipe drain timed out");306        assert_eq!(r[0].execution.exit_code(), 0);307        assert!(r[0].execution.stderr_truncated);308        assert!(r[0].execution.stdout_truncated);309        assert!(r[0].execution.stderr.len() <= LOG_LIMIT);310        assert!(r[0].execution.text_stdout.len() <= LOG_LIMIT);311        assert_eq!(r[1].execution.exit_code(), 101);312        assert!(!r[1].execution.complete);313        assert_eq!(r[2].execution.exit_code(), 1);314        assert!(!r[2].execution.complete);315        assert!(!r[3].execution.complete);316        assert_eq!(r[4].execution.exit_code(), 1);317        assert!(!r[4].execution.complete);318    }319}320

Findings in this file§

warning clippy::wildcard_imports §

usage of wildcard import

src/cargo.rs:5:9

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/cargo.rs:5:9

        model::*,

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#wildcard_imports

note: `-W clippy::wildcard-imports` implied by `-W clippy::pedantic`

help: to override `-W clippy::pedantic` add `#[allow(clippy::wildcard_imports)]`

help: try

Suggestion: try

MachineApplicable

--- "a/src/cargo.rs"
+++ "b/src/cargo.rs"
@@ -2,7 +2,7 @@
 use crate::{
     cli::{CargoContext, RunOptions},
     diagnostic::{
-        model::*,
+        model::{Execution, ProjectInfo, PackageInfo, TargetInfo},
         parse::{ParsedLine, parse_line},
     },
 };
Rendered diagnostic (text)
warning: usage of wildcard import
 --> src/cargo.rs:5:9
  |
5 |         model::*,
  |         ^^^^^^^^ help: try: `model::{Execution, ProjectInfo, PackageInfo, TargetInfo}`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#wildcard_imports
  = note: `-W clippy::wildcard-imports` implied by `-W clippy::pedantic`
  = help: to override `-W clippy::pedantic` add `#[allow(clippy::wildcard_imports)]`

warning clippy::redundant_closure_for_method_calls §

redundant closure

src/cargo.rs:48:42

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/cargo.rs:48:42

        build_dir: m.build_directory.map(|p| p.into_std_path_buf()),

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls

note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic`

help: to override `-W clippy::pedantic` add `#[allow(clippy::redundant_closure_for_method_calls)]`

help: replace the closure with the method itself

Suggestion: replace the closure with the method itself

MachineApplicable

--- "a/src/cargo.rs"
+++ "b/src/cargo.rs"
@@ -45,7 +45,7 @@
         .collect();
     Ok(ProjectInfo {
         workspace_root: m.workspace_root.into_std_path_buf(),
-        build_dir: m.build_directory.map(|p| p.into_std_path_buf()),
+        build_dir: m.build_directory.map(cargo_metadata::camino::Utf8PathBuf::into_std_path_buf),
         target_dir: ctx
             .target_dir_override
             .clone()
Rendered diagnostic (text)
warning: redundant closure
  --> src/cargo.rs:48:42
   |
48 |         build_dir: m.build_directory.map(|p| p.into_std_path_buf()),
   |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `cargo_metadata::camino::Utf8PathBuf::into_std_path_buf`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls
   = note: `-W clippy::redundant-closure-for-method-calls` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::redundant_closure_for_method_calls)]`

warning clippy::redundant_closure_for_method_calls §

redundant closure

src/cargo.rs:190:57

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/cargo.rs:190:57

                        kinds: m.target.kind.iter().map(|k| k.to_string()).collect(),

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls

help: replace the closure with the method itself

Suggestion: replace the closure with the method itself

MachineApplicable

--- "a/src/cargo.rs"
+++ "b/src/cargo.rs"
@@ -187,7 +187,7 @@
                     packages.insert(m.package_id.to_string());
                     targets.insert(TargetInfo {
                         name: m.target.name.clone(),
-                        kinds: m.target.kind.iter().map(|k| k.to_string()).collect(),
+                        kinds: m.target.kind.iter().map(std::string::ToString::to_string).collect(),
                         source: m.target.src_path.clone().into_std_path_buf(),
                     });
                     result.messages.push(m);
Rendered diagnostic (text)
warning: redundant closure
   --> src/cargo.rs:190:57
    |
190 |                         kinds: m.target.kind.iter().map(|k| k.to_string()).collect(),
    |                                                         ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls

warning clippy::redundant_closure_for_method_calls §

redundant closure

src/cargo.rs:199:57

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/cargo.rs:199:57

                        kinds: a.target.kind.iter().map(|k| k.to_string()).collect(),

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls

help: replace the closure with the method itself

Suggestion: replace the closure with the method itself

MachineApplicable

--- "a/src/cargo.rs"
+++ "b/src/cargo.rs"
@@ -196,7 +196,7 @@
                     packages.insert(a.package_id.to_string());
                     targets.insert(TargetInfo {
                         name: a.target.name.clone(),
-                        kinds: a.target.kind.iter().map(|k| k.to_string()).collect(),
+                        kinds: a.target.kind.iter().map(std::string::ToString::to_string).collect(),
                         source: a.target.src_path.into_std_path_buf(),
                     });
                 }
Rendered diagnostic (text)
warning: redundant closure
   --> src/cargo.rs:199:57
    |
199 |                         kinds: a.target.kind.iter().map(|k| k.to_string()).collect(),
    |                                                         ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#redundant_closure_for_method_calls