Skip to content

Cargo metadata should include the value of proc-macro from a lib target #11508

@celinval

Description

@celinval

Problem

Hi, I currently use cargo metadata to filter crates that are proc-macros by checking the kind in the target metadata, and looking for "proc-macro". However, I noticed that this isn't very reliable. If the target spec includes a crate-type, this check no longer works, and I cannot find any other way to get that information.

E.g.: If I run cargo metadata using the following Cargo.toml.

[package]
name = "proc_macro_lib"
version = "0.1.0"
edition = "2021"

[lib]
name = "lib"
crate-type = ["lib"]
proc-macro = true
path = "src/lib.rs"

I get the following metadata:

{
    "metadata": null,
    "packages": [
        {
            "authors": [],
            "categories": [],
            "default_run": null,
            "dependencies": [],
            "description": null,
            "documentation": null,
            "edition": "2021",
            "features": {},
            "homepage": null,
            "id": "proc_macro_lib 0.1.0 (path+file:///tmp/lib-pmacro)",
            "keywords": [],
            "license": null,
            "license_file": null,
            "links": null,
            "manifest_path": "/tmp/lib-pmacro/Cargo.toml",
            "metadata": null,
            "name": "proc_macro_lib",
            "publish": null,
            "readme": null,
            "repository": null,
            "rust_version": null,
            "source": null,
            "targets": [
                {
                    "crate_types": [
                        "lib"
                    ],
                    "doc": true,
                    "doctest": true,
                    "edition": "2021",
                    "kind": [
                        "lib"
                    ],
                    "name": "lib",
                    "src_path": "/tmp/lib-pmacro/src/lib.rs",
                    "test": true
                }
            ],
            "version": "0.1.0"
        }
    ],
    "resolve": {
        "nodes": [
            {
                "dependencies": [],
                "deps": [],
                "features": [],
                "id": "proc_macro_lib 0.1.0 (path+file:///tmp/lib-pmacro)"
            }
        ],
        "root": "proc_macro_lib 0.1.0 (path+file:///tmp/lib-pmacro)"
    },
    "target_directory": "/tmp/lib-pmacro/target",
    "version": 1,
    "workspace_members": [
        "proc_macro_lib 0.1.0 (path+file:///tmp/lib-pmacro)"
    ],
    "workspace_root": "/tmp/lib-pmacro"
}

I honestly don't know why someone would do that, but at this point I just want to make sure we can gracefully handle cases like that.

Proposed Solution

I can think of two options:

  1. Cargo should include the value of the proc-macro field as part of the target metadata, like it does with doc, test, doctest.
  2. Add "proc-macro" to the kind field whenever the flag is set to true.

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`Command-metadataS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions