CARGO-METADATA(1)

πŸ“¦ version 1.78.0, in ubuntu jammy, ubuntu noble

πŸ¦‹ cargo-metadata(1) β€” the manual page, but make it a group chat πŸ¦‹

okay bestie, welcome to cargo-metadata(1) β€” yes it lives in the General Commands Manual, and yes it's about to be your new fave. buckle up πŸŽ€


✨ NAME ✨

cargo-metadata β€” Machine-readable metadata about the current package πŸ’…


🌸 SYNOPSIS 🌸

cargo metadata [options]

πŸ’– DESCRIPTION πŸ’–

so what it does is: it spits out JSON to stdout stuffed with info about the workspace members and the resolved dependencies of the current package. that's it, that's the vibe 🌈

heads up bestie 🚨 the format of the output is subject to change in future versions of Cargo. so you should totally pass the --format-version flag to future-proof your code and make sure you get the output shape you're actually expecting. for more on what you can and can't count on, go read "Compatibility" down below πŸ‘‡

and if you want a Rust API for reading the metadata (iconic behaviour honestly), go check the cargo_metadata crate (https://crates.io/crates/cargo_metadata) πŸ¦‹


πŸŽ€ OUTPUT FORMAT πŸŽ€

🌟 Compatibility 🌟

within the same output format version, compatibility is maintained β€” except in a few scenarios. here's a non-exhaustive list of changes that are not considered incompatible (iykyk):

  • Adding new fields β€” new fields get added when they're needed. reserving this room lets Cargo evolve without bumping the format version every five seconds πŸ’€
  • Adding new values for enum-like fields β€” same energy as adding new fields. it keeps the metadata evolving instead of stagnating ✨
  • Changing opaque representations β€” the inner representations of some fields are literally just 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 specified 🫢

🌈 JSON format 🌈

the JSON output has this format, no cap:

{
    /* 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"

okay so we were literally mid-comment on the source field β€” no biggie, picking it right back up 🫢

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

bestie, real talk about that source field: it can be a string or null, and the whole + situation? do not build your personality around it. The value after the + is not explicitly defined, it can 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 get added in the future and they'll have totally different +-prefixed identifiers. It's giving moving target πŸ’€ so vibe with it, don't cling.

Then dependencies rolls in as an array of everything declared in the package's manifest πŸ“¦. Each entry is a whole little moment: name (the dependency's name), source (the source ID β€” may be null, see the package source description above), req (the version requirement, and if there's no version requirement at all it's just "*"), kind ("dev", "build", or null when it's a normal dependency), rename (the new name as a string if it got renamed, null if it didn't), optional (boolean), uses_default_features (boolean), features (array), target (the target platform, or null if it's not a target dependency), path (the filesystem path for a local path dependency β€” just not present if it isn't one), and registry (a string URL of the registry it's from; if it's unspecified or null, it's from the default registry, crates.io).


next up, the glow-up era: targets ✨

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

so kind is an array of target kinds and here's the whole roster so you never have to guess πŸŽ€ β€” lib targets list the crate-type values from the manifest, like "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"].

crate_types is its own array: lib and example libraries list the crate-type values from the manifest, like "lib", "rlib", "dylib", "proc-macro", etc. (default ["lib"]), and all the other target kinds are ["bin"]. Then you've got name (the target's name), src_path (an absolute path to the target's root source file), and edition (the Rust edition of the target β€” it defaults to the package edition).

required-features is an array of required features, and psst πŸ‘€ this property is not included if no required features are set. Then the three little booleans doing their thing: doc (should cargo doc document this target?), doctest (does this target have doc tests enabled and is it compatible with doc testing?), and test (should this target be built and run with --test?).


okay now we're in the features-and-manifest zone, keep up πŸ’…

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

features is the set of features defined for the package β€” each feature maps to an array of the features or dependencies it enables 🌸. manifest_path is the absolute path to this package's manifest. metadata is package metadata, and it's null if no metadata is specified.

and publish?? iconic and also kinda serious: it's the list of registries the package may be published to. Publishing is unrestricted if null, and forbidden if it's an empty array. that's the whole ruleset, no notes πŸ‘‘


the manifest-era continues, we're so back πŸ¦‹

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

authors is the array from the manifest (empty array if nobody's credited), categories is the array from the manifest, and default_run is an optional string β€” the default binary that cargo run picks. rust_version is another optional string, this one being the minimum supported Rust version. keywords is the array from the manifest, because SEO is forever πŸ“.


link-in-bio time, literally ✨

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

so: readme is the readme value from the manifest, or null if it's not specified. repository is the repository value from the manifest, or null if it's not specified. homepage is the homepage value from the manifest, or null if it's not specified. documentation is the documentation value from the manifest, or null if it's not specified β€” see the pattern? cute, consistent, we love that for us πŸ’–

edition is the default edition of the package, and ⚠️ heads up: individual targets may have different editions. links is an optional string that's the name of a native library the package is linking to.


and now, the workspace girlies πŸ‘‘

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

workspace_members is the array of members of the workspace, where each entry is the Package ID for the package. workspace_default_members is the array of default members of the workspace, and yep, each entry is the Package ID too. two arrays, same energy, different jobs 🌈


final stretch of this part, and it's the main character: the resolve graph πŸ”₯

               // 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.
                                              "dev", "build", or null for a normal dependency.
                                           */

so resolve is the resolved dependency graph for the entire workspace, and the enabled features are based on the enabled features for the "current" package. 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 may be used to narrow it down to a specific target triple 🎯

Inside, nodes is the array of nodes in the dependency graph β€” each node is a package. Each node has an id (the Package ID of this node) and dependencies (an array of Package IDs for this package's dependencies).

Then there's deps, which is an alternative to "dependencies" that carries extra info β€” in particular, it handles renamed dependencies. Each deps entry has a name (the name of the dependency's library target, and if the dependency is renamed, this is the new name) and a pkg (the Package ID of the dependency). Then dep_kinds β€” an array of dependency kinds, added in Cargo 1.40 β€” and each one holds the dependency kind: "dev", "build", or null for a normal dependency.

and that's exactly where we're leaving it β€” mid-dep_kinds, bestie, the cliffhanger of the century 😭

…and we're STILL in the JSON, bestie, don't worry, it ends soon. Here's the rest of that example output, verbatim, no cap:

                                           "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: ✨


πŸŽ€ OPTIONS πŸŽ€

πŸ’– Output Options

  • --no-deps β€” only spills the tea about your workspace members, and does not go fetch the dependencies. no extra crates, no drama πŸ’…
  • *--format-version *version β€” tells Cargo which version of the output format you want. rn 1 is literally the only option, so… yeah, 1 it is 🫢
  • --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 includes **all targets, bestie, all of them.

heads up tho: the dependencies listed in the "packages" array still includes all dependencies. each package definition is meant to be an unaltered reproduction of the info inside Cargo.toml. so like… the filter is doing one specific job, not all the jobs πŸ™ˆ

🌸 Feature Selection

these feature flags are how you control which features get enabled. when you give no feature options at all, the default feature is activated for every selected package. automatic, no thoughts required πŸ’–

for more deets, see the features documentation (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. you can turn on features of workspace members with the package-name/feature-name syntax. this flag can be given multiple times, and doing that enables all the features you specified. stacked, iconic πŸ‘‘
  • --all-features β€” activate every available feature of all selected packages. all of them. the whole buffet πŸ”₯
  • --no-default-features β€” do not activate the default feature of the selected packages. just… no defaults, thank you πŸŽ€

πŸ¦‹ Display Options

  • -v, --verbose β€” verbose output, duh. give it twice for "very verbose" output, which throws in the extra stuff like dependency warnings and build script output. may also be set with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). the way it just keeps talking πŸ’€
  • -q, --quiet β€” do not print cargo log messages. may also be set with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). silence, but make it chic 🀫
  • *--color *when β€” controls when colored output gets used. the valid values are:
    • auto (default): automatically detect if color support is available on the terminal.
    • always: always display colors. yes bestie, always 🌈
    • never: never display colors. monochrome moment.

may 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 looking for Cargo.toml in the current directory or any parent directory. it'll find it, don't worry 🫢
  • --frozen, --locked β€” either one of these flags demands that the Cargo.lock file is up-to-date. if the lock file is missing, or it needs updating, Cargo exits with an error. the --frozen flag also stops Cargo from trying to access the network to figure out if it's out-of-date.

these are for environments where you want to assert that Cargo.lock is up-to-date (like a CI build) or where you want to avoid network access entirely.

  • --offline β€” stops 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 try to proceed without the network if it can.
⚠️ beware, bestie: this can result in different dependency resolution than online mode. Cargo restricts itself to crates that are downloaded locally, even if a newer version might exist as indicated in the local copy of the index. see the cargo-fetch(1) command to download dependencies before going offline. plan ahead, it's giving ✨prepared✨

may 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 (https://rust-lang.github.io/rustup/overrides.html) for more info 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 given 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 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.
⚠️ 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 girlies only πŸ’…
  • -h, --help β€” prints help information. the classic. the icon. πŸŽ€
  • *-Z *flag β€” unstable (nightly-only) flags to Cargo. run cargo -Z help for the details. chaos mode, but documented chaos 🌈

🌟 ENVIRONMENT 🌟

see the reference (https://doc.rust-lang.org/cargo/reference/environment-variables.html) for the deets on environment variables that Cargo reads. iykyk ✨


πŸ’€ EXIT STATUS πŸ’€

  • 0: Cargo succeeded. slay πŸŽ‰
  • 101: Cargo failed to complete. not slay 😭

πŸ“ 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!! this was the Cargo manual's CARGO-METADATA(1) page, straight from the Cargo source docs, dated with that classic manpage footer energy. go forth and cargo metadata responsibly πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from