CARGO-METADATA(1)

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

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

okay so you opened CARGO-METADATA(1), the General Commands Manual page, and honestly? slay. this is the one that gives you machine-readable metadata about the current package. let's get into it ๐Ÿซถ


โœจ NAME โœจ

cargo-metadata โ€” Machine-readable metadata about the current package


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

cargo metadata [options]

๐ŸŒธ DESCRIPTION ๐ŸŒธ

It spits out JSON to stdout, and that JSON is packed with info about the workspace members and the resolved dependencies of the current package. like, the whole tea. โ˜•

โš ๏ธ heads up bestie: the format of the output is subject to change in futures versions of Cargo. so pls pls pls include the --format-version flag to future-proof your code, so the output is actually in the format you're expecting. for more on that whole situation, go read "Compatibility" 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 <https://crates.io/crates/cargo_metadata>. iconic of them honestly. ๐Ÿ’…


๐Ÿ‘‘ OUTPUT FORMAT ๐Ÿ‘‘

๐ŸŒˆ Compatibility ๐ŸŒˆ

Within the same output format version, the 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 Cargo when these happen, they're allowed):

  • Adding new fields โ€” New fields will be added when needed. Reserving this helps Cargo evolve without bumping the format version too often. growth mindset fr fr โœจ
  • Adding new values for enum-like fields โ€” Same as adding new fields. It keeps metadata evolving without stagnation. no stagnation era allowed ๐Ÿ’€
  • 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. iykyk โ€” if it says opaque, treat it as opaque, don't peek behind the curtain ๐Ÿซฃ

๐Ÿฆ‹ 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"

lowkey the id field is the main character here โ€” it's the Package ID for referring to the package within the document and as the --package argument to many commands. remember it, love it, use it. ๐Ÿ’œ

and for the source ID: it's null for path dependencies and workspace members, but for other dependencies it's a string in one of those three formats โ€” registry+URL, git+URL, or sparse+URL. don't rely on the inner representation tho, per the Compatibility tea above. ๐Ÿ™ˆ

okay part 1 done and we're literally only getting started โ€” more coming โœจ

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


---

okay bestie, that was the `dependencies` array and she **ate** ๐Ÿ’… every single dep in the package's manifest gets an entry, and here's the tea on each one:

- `name` โ€” literally just the dependency's name.
- `source` โ€” the source ID of the dep, and yeah it **may be `null`**, see the package source description. And that `+` suffix situation? <pink>not explicitly defined</pink> fr fr โ€” it can change between Cargo versions, it might not line up with registry definitions in a config file, and new source kinds with different `+` prefixed identifiers may show up in the future. So don't build your whole personality on it ๐Ÿ™ˆ
- `req` โ€” the version requirement. No version requirement at all? Then it's literally `"*"`. periodt.
- `kind` โ€” `"dev"`, `"build"`, or `null` for a normal dependency.
- `rename` โ€” if the dep got renamed, this is the new name (a string). `null` if she wasn't renamed.
- `optional` โ€” boolean, is it optional or not.
- `uses_default_features` โ€” boolean, default features on or off.
- `features` โ€” array of the features that are enabled.
- `target` โ€” the target platform for the dep, `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 (she only shows up when she's invited โœจ).
- `registry` โ€” a string of the URL of the registry this dep comes from. If it's not specified or `null`, the dep is from the default registry (`crates.io`). ๐Ÿ‘‘

---

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

---

okay so `targets` is the array of Cargo targets and there is **so much** going on here, buckle up ๐ŸŽ€

- `kind` โ€” array of target kinds. Here's the whole menu:
  - `lib` targets list the `crate-type` values from the manifest like `"lib"`, `"rlib"`, `"dylib"`, `"proc-macro"`, etc. (default is `["lib"]`)
  - binary is `["bin"]`
  - example is `["example"]`
  - integration test is `["test"]`
  - benchmark is `["bench"]`
  - build script is `["custom-build"]`
- `crate_types` โ€” array of crate types. `lib` and example libraries list the manifest's `crate-type` values like `"lib"`, `"rlib"`, `"dylib"`, `"proc-macro"`, etc. (default `["lib"]`), and **all** other target kinds are `["bin"]`.
- `name` โ€” the name of the target. Fun fact for the lib girlies: for lib targets, dashes get replaced with underscores. iconic.
- `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. <hotpink>This property is not included if no required features are set</hotpink>, she's shy ๐Ÿ™ˆ
- `doc` โ€” whether the target should be documented by `cargo doc`.
- `doctest` โ€” whether doc tests are enabled **and** the target is compatible with doc testing.
- `test` โ€” whether the target should be built and run with `--test`.

