CARGO-METADATA(1)

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

πŸ’– CARGO-METADATA πŸ’–

okay bestie, welcome to cargo-metadata β€” it's a General Commands Manual situation, and it's literally about getting machine-readable metadata about the current package. no cap. let's get into it 🫢


✨ NAME ✨

cargo-metadata β€” Machine-readable metadata about the current package 🌸


πŸŽ€ SYNOPSIS πŸŽ€

cargo metadata [options]

that's it. that's the whole vibe. cargo metadata plus whatever options you feel like throwing at it πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so what it does is: it spits out JSON to stdout containing all the info about the workspace members and the resolved dependencies of the current package. like, everything. the whole picture. πŸ‘‘

⚠️ ok important one, listen up: the format of the output is subject to change in futures versions of Cargo. it is recommended that you include the --format-version flag to future-proof your code, so you actually get the output in the format you're expecting. for more on the expectations, go read "Compatibility" down below πŸ‘‡

also, if you're a Rust person and you want an actual API for reading the metadata instead of parsing JSON with your bare hands, 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 (yes, that means they can just happen to you, bestie, and that's allowed 😭):

  • Adding new fields β€” new fields will be added when needed. reserving this helps Cargo evolve without bumping the format version too often. iconic behaviour honestly.
  • Adding new values for enum-like fields β€” same energy as adding new fields. it keeps the metadata evolving without stagnation. growth mindset fr fr 🌱
  • 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 it's specified. so like... don't build your whole personality on them, okay? πŸ™ˆ

πŸŽ€ JSON format πŸŽ€

