BoundaryML/baml · error

cargo metadata failed

Error message

cargo metadata failed

What it means

resolve_lib_name shells out to `cargo metadata --no-deps --format-version 1` from the workspace root. If cargo exits with a non-zero status, the tool bails with this short message. It indicates cargo itself rejected the invocation or the workspace, not that JSON parsing failed.

Source

Thrown at baml_language/crates/tools_size_gate/src/measure.rs:243

/// Append the platform executable extension (`.exe` on Windows).
fn exe_filename(stem: &str) -> String {
    if cfg!(target_os = "windows") {
        format!("{stem}.exe")
    } else {
        stem.to_owned()
    }
}

/// Resolve the lib name for a package using cargo metadata.
fn resolve_lib_name(workspace_root: &Path, package_name: &str) -> Result<String> {
    let output = Command::new("cargo")
        .args(["metadata", "--no-deps", "--format-version", "1"])
        .current_dir(workspace_root)
        .output()
        .context("failed to run cargo metadata")?;

    if !output.status.success() {
        bail!("cargo metadata failed");
    }

    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).context("failed to parse cargo metadata")?;

    let packages = json["packages"]
        .as_array()
        .context("no packages in metadata")?;

    for pkg in packages {
        if pkg["name"].as_str() == Some(package_name) {
            if let Some(targets) = pkg["targets"].as_array() {
                for target in targets {
                    let kinds = target["kind"].as_array();
                    let is_cdylib = kinds
                        .map(|k| k.iter().any(|v| v.as_str() == Some("cdylib")))
                        .unwrap_or(false);
                    if is_cdylib {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Run `cargo metadata --no-deps` manually in the workspace root to see the real cargo error.
  2. Fix the underlying Cargo.toml/lockfile error cargo reports.
  3. Ensure workspace_root points to the actual workspace containing Cargo.toml.
  4. Verify the cargo toolchain (rustup update / correct PATH) used by the gate matches your shell.

Example fix

// diagnose before running gate\ncd <workspace_root> && cargo metadata --no-deps --format-version 1\n// fix whatever cargo reports, then re-run the size gate
Defensive patterns

Strategy: try-catch

Validate before calling

let probe = Command::new("cargo").args(["metadata","--no-deps","--format-version","1"]).current_dir(workspace_root).output()?;\nif !probe.status.success() { eprintln!("cargo metadata failed: {}", String::from_utf8_lossy(&probe.stderr)); }

Try / catch

match load_result {\n    Err(e) if e.to_string().contains("cargo metadata failed") => {\n        eprintln!("Run `cargo metadata --no-deps` in the workspace root to see the real error.");\n        std::process::exit(1);\n    }\n    other => other?,\n}

Prevention

When it happens

Trigger: locate_artifact needs to map a package name to its library name and invokes cargo metadata in a workspace where the command fails: invalid Cargo.toml, unparsable lockfile, cargo not resolvable in PATH context, or bad current_dir.

Common situations: Corrupted or hand-edited Cargo.toml/lockfile; running from an environment where cargo is missing or a toolchain shim fails; workspace_root pointing at a non-cargo directory; dependency resolution requiring network while offline.

Related errors


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