CARGO-METADATA(1)

๐Ÿ“ฆ version 1.81.0, in ubuntu jammy, ubuntu noble

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 be null โ€” go peek at the package source description for that one
  • req โ€” the version requirement. If a dependency has no version requirement, this is "*"
  • kind โ€” the dependency kind: "dev", "build", or null for a normal dependency
  • rename โ€” if the dep got renamed, this is the new name as a string. null if it didn't get renamed
  • optional โ€” boolean, is this an optional dependency or not
  • uses_default_features โ€” boolean, are default features enabled
  • features โ€” array of the features that are enabled
  • target โ€” the target platform for the dependency. null if it's not a target dependency
  • path โ€” the file system path for a local path dependency. Not present if it's not a path dependency
  • registry โ€” a string with the URL of the registry this dependency comes from. If it's not specified or it's null, 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 the crate-type values 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 the crate-type values 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 underscores
  • src_path โ€” absolute path to the target's root source file
  • edition โ€” the Rust edition of the target. Defaults to the package edition
  • required-features โ€” array of required features. This property is not included at all if no required features are set
  • doc โ€” whether cargo doc should document this target
  • doctest โ€” whether this target has doc tests enabled and whether it's compatible with doc testing
  • test โ€” 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 manifest
  • metadata โ€” package metadata. This is null if no metadata is specified
  • publish โ€” list of registries this package may be published to. Publishing is unrestricted if null, and forbidden if it's an empty array
  • authors โ€” array of authors from the manifest. Empty array if no authors were specified
  • categories โ€” array of categories from the manifest
  • default_run โ€” optional string, the default binary that cargo run picks
  • rust_version โ€” optional string, the minimum supported Rust version
  • keywords โ€” array of keywords from the manifest
  • readme โ€” the readme value from the manifest, or null if it's not specified
  • repository โ€” the repository value from the manifest, or null if it's not specified
  • homepage โ€” the homepage value from the manifest, or null if it's not specified
  • documentation โ€” the documentation value from the manifest, or null if it's not specified
  • edition โ€” 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 package
  • workspace_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:


โœจ 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-version version โ€” pick which version of the output format you want. Right now 1 is literally the only value that exists, so that's your pick. ๐Ÿ‘‘
  • --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. 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 inside Cargo.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. ๐Ÿ’œ

  • -F features, --features features โ€” a space- or comma-separated list of features to activate. You can enable features of workspace members with the package-name/feature-name syntax ๐ŸŽ€. 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 the term.verbose config value https://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 the term.quiet config value https://doc.rust-lang.org/cargo/reference/config.html. Silence, but make it fashion. ๐Ÿคซ
  • --color when โ€” 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-path path โ€” path to the Cargo.toml file. By default, Cargo searches for the Cargo.toml file 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 existing Cargo.lock file 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 --locked and --offline at 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 +stable or +nightly). See the rustup documentation https://rust-lang.github.io/rustup/overrides.html for more on how toolchain overrides work. Slay, pick your era. โญ
  • --config KEY=VALUE or PATH โ€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or given as a path to an extra configuration file. This flag can 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. ๐Ÿ› ๏ธ
  • -C PATH โ€” 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, like cargo -C path/to/my-project build.
Fr fr, this option is only available on the nightly channel https://doc.rust-lang.org/book/appendix-07-nightly-rust.html and it needs the -Z unstable-options flag to be enabled (see #10098 https://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. ๐Ÿซถ
  • -Z flag โ€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for 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

  1. 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