CARGO-METADATA(1)
๐ CARGO-METADATA(1) โ General Commands Manual ๐
okay bestie, pull up a chair, because this one is literally the page that tells you what cargo-metadata is, and it's giving "machine-readable metadata about the current package" ๐ฆ
โจ NAME โจ
cargo-metadata โ Machine-readable metadata about the current package
๐ธ SYNOPSIS ๐ธ
cargo metadata [options]
๐ DESCRIPTION ๐
So what it does is: it outputs JSON to stdout containing information about the workspace members and resolved dependencies of the current package. That's the whole vibe. No cap. ๐
โ ๏ธ okay so this is the important part โ the format of the output is subject to change in futures versions of Cargo. Not a typo, that's what it says, and either way: things can change. It is recommended to include the --format-version flag to future-proof your code to ensure the output is in the format you are expecting. For more on the expectations, see "Compatibility" (it's down below, keep scrolling bestie ๐ซถ).
Also, if you want a Rust API for reading the metadata, go look at the cargo_metadata crate at https://crates.io/crates/cargo_metadata โ iconic of them honestly.
๐ OUTPUT FORMAT ๐
๐ Compatibility ๐
Within the same output format version, the compatibility is maintained, except some scenarios. And the following is a non-exhaustive list of changes that are not considered as incompatible โ meaning: don't come for Cargo when these happen, they were always allowed ๐ญ
- Adding new fields โ New fields will be added when needed. Reserving this helps Cargo evolve without bumping the format version too often. Lowkey genius.
- Adding new values for enum-like fields โ Same as adding new fields. It keeps metadata evolving without stagnation.
- Changing opaque representations โ The inner representations of some fields are implementation details. For example, fields related to "Source ID" are treated as opaque identifiers to differentiate packages or sources. Consumers shouldn't rely on those representations unless specified. Fr fr, don't peek behind the curtain ๐
๐ง JSON format ๐ง
The JSON output has the following format โ and yes, we're pasting it exactly, because every single character here matters:
{
/* 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"
quick little breakdown of the sauce in there, because it's a lot ๐
packagesโ array of all packages in the workspace. It also includes all feature-enabled dependencies unless--no-depsis used.nameโ the name of the package.versionโ the version of the package.idโ the Package ID for referring to the package within the document and as the--packageargument to many commands.licenseโ thelicensevalue from the manifest, ornull.license_fileโ thelicense-filevalue from the manifest, ornull.descriptionโ thedescriptionvalue from the manifest, ornull.- the source ID โ an "opaque" identifier representing where a package is retrieved from. See "Compatibility" above for the stability guarantee.
- This is
nullfor 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"
- This is
...and the source ID rules keep going, bestie, because that + is not the vibe you think it is ๐
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,
/* 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,
/* (unstable) Boolean flag of whether or not this is a pulbic
dependency. This field is only present when
`-Zpublic-dependency` is enabled.
*/
"public": false
}
],
okay so real talk: the thing after the + is not explicitly defined, and it may change between versions of Cargo, and it may not directly correlate to stuff like registry definitions in a config file. Also new source kinds can show up in the future and they'll have their own different +-prefixed identifiers. so don't hardcode your whole personality to it, fr fr ๐ญ
and yes โ it really says "pulbic" up there in the public comment. not a typo on my end, that's verbatim from the docs, no cap. also peep that public field is (unstable) and only shows up when -Zpublic-dependency is enabled. ๐
req being "*" means no version requirement was given, kind is "dev", "build", or null for a normal dependency, and rename is null unless the dep got renamed. slay.
okay now the targets and the whole package-info glow-up โจ
/* 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
}
],
/* 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,
}
],
not the "publish" lore ๐ โ publishing is unrestricted if it's null, and forbidden if it's an empty array. it's giving permission slip energy. and "required-features" is not included if there are no required features, so don't go looking for it, she's just not there ๐
also "links" is an optional string naming a native library the package links to, and "rust_version" is an optional string that's the minimum supported rust version. "default_run" is an optional string and it's the default binary picked by cargo run. ๐
and now the workspace + the resolve graph, aka the main character section ๐
/* 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",
],
// 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. */
okay so important resolve tea โ: it's the resolved dependency graph for the entire workspace, the enabled features are based on the enabled features for the "current" package, and inactivated optional dependencies are not listed. it's null if --no-deps is specified. by default it includes all dependencies for all target platforms, and the --filter-platform flag can narrow it to a specific target triple. ๐ฆ
and then we're right in the middle of a deps entry โ the JSON just keeps going, bestie, so part 3 is gonna pick it up right there ๐
โจ
"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.
"dev", "build", or null for a normal dependency.
*/
"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
}
}
}
}
Notes ๐
- For the
"id"field syntax, go peek at Package ID Specifications <https://doc.rust-lang.org/cargo/reference/pkgid-spec.html> in the reference. that's where the lore lives bestie โจ
OPTIONS ๐
Output Options ๐
--no-depsโ only spits out info about the workspace members, and does not go fetch the dependencies. keeping it local, keeping it chill ๐ซถ--format-version versionโ tells Cargo which version of the output format you want. rn1is literally the only possible value, that's it, that's the whole menu ๐--filter-platform tripleโ this filters the resolve output so it only includes dependencies for the given target triple <https://doc.rust-lang.org/cargo/appendix/glossary.html#target>. skip this flag and the resolve just includes all the targets, no filter, no thoughts ๐ญ
heads up bestie: the dependencies listed in the"packages"array still include all dependencies. each package definition is meant to be an unaltered reproduction of the info insideCargo.toml. no glow-ups, no edits, no cap ๐
Feature Selection ๐ธ
The feature flags are how you control which features get enabled. When you pass no feature options at all, the default feature gets activated for every selected package. that's the auto-pilot vibe โ๏ธ
See the features documentation <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options> for more details. iykyk ๐
-F features,--features featuresโ a space- or comma-separated list of features to activate. features of workspace members can be switched on with thepackage-name/feature-namesyntax. this flag can be specified multiple times, and doing that enables all the specified features. stack 'em, slay ๐--all-featuresโ activate all available features of all selected packages. go big or go home โจ--no-default-featuresโ do not activate the default feature of the selected packages. respectfully, no defaults today ๐
Display Options ๐ฆ
-v,--verboseโ use verbose output. you can pass it twice for "very verbose" output, which includes extra stuff like dependency warnings and build script output. may also be specified with theterm.verboseconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. the way that double-vunlocks the gossip ๐ญ-q,--quietโ do not print cargo log messages. may also be specified with theterm.quietconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. shhh era ๐คซ--color whenโ controls when colored output gets used. valid values are:auto(default): automatically detect if color support is available on the terminal.always: always display colors. ๐never: never display colors. ๐
May also be specified with the term.color config value <https://doc.rust-lang.org/cargo/reference/config.html>.
Manifest Options ๐
--manifest-path pathโ path to theCargo.tomlfile. by default, Cargo goes hunting for theCargo.tomlfile in the current directory or any parent directory. it will find it, it always finds it ๐--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 shows up:- The lock file is missing.
- Cargo attempted to change the lock file due to a different dependency resolution.
it may be used in environments where deterministic builds are desired, such as in CI pipelines. for the girlies who need it to be the same every single time ๐
--offlineโ prevents Cargo from accessing the network for any reason. without this flag, Cargo will stop with an error if it needs the network and the network isn't available. with this flag, Cargo will attempt to proceed without the network if possible. airplane mode but make it builds โ๏ธ
beware bestie: this may result in a different dependency resolution than online mode. Cargo will restrict itself to crates that are 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. prep first, then vibe ๐ง
May also be specified with the net.offline config value <https://doc.rust-lang.org/cargo/reference/config.html>.
--frozenโ equivalent to specifying both--lockedand--offline. two-for-one, iconic combo ๐ซถ--lockfile-path PATHโ changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) toPATH.PATHmust end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). note that providing--lockfile-pathwill ignore the existing lockfile at the default path, and instead will either use the lockfile fromPATH, or write a new lockfile into the providedPATHif it doesn't exist. this flag can be used to run most commands in read-only directories, writing the lockfile into the providedPATH. no touchy the read-only dir ๐
this option is only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and requires the -Z unstable-options flag to enable (see #14421 <https://github.com/rust-lang/cargo/issues/14421>). nightly only, you've been warned โญ
Common Options โจ
+toolchainโ if Cargo has been installed with rustup, and the first argument tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). see the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more info about how toolchain overrides work. pick your fighter ๐ฎ--config KEY=VALUEorPATHโ overrides a Cargo configuration value. the argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. this flag may be specified multiple times. see the command-line overrides section <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides> for more info. main character energy, overriding everything ๐-C PATHโ changes the current working directory before executing any specified operations. this affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering.cargo/config.toml, for example. this option must appear before the command name, for examplecargo -C path/to/my-project build. order matters bestie, don't fight it ๐
this option is only available on the nightly channel <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and requires the -Z unstable-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>). nightly only, again ๐
-h,--helpโ prints help information. when in doubt, ask for help, that's healthy ๐ซถ-Z flagโ unstable (nightly-only) flags to Cargo. runcargo -Z helpfor details. the forbidden menu, only unlocked at night ๐โจ
ENVIRONMENT ๐ฑ
See the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on the environment variables that Cargo reads. it reads them, it knows things ๐
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 a wrap on CARGO-METADATA(1), bestie ๐ the little footer down there is just the page name and its date stamp, lowkey doing its own thing so the docs stay versioned and cute. go read your JSON, you ate ๐โจ๐
๐ฆ where this page comes from
- ubuntu jammy: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo10-0ubuntu4.22.04.1 ยท Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo2-0ubuntu2.24.04.1 ยท Apache-2.0 WITH LLVM exception
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms