BoundaryML/baml · error

cargo metadata failed

Error message

cargo metadata failed

What it means

find_workspace_root runs `cargo metadata --no-deps --format-version 1` to locate the workspace root; if cargo exits non-zero it bails with this generic message. It means cargo itself rejected the invocation — usually because the current directory is not inside a valid Cargo workspace or the manifest is broken.

Source

Thrown at baml_language/crates/tools_size_gate/src/main.rs:197

    };

    match result {
        Ok(code) => std::process::exit(code),
        Err(e) => {
            eprintln!("error: {e:#}");
            std::process::exit(EXIT_TOOL_ERROR);
        }
    }
}

/// Find the workspace root via cargo metadata.
fn find_workspace_root() -> Result<PathBuf> {
    let output = std::process::Command::new("cargo")
        .args(["metadata", "--no-deps", "--format-version", "1"])
        .output()
        .context("failed to run cargo metadata")?;
    if !output.status.success() {
        anyhow::bail!("cargo metadata failed");
    }
    let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;
    let root = json["workspace_root"]
        .as_str()
        .context("no workspace_root in metadata")?;
    Ok(PathBuf::from(root))
}

/// Determine which artifact names are relevant for the current host.
/// Returns a map: platform -> artifact names.
fn relevant_artifacts(config: &Config, filter: Option<&[String]>) -> BTreeMap<String, Vec<String>> {
    let host = host_triple();
    let mut groups: BTreeMap<String, Vec<String>> = BTreeMap::new();

    for (name, artifact) in &config.artifacts {
        if let Some(only) = filter {
            if !only.iter().any(|o| o == name) {
                continue;

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Run size-gate from the repository/workspace root (or a subdirectory of it)
  2. Run `cargo metadata --no-deps --format-version 1` manually to see cargo's real error
  3. Fix the invalid Cargo.toml that cargo reports
  4. Ensure a working Rust toolchain is installed (rustup update stable)

Example fix

// before: wrong cwd
$ cd /tmp && size-gate check
cargo metadata failed
// after
$ cd baml_language && size-gate check
Defensive patterns

Strategy: validation

Validate before calling

let ok = std::process::Command::new("cargo")
    .args(["metadata", "--no-deps", "--format-version", "1"])
    .output()
    .map(|o| o.status.success())
    .unwrap_or(false);
if !ok { eprintln!("not in a valid cargo workspace; run from the repo root"); std::process::exit(1); }

Prevention

When it happens

Trigger: Running size-gate (record/check/bake/diff) from a directory outside a Cargo workspace, or with a corrupt/invalid Cargo.toml, or a broken Rust toolchain.

Common situations: Invoking the tool from a random directory instead of the repo root; malformed workspace members in Cargo.toml; rustup toolchain missing; cargo not on PATH (that instead yields the 'failed to run cargo metadata' context error).

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/7c889c22fca9a556. Report an issue: GitHub.