the JSON output has this format, and I'm putting it here exactly as it is because details matter πŸ’…:

        {
            /* 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 that's where part 1 leaves us, because this page is a whole saga and we are not done yet. more parts incoming 🌸✨

okay so we're mid-JSON and honestly? still iconic πŸ’… here's the rest of that source comment + the entire dependencies array, verbatim, no edits, because details are sacred:

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

quick vibe check on that one: source can literally be null, req falls back to "*" when there's no version requirement, kind is "dev", "build", or null for a normal dep, rename is null when it's not renamed, and path isn't even present unless it's a path dependency. registry: null = default registry = crates.io. iykyk 🌸


next up, the targets array β€” every kind, every crate type, every flag. and yes the doc, doctest and test booleans are all doing their own little thing 🫢

                       /* 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
                           }
                       ],
heads up bestie: required-features only shows up if you actually set it. no required features = the property just isn't there. don't go looking for it. πŸ™ˆ

okay now the part where your package shows its whole personality: features, manifest_path, metadata, publish, and every single manifest field it carries along πŸŽ€

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

the publish field is doing the most, lowkey: null = publish wherever you want, empty array = publishing is straight up forbidden. and metadata is null if you didn't set any. authors is an empty array if there are none. every other manifest-ish field (readme, repository, homepage, documentation) is null when it's not specified. simple as that ✨


and finally for this chunk: the workspace arrays and the big resolve graph, with the deps and dep_kinds stuff that handles renamed dependencies πŸ‘‘

               /* Array of members of the workspace.
                  Each entry is the Package ID for the package.
               */
               "workspace_members": [
                   "file:///path/to/my-package#0.1.0",
               ],
               /* Array of default members of the workspace.
                  Each entry is the Package ID for the package.
               */
               "workspace_default_members": [
                   "file:///path/to/my-package#0.1.0",
               ],
               // The resolved dependency graph for the entire workspace. The enabled
               // features are based on the enabled features for the "current" package.
               // Inactivated optional dependencies are not listed.
               //
               // This is null if --no-deps is specified.
               //
               // By default, this includes all dependencies for all target platforms.
               // The `--filter-platform` flag may be used to narrow to a specific
               // target triple.
               "resolve": {
                   /* Array of nodes within the dependency graph.
                      Each node is a package.
                   */
                   "nodes": [
                       {
                           /* The Package ID of this node. */
                           "id": "file:///path/to/my-package#0.1.0",
                           /* The dependencies of this package, an array of Package IDs. */
                           "dependencies": [
                               "https://github.com/rust-lang/crates.io-index#bitflags@1.0.4"
                           ],
                           /* The dependencies of this package. This is an alternative to
                              "dependencies" which contains additional information. In
                              particular, this handles renamed dependencies.
                           */
                           "deps": [
                               {
                                   /* The name of the dependency's library target.
                                      If this is a renamed dependency, this is the new
                                      name.
                                   */
                                   "name": "bitflags",
                                   /* The Package ID of the dependency. */
                                   "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.
                                           */

key deets on resolve: the enabled features come from the "current" package, inactivated optional deps aren't listed, it's null when you pass --no-deps, and by default it covers all dependencies for all target platforms β€” unless you use --filter-platform to narrow it down to one specific target triple. πŸ’–

and the deps array is the glow-up version of dependencies: same energy, extra info, and it's the one that actually handles renamed dependencies. dep_kinds was added in Cargo 1.40, ngl that's a fun little historical note 🫢

   "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, bestie, write these down πŸ“:


✨ OPTIONS ✨

πŸŽ€ Output Options

  • --no-deps β€” Only spill the tea about your workspace members, k? It won't go fetch the dependencies.
  • --format-version version β€” Pick which version of the output format you want. RN 1 is literally the only value that exists. That's it. That's the whole list.
  • --filter-platform triple β€” This filters the resolve output so it only includes dependencies for the target triple you hand it (https://doc.rust-lang.org/cargo/appendix/glossary.html#target). No flag? Then the resolve just includes every single target, all of them, the whole buffet.
Heads up though! The dependencies listed in the "packages" array still include all dependencies, no matter what. Each package definition is meant to be a totally unaltered reproduction of the info inside Cargo.toml. No edits, no glow-up, just the raw truth πŸ’…

🌸 Feature Selection

The feature flags let you control which features are enabled, bestie. When you give no feature options at all, the default feature gets activated for every single selected package. Periodt.

See the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options for way more details.

  • -F features, --features features β€” A space or comma separated list of features to activate. You can enable features of workspace members using the package-name/feature-name syntax. And yes, you can pass this flag multiple times β€” doing that enables all the specified features at once. Slay.
  • --all-features β€” Activate every available feature of all selected packages. Go big or go home πŸ”₯
  • --no-default-features β€” Do NOT activate the default feature of the selected packages. Sometimes you just don't want the default, and that's valid.

πŸ¦‹ Display Options

  • -v, --verbose β€” Use verbose output. Pass it twice for "very verbose" output, which throws in extra stuff like dependency warnings and build script output. You can also set this with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html).
  • -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).
  • --color when β€” Control when colored output gets used. Valid values are:
    • auto (default): Automatically detect if color support is available on the terminal.
    • always: Always display colors.
    • never: Never display colors.

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

πŸ’œ Manifest Options

  • --manifest-path path β€” Path to 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 up the family tree, basically.
  • --frozen, --locked β€” Either of these flags requires that the Cargo.lock file is up-to-date. If the lock file is missing, or if it needs to be updated, Cargo will exit 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 β€” Prevents Cargo from accessing the network for any reason at all. Without this flag, Cargo will stop with an error if it needs the network and the network isn't available. With this flag, Cargo will attempt to proceed without the network if it can.
Beware, babes πŸ’€ This may result in 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 cargo-fetch(1) to download dependencies before going offline. Plan ahead!

May also be specified 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 begins 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 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 things like where cargo looks by default for the project manifest (Cargo.toml), and also the directories searched 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 at https://github.com/rust-lang/cargo/issues/10098). So like, don't be surprised when stable says no πŸ™ˆ
  • -h, --help β€” Prints help information. Classic. Iconic. Always there for you.
  • -Z flag β€” Unstable (nightly-only) flags to Cargo. Run cargo -Z help for details.

🌈 ENVIRONMENT

See the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html for all the deets on the environment variables that Cargo reads. It's all over there, fr fr.


πŸ‘‘ EXIT STATUS

  • 0: Cargo succeeded. πŸŽ‰ Ate. No notes.
  • 101: Cargo failed to complete. 😭 It's giving sad girl hours.

πŸ’– EXAMPLES

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

🫢 SEE ALSO


and that's the whole page, bestie πŸŽ€βœ¨ This one's CARGO-METADATA(1), straight out of the Cargo manual β€” official Cargo documentation vibes only πŸ’… Go forth and pipe some JSON into jq, you absolute main character πŸ‘‘πŸ¦‹

πŸ“¦ where this page comes from