CARGO-METADATA(1)
cargo-metadata ๐
okay bestie, welcome to CARGO-METADATA(1), a General Commands Manual page, and honestly? it's giving "your code deserves to know what your Cargo project actually is" and I'm obsessed. ๐
โจ NAME โจ
cargo-metadata โ machine-readable metadata about the current package ๐ฆ
โจ SYNOPSIS โจ
cargo metadata [options]
โจ DESCRIPTION โจ
So basically ๐
this command dumps JSON to stdout with all the info about the workspace members and the resolved dependencies of the current package. That's it, that's the vibe. ๐
Now here's the part where I need you to lock in, because the format of that output is subject to change in future versions of Cargo. Which means: it is highly recommended that you include the --format-version flag so you're future-proofing your code and you actually get the output format you're expecting. It's giving "protect yourself, babe." ๐ซถ For more on what to expect, peek at "Compatibility" down below.
Also!! If you want a Rust API for reading the metadata instead of hand-parsing JSON like a gremlin, there's the cargo_metadata crate over at https://crates.io/crates/cargo_metadata โ iconic, go look. โญ
๐ OUTPUT FORMAT ๐
๐ Compatibility ๐
Within the same output format version, compatibility is maintained โ except in some scenarios, bestie. ๐ Here's a non-exhaustive list of changes that are not considered incompatible:
- Adding new fields โ new fields get added whenever they're needed. Reserving the right to do this helps Cargo evolve without bumping the format version too often. Slay, honestly.
- Adding new values for enum-like fields โ same deal as adding new fields. It keeps metadata evolving without stagnation. No cap. ๐ฅ
- Changing opaque representations โ the inner representations of some fields are, lowkey, implementation details. For example, fields related to "Source ID" are treated as opaque identifiers to tell packages or sources apart. Consumers shouldn't rely on those representations unless it's actually specified. So don't go depending on internals, bestie, that's not the move. ๐
๐ฆ JSON format ๐ฆ
The JSON output has the following format:
{
/* Array of all packages in the workspace.
It also includes all feature-enabled dependencies unless --no-deps is used.
*/
"packages": [
{
/* The name of the package. */
"name": "my-package",
/* The version of the package. */
"version": "0.1.0",
/* The Package ID for referring to the
package within the document and as the `--package` argument to many commands
*/
"id": "file:///path/to/my-package#0.1.0",
/* The license value from the manifest, or null. */
"license": "MIT/Apache-2.0",
/* The license-file value from the manifest, or null. */
"license_file": "LICENSE",
/* The description value from the manifest, or null. */
"description": "Package description.",
/* The source ID of the package, an "opaque" identifier representing
where a package is retrieved from. See "Compatibility" above for
the stability guarantee.
This is null for path dependencies and workspace members.
For other dependencies, it is a string with the format:
- "registry+URL" for registry-based dependencies.
Example: "registry+https://github.com/rust-lang/crates.io-index"
- "git+URL" for git-based dependencies.
Example: "git+https://github.com/rust-lang/cargo?rev=5e85ba14aaa20f8133863373404cb0af69eeef2c#5e85ba14aaa20f8133863373404cb0af69eeef2c"
- "sparse+URL" for dependencies from a sparse registry
Example: "sparse+https://my-sparse-registry.org"
...and the source comment is still going, bestie ๐ญ like it really said "let me finish my thought". Here's the rest of it, plus the source field itself:
The value after the `+` is not explicitly defined, and may change
between versions of Cargo and may not directly correlate to other
things, such as registry definitions in a config file. New source
kinds may be added in the future which will have different `+`
prefixed identifiers.
*/
"source": null,
tl;dr on that one: the bit after the + is not officially defined, it can change between Cargo versions, and it might not line up with other stuff like registry definitions in a config file. And future source kinds may show up with different +-prefixed identifiers. So don't hardcode your whole personality to it ๐ซถ
๐ the dependencies array
/* Array of dependencies declared in the package's manifest. */
"dependencies": [
{
/* The name of the dependency. */
"name": "bitflags",
/* The source ID of the dependency. May be null, see
description for the package source.
*/
"source": "registry+https://github.com/rust-lang/crates.io-index",
/* The version requirement for the dependency.
Dependencies without a version requirement have a value of "*".
*/
"req": "^1.0",
/* The dependency kind.
"dev", "build", or null for a normal dependency.
*/
"kind": null,
/* If the dependency is renamed, this is the new name for
the dependency as a string. null if it is not renamed.
*/
"rename": null,
/* Boolean of whether or not this is an optional dependency. */
"optional": false,
/* Boolean of whether or not default features are enabled. */
"uses_default_features": true,
/* Array of features enabled. */
"features": [],
/* The target platform for the dependency.
null if not a target dependency.
*/
"target": "cfg(windows)",
/* The file system path for a local path dependency.
not present if not a path dependency.
*/
"path": "/path/to/dep",
/* A string of the URL of the registry this dependency is from.
If not specified or null, the dependency is from the default
registry (crates.io).
*/
"registry": null
}
],
okay so every dependency in the manifest gets one of these little entries โจ and here's the field-by-field tea:
nameโ the dependency's name, obviously ๐sourceโ the source ID of the dependency. Can benullโ go peek at the package source description for that onereqโ the version requirement. If a dependency has no version requirement, this is"*"kindโ the dependency kind:"dev","build", ornullfor a normal dependencyrenameโ if the dep got renamed, this is the new name as a string.nullif it didn't get renamedoptionalโ boolean, is this an optional dependency or notuses_default_featuresโ boolean, are default features enabledfeaturesโ array of the features that are enabledtargetโ the target platform for the dependency.nullif it's not a target dependencypathโ the file system path for a local path dependency. Not present if it's not a path dependencyregistryโ a string with the URL of the registry this dependency comes from. If it's not specified or it'snull, the dependency is from the default registry (crates.io) ๐
๐ฆ the targets array
/* Array of Cargo targets. */
"targets": [
{
/* Array of target kinds.
- lib targets list the `crate-type` values from the
manifest such as "lib", "rlib", "dylib",
"proc-macro", etc. (default ["lib"])
- binary is ["bin"]
- example is ["example"]
- integration test is ["test"]
- benchmark is ["bench"]
- build script is ["custom-build"]
*/
"kind": [
"bin"
],
/* Array of crate types.
- lib and example libraries list the `crate-type` values
from the manifest such as "lib", "rlib", "dylib",
"proc-macro", etc. (default ["lib"])
- all other target kinds are ["bin"]
*/
"crate_types": [
"bin"
],
/* The name of the target.
For lib targets, dashes will be replaced with underscores.
*/
"name": "my-package",
/* Absolute path to the root source file of the target. */
"src_path": "/path/to/my-package/src/main.rs",
/* The Rust edition of the target.
Defaults to the package edition.
*/
"edition": "2018",
/* Array of required features.
This property is not included if no required features are set.
*/
"required-features": ["feat1"],
/* Whether the target should be documented by `cargo doc`. */
"doc": true,
/* Whether or not this target has doc tests enabled, and
the target is compatible with doc testing.
*/
"doctest": false,
/* Whether or not this target should be built and run with `--test`
*/
"test": true
}
],
the deets, in order, because I love a list:
kindโ array of target kinds. lib targets list thecrate-typevalues from the manifest like"lib","rlib","dylib","proc-macro", etc. (default is["lib"]). A binary is["bin"], an example is["example"], an integration test is["test"], a benchmark is["bench"], and a build script is["custom-build"]crate_typesโ array of crate types. lib and example libraries list thecrate-typevalues from the manifest like"lib","rlib","dylib","proc-macro", etc. (default["lib"]); all other target kinds are["bin"]nameโ the target's name. For lib targets, dashes get swapped for underscoressrc_pathโ absolute path to the target's root source fileeditionโ the Rust edition of the target. Defaults to the package editionrequired-featuresโ array of required features. This property is not included at all if no required features are setdocโ whethercargo docshould document this targetdoctestโ whether this target has doc tests enabled and whether it's compatible with doc testingtestโ whether this target should be built and run with--test
๐ features, paths, and the whole manifest vibe
/* Set of features defined for the package.
Each feature maps to an array of features or dependencies it
enables.
*/
"features": {
"default": [
"feat1"
],
"feat1": [],
"feat2": []
},
/* Absolute path to this package's manifest. */
"manifest_path": "/path/to/my-package/Cargo.toml",
/* Package metadata.
This is null if no metadata is specified.
*/
"metadata": {
"docs": {
"rs": {
"all-features": true
}
}
},
/* List of registries to which this package may be published.
Publishing is unrestricted if null, and forbidden if an empty array. */
"publish": [
"crates-io"
],
/* Array of authors from the manifest.
Empty array if no authors specified.
*/
"authors": [
"Jane Doe <user@example.com>"
],
/* Array of categories from the manifest. */
"categories": [
"command-line-utilities"
],
/* Optional string that is the default binary picked by cargo run. */
"default_run": null,
/* Optional string that is the minimum supported rust version */
"rust_version": "1.56",
/* Array of keywords from the manifest. */
"keywords": [
"cli"
],
/* The readme value from the manifest or null if not specified. */
"readme": "README.md",
/* The repository value from the manifest or null if not specified. */
"repository": "https://github.com/rust-lang/cargo",
/* The homepage value from the manifest or null if not specified. */
"homepage": "https://rust-lang.org",
/* The documentation value from the manifest or null if not specified. */
"documentation": "https://doc.rust-lang.org/stable/std",
/* The default edition of the package.
Note that individual targets may have different editions.
*/
"edition": "2018",
/* Optional string that is the name of a native library the package
is linking to.
*/
"links": null,
}
],
quick rundown so nothing slips by:
featuresโ the set of features defined for the package. Each feature maps to an array of features or dependencies that it enables ๐ธmanifest_pathโ absolute path to this package's manifestmetadataโ package metadata. This isnullif no metadata is specifiedpublishโ list of registries this package may be published to. Publishing is unrestricted ifnull, and forbidden if it's an empty arrayauthorsโ array of authors from the manifest. Empty array if no authors were specifiedcategoriesโ array of categories from the manifestdefault_runโ optional string, the default binary thatcargo runpicksrust_versionโ optional string, the minimum supported Rust versionkeywordsโ array of keywords from the manifestreadmeโ the readme value from the manifest, ornullif it's not specifiedrepositoryโ the repository value from the manifest, ornullif it's not specifiedhomepageโ the homepage value from the manifest, ornullif it's not specifieddocumentationโ the documentation value from the manifest, ornullif it's not specifiededitionโ the default edition of the package. Note: individual targets may have different editions!linksโ optional string, the name of a native library the package is linking to
๐ workspace members
/* Array of members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_members": [
"file:///path/to/my-package#0.1.0",
],
/* Array of default members of the workspace.
Each entry is the Package ID for the package.
*/
"workspace_default_members": [
"file:///path/to/my-package#0.1.0",
],
two arrays, both full of Package IDs:
workspace_membersโ array of the workspace's members. Each entry is the Package ID for that packageworkspace_default_membersโ array of the workspace's default members. Each entry is also the Package ID for the package
๐ and now: resolve
this is the big one, the resolved dependency graph for the entire workspace. Read the comments carefully because they're doing a lot of heavy lifting here:
// The resolved dependency graph for the entire workspace. The enabled
// features are based on the enabled features for the "current" package.
// Inactivated optional dependencies are not listed.
//
// This is null if --no-deps is specified.
//
// By default, this includes all dependencies for all target platforms.
// The `--filter-platform` flag may be used to narrow to a specific
// target triple.
"resolve": {
/* Array of nodes within the dependency graph.
Each node is a package.
*/
"nodes": [
{
/* The Package ID of this node. */
"id": "file:///path/to/my-package#0.1.0",
/* The dependencies of this package, an array of Package IDs. */
"dependencies": [
"https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
],
/* The dependencies of this package. This is an alternative to
"dependencies" which contains additional information. In
particular, this handles renamed dependencies.
*/
"deps": [
{
/* The name of the dependency's library target.
If this is a renamed dependency, this is the new
name.
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
/* The dependency kind.
"kind": null,
/* The target platform for the dependency.
null if not a target dependency.
*/
"target": "cfg(windows)"
}
]
}
],
/* Array of features enabled on this package. */
"features": [
"default"
]
}
],
/* The root package of the workspace.
This is null if this is a virtual workspace. Otherwise it is
the Package ID of the root package.
*/
"root": "file:///path/to/my-package#0.1.0",
},
/* The absolute path to the build directory where Cargo places its output. */
"target_directory": "/path/to/my-package/target",
/* The version of the schema for this metadata structure.
This will be changed if incompatible changes are ever made.
*/
"version": 1,
/* The absolute path to the root of the workspace. */
"workspace_root": "/path/to/my-package"
/* Workspace metadata.
This is null if no metadata is specified. */
"metadata": {
"docs": {
"rs": {
"all-features": true
}
}
}
}
And that's the whole shape of it bestie, every field lovingly annotated ๐
Notes:
- For
"id"field syntax, see Package ID Specificationshttps://doc.rust-lang.org/cargo/reference/pkgid-spec.htmlin the reference. ๐ซถ
โจ OPTIONS โจ
๐ Output Options
--no-depsโ only spill the tea about the workspace members, and do not go fetch dependencies. Keep it local, keep it cute.--format-versionversion โ pick which version of the output format you want. Right now1is literally the only value that exists, so that's your pick. ๐--filter-platformtriple โ this filters the resolve output so it only includes dependencies for the given target triplehttps://doc.rust-lang.org/cargo/appendix/glossary.html#target. Without this flag, the resolve just includes all the targets, no filter, no chill.
Heads up bestie: the dependencies listed in the"packages"array still include <em>all</em> dependencies. Each package definition is meant to be an unaltered reproduction of the info insideCargo.toml. No edits, no glow-up, just the facts. ๐
๐ธ Feature Selection
The feature flags let you control which features get enabled. When you give no feature options at all, the default feature is activated for every selected package. That's the vibe by default, periodt.
See the features documentation https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options for more deets. ๐
-Ffeatures,--featuresfeatures โ a space- or comma-separated list of features to activate. You can enable features of workspace members with thepackage-name/feature-namesyntax ๐. This flag can be given multiple times, and giving it more than once enables all the features you listed.--all-featuresโ activate every single available feature of all selected packages. Go big, no cap.--no-default-featuresโ do not activate the default feature of the selected packages. Respectfully declining the default. ๐
๐ฆ Display Options
-v,--verboseโ use verbose output. Give it twice for "very verbose" output, which includes extra stuff like dependency warnings and build script output. Can also be set with theterm.verboseconfig valuehttps://doc.rust-lang.org/cargo/reference/config.html. Yes queen, more words please. ๐ฌ-q,--quietโ do not print cargo log messages. Can also be set with theterm.quietconfig valuehttps://doc.rust-lang.org/cargo/reference/config.html. Silence, but make it fashion. ๐คซ--colorwhen โ control when colored output gets used. Valid values are:auto(the default): automatically detect whether the terminal supports color. ๐always: always display colors, always serving looks. ๐never: never display colors. Monochrome moment. ๐ค
Can also be set with the term.color config value https://doc.rust-lang.org/cargo/reference/config.html.
๐ Manifest Options
--manifest-pathpath โ path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. It'll go looking for you, bestie. ๐--lockedโ asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of these scenarios happens:- The lock file is missing. ๐ญ
- Cargo tried to change the lock file because of a different dependency resolution. ๐
It can be used in environments where deterministic builds are the goal, like in CI pipelines. Consistency is the moment.
--offlineโ stops Cargo from touching the network for any reason. Without this flag, Cargo stops with an error if it needs the network and the network isn't there. With this flag, Cargo will try to proceed without the network if it can. โ๏ธ
Beware bestie ๐จ this can give you a <em>different</em> dependency resolution than online mode. Cargo restricts itself to crates that are already downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline.
Can also be set with the net.offline config value https://doc.rust-lang.org/cargo/reference/config.html.
--frozenโ equivalent to specifying both--lockedand--offlineat the same time. Two icons, one flag. ๐
๐ Common Options
+toolchainโ if Cargo was installed with rustup, and the first argument to cargo starts with+, it gets interpreted as a rustup toolchain name (like+stableor+nightly). See the rustup documentationhttps://rust-lang.github.io/rustup/overrides.htmlfor more on how toolchain overrides work. Slay, pick your era. โญ--configKEY=VALUE or PATH โ overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or given as a path to an extra configuration file. This flag can be specified multiple times. See the command-line overrides sectionhttps://doc.rust-lang.org/cargo/reference/config.html#command-line-overridesfor more info. ๐ ๏ธ-CPATH โ changes the current working directory before executing any specified operations. This affects stuff like where cargo looks by default for the project manifest (Cargo.toml), and also the directories it searches for discovering.cargo/config.toml, for example. This option must appear before the command name, likecargo -C path/to/my-project build.
Fr fr, this option is only available on the nightly channelhttps://doc.rust-lang.org/book/appendix-07-nightly-rust.htmland it needs the-Z unstable-optionsflag to be enabled (see #10098https://github.com/rust-lang/cargo/issues/10098). Nightly girlies only. ๐
-h,--helpโ prints help information. When in doubt, ask for help, that's self care. ๐ซถ-Zflag โ unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor details. The mysterious ones. ๐ฎ
๐ ENVIRONMENT
See the reference https://doc.rust-lang.org/cargo/reference/environment-variables.html for details on the environment variables that Cargo reads. It's all in there, iykyk. ๐ง
๐ EXIT STATUS
0: Cargo succeeded. ๐101: Cargo failed to complete. ๐ญ
๐ EXAMPLES
- Output JSON about the current package:
cargo metadata --format-version=1
๐ฅ SEE ALSO
cargo(1), cargo-pkgid(1), Package ID Specifications https://doc.rust-lang.org/cargo/reference/pkgid-spec.html, JSON messages https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages
And that's the whole page, bestie โ you just read the entire cargo-metadata(1) manual, from the top of the NAME section all the way down to this little sign-off. That's the CARGO-METADATA(1) page, straight from the cargo docs, and it's giving reference manual but make it readable. ๐
โจ Go forth and cargo metadata responsibly. Mwah. ๐ซถ
๐ฆ where this page comes from
- ubuntu jammy: cargo-1.81 1.81.0+dfsg0ubuntu0-0ubuntu0.22.04.1 ยท Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.81 1.81.0+dfsg0ubuntu1-0ubuntu0.24.04.1 ยท MIT OR Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms