cargo-metadata(1)

名称

cargo-metadata — 关于当前包的机器可读元数据

概要

cargo metadata [选项]

描述

将包含当前包的工作空间成员和已解析依赖项信息的 JSON 输出到标准输出。

输出格式在未来版本的 Cargo 中可能会发生变化。建议包含 --format-version 标志以确保代码面向未来,确保输出格式符合您的预期。有关预期的更多信息,请参阅“兼容性”

有关用于读取元数据的 Rust API,请参阅 cargo_metadata crate

输出格式

兼容性

在相同的输出格式版本中,兼容性得以保持,但某些情况下除外。以下是不被视为不兼容的更改的非详尽列表

  • 添加新字段 — 将根据需要添加新字段。保留此功能有助于 Cargo 发展,而无需频繁更改格式版本。
  • 为类枚举字段添加新值 — 与添加新字段相同。它使元数据能够不断发展而不会停滞不前。
  • 更改不透明表示形式 — 某些字段的内部表示形式是实现细节。例如,与“源 ID”相关的字段被视为不透明标识符,用于区分包或源。除非另有说明,否则消费者不应依赖这些表示形式。

JSON 格式

JSON 输出采用以下格式

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

               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
                }
            ],
            /* 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
                }
            ],
            /* 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 <[email protected]>"
            ],
            /* 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://www.rust-lang.net.cn",
            /* The documentation value from the manifest or null if not specified. */
            "documentation": "https://doc.rust-lang.net.cn/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",
    ],
    // 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#[email protected]"
                ],
                /* 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#[email protected]"
                        /* 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
            }
        }
    }
}

注意

  • 有关 "id" 字段语法的说明,请参阅参考中的包 ID 规范

选项

输出选项

--no-deps
仅输出有关工作空间成员的信息,而不获取依赖项。
--format-version 版本
指定要使用的输出格式版本。当前,1 是唯一可能的值。
--filter-platform 三元组
这会过滤 resolve 输出,使其仅包含给定目标三元组的依赖项。如果没有此标志,则解析将包含所有目标。

请注意,“packages”数组中列出的依赖项仍然包含所有依赖项。每个包定义都旨在完整地复制 Cargo.toml 中的信息。

功能选择

功能标志允许您控制启用哪些功能。如果未给出功能选项,则会为每个选定的包激活 default 功能。

有关更多详细信息,请参阅功能文档

-F 功能
--features 功能
以空格或逗号分隔的要激活的功能列表。可以使用 package-name/feature-name 语法启用工作空间成员的功能。此标志可以指定多次,这将启用所有指定的功能。
--all-features
激活所有选定包的所有可用功能。
--no-default-features
不要激活选定包的 default 功能。

显示选项

-v
--verbose
使用详细输出。可以指定两次以获得“非常详细”的输出,其中包括额外的输出,例如依赖项警告和构建脚本输出。也可以使用 term.verbose 配置值指定。
-q
--quiet
不打印 cargo 日志消息。也可以使用 term.quiet 配置值指定。
--color 何时
控制何时使用彩色输出。有效值

  • auto(默认):自动检测终端上是否支持彩色输出。
  • always:始终显示颜色。
  • never:从不显示颜色。

也可以使用 term.color 配置值指定。

清单选项

--manifest-path 路径
Cargo.toml 文件的路径。默认情况下,Cargo 会在当前目录或任何父目录中搜索 Cargo.toml 文件。
--locked
断言使用的依赖项和版本与最初生成现有 Cargo.lock 文件时完全相同。在出现以下任一情况时,Cargo 将退出并报错

  • 缺少锁定文件。
  • 由于依赖项解析不同,Cargo 尝试更改锁定文件。

它可以在需要确定性构建的环境中使用,例如在 CI 管道中。

--offline
阻止 Cargo 出于任何原因访问网络。如果没有此标志,Cargo 在需要访问网络而网络不可用时将停止并报错。使用此标志,Cargo 将尝试在可能的情况下在没有网络的情况下继续操作。

请注意,这可能会导致与在线模式不同的依赖项解析。Cargo 将限制自己使用本地下载的 crate,即使索引的本地副本中可能指示有更新的版本。请参阅 cargo-fetch(1) 命令以下载依赖项,然后再进入离线模式。

也可以使用 net.offline 配置值指定。

--frozen
等效于同时指定 --locked--offline

通用选项

+工具链
如果使用 rustup 安装了 Cargo,并且 cargo 的第一个参数以 + 开头,则它将被解释为 rustup 工具链名称(例如 +stable+nightly)。有关工具链覆盖如何工作的更多信息,请参阅 rustup 文档
--config 键=值路径
覆盖 Cargo 配置值。参数应采用 键=值 的 TOML 语法,或作为额外配置文件的路径提供。此标志可以指定多次。有关更多信息,请参阅命令行覆盖部分
-C 路径
在执行任何指定的操作之前更改当前工作目录。例如,这会影响 cargo 默认查找项目清单 (Cargo.toml) 的位置,以及搜索 .cargo/config.toml 的目录。此选项必须出现在命令名称之前,例如 cargo -C path/to/my-project build

此选项仅在夜间版通道上可用,并且需要 -Z unstable-options 标志才能启用(请参阅 #10098)。

-h
--help
打印帮助信息。
-Z 标记
Cargo 的不稳定(仅限 nightly 版本)标记。运行 cargo -Z help 获取详细信息。

环境

有关 Cargo 读取的环境变量的详细信息,请参阅参考

退出状态

  • 0:Cargo 成功。
  • 101:Cargo 未能完成。

示例

  1. 输出当前包的 JSON 信息

    cargo metadata --format-version=1
    

另请参阅

cargo(1)cargo-pkgid(1)包 ID 规范JSON 消息