Skip to content

Allow limiting required crate-types of dependencies #11232

Open
@mystor

Description

@mystor

Problem

When depending on a crate which declares it supports multiple crate types in its [lib] section, cargo will always build all of the specified crate types when building the dependency. This means that a crate which specifies something like the following in its Cargo.toml will always build a cdylib and staticlib when built as a dependency, even if we're only going to be using the normal lib dependency.

[lib]
crate-type = ["cdylib", "staticlib", "lib"]

This leads to wasted effort building unnecessary artifacts for dependencies, and can also lead to issues when the configuration used to build the lib would cause building the cdylib or staticlib to fail to link for whatever reason (e.g. due to linker flags).

Proposed Solution

It would be nice if there was a way to specify which crate types you are depending on from your dependencies when declaring a dependency in Cargo.toml, similar to how you can specify required feature flags.

I imagine this acting in a similar way to features, where when a dependency is declared without a crate-type specification, it implicitly requires the crate types from the dependency's Cargo.toml, and a dependency with crate-type only requires the specified crate types.

This might look something like the following:

[dependencies.mycrate]
version = "1.0.0"
crate-type = ["lib"]

Notes

There may be some overlap between this and other work for artifact dependencies (#9096), although I am not familiar enough with the work in that area to know if it already covers this use-case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-crate-typesArea: crate-type declaration (lib, staticlib, dylib, cdylib, etc.)C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-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