---

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


---

bestie we're in the **manifest glow-up era** now ๐Ÿ’– quick rundown:

- `features` โ€” the set of features defined for the package, and 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. `null` if no metadata is specified.
- `publish` โ€” list of registries this package may be published to. <yellow>Publishing is unrestricted if `null`, and forbidden if an empty array.</yellow> big difference, don't mix them up.
- `authors` โ€” array from the manifest. Empty array if no authors specified.
- `categories` โ€” array from the manifest.
- `default_run` โ€” optional string, the default binary picked by `cargo run`.
- `rust_version` โ€” optional string, the minimum supported rust version.
- `keywords` โ€” array from the manifest.
- `readme` โ€” the readme value from the manifest, or `null` if not specified.
- `repository` โ€” the repository value from the manifest, or `null` if not specified.
- `homepage` โ€” the homepage value from the manifest, or `null` if not specified.
- `documentation` โ€” the documentation value from the manifest, or `null` if not specified.
- `edition` โ€” the default edition of the package. <lavender>Note that individual targets may have different editions</lavender>, so this is the vibe, not the whole story.
- `links` โ€” optional string, the name of a native library the package is linking to.

then the package object closes up, and `workspace_members` + `workspace_default_members` are arrays of workspace members where **each entry is the Package ID** for the package. ๐Ÿซถ

---

and now... the moment you've all been waiting for โœจ the `resolve` block. It's the **resolved dependency graph for the entire workspace**, and she's the main character of this whole page, no cap. Some important lore before we read it:

- 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 it includes all dependencies for **all** target platforms, and the `--filter-platform` flag narrows it down to a specific target triple.

okay now read her:

