LinuxCommandLibrary

cargo-metadata

Retrieve Rust crate metadata in JSON format

TLDR

Print the workspace members and resolved dependencies of the current package

$ cargo metadata
copy

Print only the workspace members and do not fetch dependencies
$ cargo metadata --no-deps
copy

Print metadata in a specific format based on the specified version
$ cargo metadata --format-version [version]
copy

Print metadata with the resolve field including dependencies only for the given target triple (Note: The packages array will still include the dependencies for all targets)
$ cargo metadata --filter-platform [target_triple]
copy

SYNOPSIS

cargo metadata [--format-version VERSION] [--filter-platform TRIPLE] [--all-features] [--no-deps] [--features FEATURES] [-p SPEC...] [--manifest-path PATH]

PARAMETERS

--format-version VERSION
    JSON format version (default: 1, only 1 supported)

--filter-platform TRIPLE
    Filter dependencies to given platform triple (e.g., x86_64-unknown-linux-gnu)

--all-features
    Activate all features of all dependencies

--no-deps
    Exclude resolved dependencies (direct deps only)

--features FEATURES
    Comma/space-separated features to activate

-p, --package SPEC
    Restrict to specific package(s) by name/version

--manifest-path PATH
    Path to Cargo.toml (common Cargo option)

DESCRIPTION

cargo metadata is a Cargo subcommand that emits a structured JSON document describing the current workspace or package, including resolved dependencies, features, packages, targets, and more. It serves as a stable, machine-readable interface for tools like IDEs (e.g., rust-analyzer), CI systems, and scripts to query project structure without parsing Cargo.toml manually.

The output includes keys like packages (array of package objects with name, version, features), workspace_members, resolve (dependency graph), target_directory, and version (format version, default 1).

Common use cases: generating dependency graphs, listing transitive deps, or integrating with external build tools. By default, it resolves the workspace; options allow filtering to specific packages, platforms, or feature sets. Output is deterministic for reproducible tooling.

Designed for stability, format version 1 has been unchanged since introduction, ensuring backward compatibility.

CAVEATS

Output can be very large (>1MB) for complex workspaces; pipe to jq for filtering. Requires Cargo workspace or package context.

JSON OUTPUT KEYS

Core fields: packages (all pkgs), workspace_members (member IDs), resolve (nodes/edges graph), target_directory, workspace_root.

HISTORY

Introduced in Cargo 0.18 (2017); stable JSON format 1 since 0.20 (2018). Widely used in Rust tooling ecosystem.

SEE ALSO

cargo(1), rustc(1), jq(1)

Copied to clipboard