CARGO-METADATA(1)

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

๐Ÿ’– cargo-metadata โ€” bestie edition ๐Ÿ’–

okay so <rainbow>cargo-metadata</rainbow> โ€” it's the Cargo subcommand that spits out machine-readable metadata about the current package. that's it, that's the whole vibe. โœจ


๐ŸŒธ 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 stuffed with info about the workspace members and the resolved dependencies of the current package. no cap, that's the whole thing. ๐Ÿ’…

โš ๏ธ okay listen โ€” the format of that output is subject to change in futures versions of Cargo. fr fr. so it's highly recommended you pass the --format-version flag to future-proof your code, so you actually get the output in the format you're expecting. for more on what to expect, go read "Compatibility" below. ๐Ÿ‘‡

also, if you're a Rust girlie and want an actual API for reading the metadata instead of vibing with raw JSON, there's the cargo_metadata crate <https://crates.io/crates/cargo_metadata> ๐Ÿ’œ


๐ŸŒˆ OUTPUT FORMAT ๐ŸŒˆ

โœจ Compatibility โœจ

within the same output format version, compatibility is maintained โ€” except in some scenarios. here's a non-exhaustive list of changes that are NOT considered incompatible, so don't come for us: ๐Ÿ™ˆ

  • Adding new fields โ€” new fields get added whenever they're needed. reserving this lets Cargo evolve without bumping the format version too often. iconic behavior tbh.
  • Adding new values for enum-like fields โ€” same energy as adding new fields. it keeps metadata evolving without stagnation. growth mindset ๐Ÿ’…
  • Changing opaque representations โ€” the inner representations of some fields are literally implementation details. for example, fields related to "Package ID" or "Source ID" are treated as opaque identifiers to differentiate packages or sources. so consumers shouldn't rely on those representations unless it's specified. periodt. ๐ŸŽ€

๐Ÿฆ‹ 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, an opaque and unique identifier for referring to the
               package. See "Compatibility" above for the stability guarantee.
            */
            "id": "my-package 0.1.0 (path+file:///path/to/my-package)",
            /* 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"
                          the value after the `+`? bestie it is NOT explicitly defined ๐Ÿ˜ญ it may
                          change between versions of Cargo, and it may not directly correlate to
                          other things, like registry definitions in a config file. new source
                          kinds may be added in the future, and those will have different `+`
                          prefixed identifiers.
                       */
                       "source": null,
                       /* array of dependencies declared in the package's manifest. */
                       "dependencies": [
                           {
                               /* the name of the dependency. that's it. that's the field. */
                               "name": "bitflags",
                               /* the source ID of the dependency. it may be null, so go read the
                                  description for the package source up above, ok?
                               */
                               "source": "registry+https://github.com/rust-lang/crates.io-index",
                               /* the version requirement for the dependency.
                                  dependencies with no version requirement just 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 that was the deps, slay. now the targets, and this is where it gets cute:

                       /* array of Cargo targets. */
                       "targets": [
                           {
                               /* array of target kinds. okay so:
                                  - 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. */
                               "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
                           }
                       ],

๐Ÿฆ‹ and then the package-level lore โ€” features, metadata, publish, the whole personality section:

                       /* 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,
                   }
               ],

and now we get to the workspace tea ๐Ÿ‘‘

               /* array of members of the workspace.
                  each entry is the Package ID for the package.
               */
               "workspace_members": [
                   "my-package 0.1.0 (path+file:///path/to/my-package)",
               ],
               /* array of default members of the workspace.
                  each entry is the Package ID for the package.
               */
               "workspace_default_members": [
                   "my-package 0.1.0 (path+file:///path/to/my-package)",
               ],
               // 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": "my-package 0.1.0 (path+file:///path/to/my-package)",
                           /* the dependencies of this package, an array of Package IDs. */
                           "dependencies": [
                               "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
                           ],
                           /* 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": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
                                   /* array of dependency kinds. added in Cargo 1.40. */
                                   "dep_kinds": [
                                       {
                                           /* the dependency kind.
                                              "dev", "build", or null for a normal dependency.

โ€ฆand here's the rest of that JSON, bestie, all the way to the closing brace:

                                           */
                                           "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": "my-package 0.1.0 (path+file:///path/to/my-package)"
               },
               /* 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
                       }
                   }
               }
           }

๐Ÿ’… OPTIONS ๐Ÿ’…

โœจ Output Options โœจ

  • --no-deps โ€” only spill the tea about your workspace members, okay? No dependency fetching, no extra drama. Just the members. ๐Ÿซถ
  • --format-version version โ€” pick which version of the output format you want. Right now 1 is literally the only valid value, so it's giving "one option only, bestie." ๐Ÿ’€
  • --filter-platform triple โ€” this one filters the resolve output so it only includes deps for the target triple you name (https://doc.rust-lang.org/cargo/appendix/glossary.html#target). Skip this flag and the resolve just includes all the targets, no filter, no chill.
heads up bestie ๐Ÿšจ the dependencies listed in the "packages" array still include all dependencies regardless. Every package definition is meant to be an unaltered reproduction of the info inside Cargo.toml โ€” no edits, no touch-ups, periodt.

๐ŸŽ€ Feature Selection ๐ŸŽ€

The feature flags let you control which features get enabled. When you give no feature options at all, the default feature gets activated for every selected package. That's the vibe.

For the whole lore, see the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options. ๐Ÿงš

  • -F features, --features features โ€” a space or comma separated list of features to activate. Features of workspace members can be switched on with the package-name/feature-name syntax. And yes, you can pass this flag multiple times โ€” every specified feature gets enabled. โœจ
  • --all-features โ€” activate all the available features of all selected packages. Go feral. ๐Ÿ‘‘
  • --no-default-features โ€” do NOT activate the default feature of the selected packages. Sometimes we're in our minimalist era. ๐ŸŒธ

๐Ÿฆ‹ Display Options ๐Ÿฆ‹

  • -v, --verbose โ€” verbose output. Pass it twice and you get "very verbose" output, which includes extra stuff like dependency warnings and build script output. You can also set it with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html).
  • -q, --quiet โ€” do not print cargo log messages. Also settable with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). Silent mode, main character energy. ๐Ÿคซ
  • --color when โ€” controls when colored output is used. Valid values, bestie:
    • auto (the default): automatically detect if the terminal supports color. ๐Ÿ”
    • always: always display colors. ๐ŸŒˆ
    • never: never display colors. ๐Ÿ’€

Can also be set with the term.color config value (https://doc.rust-lang.org/cargo/reference/config.html).

๐ŸŒธ Manifest Options ๐ŸŒธ

  • --manifest-path path โ€” the path to the Cargo.toml file. By default Cargo goes hunting for Cargo.toml in the current directory or any parent directory. It's giving "I'll find it, don't worry." ๐Ÿ”Ž
  • --frozen, --locked โ€” either one of these flags requires that the Cargo.lock file is up-to-date. If the lock file is missing, or it needs updating, Cargo will exit with an error. The --frozen flag also stops Cargo from even attempting to access the network to figure out if it's out-of-date.

Use these in environments where you wanna assert that Cargo.lock is up-to-date (like a CI build, slay) or where you wanna avoid network access entirely. ๐Ÿ›ก๏ธ

  • --offline โ€” prevents Cargo from accessing the network for any reason whatsoever. Without this flag, Cargo just stops with an error if it needs the network and the network isn't there. With this flag, Cargo will attempt to proceed without the network if it possibly can. ๐Ÿ’ช
beware bestie ๐Ÿ˜ญ this may result in different dependency resolution than online mode. Cargo restricts itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. Check out the cargo-fetch(1) command to download dependencies before you go offline. ๐Ÿงš

Can also be set with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html).

๐Ÿ‘‘ 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 at https://rust-lang.github.io/rustup/overrides.html for more on how toolchain overrides work. ๐Ÿฆ€
  • --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 may be specified multiple times. See the command-line overrides section at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more. ๐ŸŽ€
  • -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 has to appear before the command name, like cargo -C path/to/my-project build.
ngl this option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it requires the -Z unstable-options flag to enable (see #10098 at https://github.com/rust-lang/cargo/issues/10098). ๐Ÿšง
  • -h, --help โ€” prints help information. The classic. ๐Ÿซถ
  • -Z flag โ€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for the details. ๐Ÿ”ฎ

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

See the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html for the details on environment variables that Cargo reads. iykyk ๐Ÿ’œ


โญ EXIT STATUS โญ

  • 0: Cargo succeeded. ๐ŸŽ‰ ate, no crumbs.
  • 101: Cargo failed to complete. ๐Ÿ˜ญ it's giving tragedy.

๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

  1. Output JSON about the current package:
cargo metadata --format-version=1

๐Ÿ’œ SEE ALSO ๐Ÿ’œ


okay that's the whole page bestie, we did it ๐Ÿ’– that footer at the bottom? it's just CARGO-METADATA(1) โ€” the Cargo manual, this very page, no cap. go forth and pipe JSON into things. ๐ŸŽ€โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from