// 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.
                                           */
                                           "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 spit out info about the workspace members, no cap, and don't go fetch the dependencies. just the members, nothing else ๐Ÿ’…
  • --format-version version โ€” pick which version of the output format you want. Right now 1 is literally the only possible value, so like... it's a one-option menu. Choose wisely. ๐ŸŽ€
  • --filter-platform triple โ€” this filters the resolve output so it only includes dependencies for the given target triple (see the glossary at https://doc.rust-lang.org/cargo/appendix/glossary.html#target). Without this flag, the resolve includes all targets. all of them. every single one. ๐Ÿฆ‹
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 information inside Cargo.toml. So don't come for the packages array expecting it to be filtered, because it's not. ๐Ÿ’€

๐ŸŒธ Feature Selection ๐ŸŒธ

The 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. defaults stay winning, fr fr โœจ

For the full deets, 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. You can enable features of workspace members with the package-name/feature-name syntax. This flag can be specified multiple times, and doing so enables all the specified features. stack 'em up ๐Ÿงš
  • --all-features โ€” activate every available feature of all selected packages. ALL OF THEM. periodt. ๐Ÿ‘‘
  • --no-default-features โ€” do NOT activate the default feature of the selected packages. the default feature said "not today" ๐Ÿ™ˆ

๐ŸŒˆ Display Options ๐ŸŒˆ

  • -v, --verbose โ€” use verbose output. You can specify it twice for "very verbose" output, which includes extra output like dependency warnings and build script output. May also be specified with the term.verbose config value at https://doc.rust-lang.org/cargo/reference/config.html. twice = very verbose, that's the vibe check ๐Ÿ”ฅ
  • -q, --quiet โ€” do not print cargo log messages. May also be specified with the term.quiet config value at https://doc.rust-lang.org/cargo/reference/config.html. shhh ๐Ÿคซ
  • --color when โ€” control when colored output is used. Valid values are:
    • auto (default): automatically detect if color support is available on the terminal. she's perceptive
    • always: always display colors. maximalism ๐Ÿ’…
    • never: never display colors. monochrome moment

May also be specified with the term.color config value at https://doc.rust-lang.org/cargo/reference/config.html. ๐ŸŽ€

๐Ÿฆ‹ Manifest Options ๐Ÿฆ‹

  • --manifest-path path โ€” the path to the Cargo.toml file. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. it goes looking, 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 comes up:
    • The lock file is missing. ๐Ÿ’€ gone. vanished.
    • Cargo attempted to change the lock file due to a different dependency resolution. ๐Ÿ’€ it tried to switch things up

This may be used in environments where deterministic builds are desired, such as in CI pipelines. CI girlies, this one's for you โญ

  • --offline โ€” prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access 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 cargo โœˆ๏ธ
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 before you disconnect, fr fr ๐Ÿซถ

May also be specified with the net.offline config value at https://doc.rust-lang.org/cargo/reference/config.html.

  • --frozen โ€” equivalent to specifying both --locked and --offline. two-for-one, iconic โœจ
  • --lockfile-path PATH โ€” changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing --lockfile-path will ignore the existing lockfile at the default path, and instead will either use the lockfile from PATH, or write a new lockfile into the provided PATH if it doesn't exist. This flag can be used to run most commands in read-only directories, writing the lockfile into the provided PATH. read-only dir? no problem, we relocate ๐Ÿ’…
This option is only available on the nightly channel (see https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the -Z unstable-options flag to enable (see #14421 at https://github.com/rust-lang/cargo/issues/14421). nightly only, no cap ๐Ÿ‘‘

๐Ÿ’œ Common Options ๐Ÿ’œ

  • +toolchain โ€” if Cargo has been installed with rustup, and the first argument to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +nightly). See the rustup documentation at https://rust-lang.github.io/rustup/overrides.html for more info about how toolchain overrides work. the plus sign is doing a LOT of work here ๐Ÿงš
  • --config KEY=VALUE or PATH โ€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or provided 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 information. overriding, but make it TOML ๐ŸŒธ
  • -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 example cargo -C path/to/my-project build. before the command name, bestie, don't forget ๐Ÿซถ
This option is only available on the nightly channel (see https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the -Z unstable-options flag to enable (see #10098 at https://github.com/rust-lang/cargo/issues/10098). nightly only again, sorryyy ๐Ÿ’€
  • -h, --help โ€” prints help information. when in doubt, ask for help ๐Ÿ’…
  • -Z flag โ€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details. the forbidden switches ๐Ÿ‘€ โญ

๐ŸŒฟ ENVIRONMENT ๐ŸŒฟ

See the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html for details on the environment variables that Cargo reads. yes she's nosy, she reads them all ๐Ÿฆ‹


๐Ÿ’– EXIT STATUS ๐Ÿ’–

  • 0: Cargo succeeded. we ate. ๐ŸŽ‰
  • 101: Cargo failed to complete. the 101 jump scare ๐Ÿ˜ญ

โœจ EXAMPLES โœจ

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

that's it, that's the whole recipe ๐ŸŽ€


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


And that's a wrap on CARGO-METADATA(1), bestie โ€” this page is filed under the CARGO-METADATA(1) footer, so that's the name you're looking at, and if you ever need to cite her, she's versioned and dated like every good icon. go forth and pipe JSON into jq, you've got this ๐Ÿ’…โœจ ๐Ÿซถ

๐Ÿ“ฆ where this page comes from