Cargo Crashes with Consecutive Cleaning & Compiling


Hi all,

I’ve been trying to use cargo programmatically to clean the project in the current directory without removing build artefacts of dependencies, then compile it. Unfortunately, it looks like I can’t do both at the same time:

extern crate cargo;

use cargo::ops::{CompileOptions, CleanOptions, Packages, CompileFilter, MessageFormat, CompileMode};
use cargo::core::Workspace;
use cargo::core::shell::Verbosity;
use cargo::util::config::Config;
use std::env::current_dir;

fn main() {
    let rustc_opts = {
        let mut opts = vec!("--emit", "asm", "-C", "debuginfo=2");
    }.iter().map(|s| (*s).into()).collect::<Vec<String>>();

    let conf = {
        let mut conf = Config::default().unwrap();;

    let ws = Workspace::new(&current_dir().unwrap().join("Cargo.toml"), &conf).unwrap();

    cargo::ops::clean(&ws, &CleanOptions {
        spec: &["NAME OF PROJECT FROM CARGO.TOML HERE".into()],
        target: None,
        config: &conf,
        release: false

    let compilation = cargo::ops::compile(&ws, &CompileOptions {
        config: &conf,
        jobs: None,
        target: None,
        features: &[],
        all_features: false,
        no_default_features: false,
        spec: Packages::All,
        release: false,
        filter: CompileFilter::Default { required_features_filterable: true },
        mode: CompileMode::Build,
        message_format: MessageFormat::Human,
        target_rustdoc_args: None,
        target_rustc_args: Some(&rustc_opts)
    }).expect("it crashes here");

Cleaning the project without compiling it works, as does compiling without cleaning. The error is

'couldn't get the path to cargo executable', Os { code: 2, kind: NotFound, message: "No such file or directory" }

I’ve tried a few permutations of settings, workspaces, etc, but there’s a lot of surface area to cover. It also fails if I try to clean the entire project as well (spec: &[] in CleanOptions). Am I missing something here?