Cargo Confusion With Features And Target Specific Dependencies

I'm encountering some strange behavior with Cargo when trying to use different features for a dependency depending on the target. This is what I'm attempting:

[target.'cfg(target_arch = "wasm32")'.dependencies.gfx-backend-gl]
path = "../src/backend/gfx-backend-gl"
optional = true

[target.'cfg(windows)'.dependencies.gfx-backend-gl]
path = "../src/backend/gfx-backend-gl"
features = ["wgl"]
optional = true

[target.'cfg(unix)'.dependencies.gfx-backend-gl]
path = "../src/backend/gfx-backend-gl"
features = ["surfman"]
optional = true

The goal is to include gfx-backend-gl with a different feature enabled depending on the target. The strange behavior is that when I compile for wasm32 with this setup, it includes the gfx-backend-gl dependency, but with the wgl and surfman features as well, which doesn't make a lot of sense because those features aren't included in the dependency for the matching target.

It would work fine if each dependency was on a different crate, but because they are the same it seems to want to merge the features. Is this a bug?


Hmm, I haven't tried it yet, but maybe if I name each dependency different and use the rename feature to point them all to the same crate?

That is a known issue with how features in cargo works: they are always unified across all specified dependencies, ignoring any target-config.

In nightly there's now an unstable feature that tries to fix this: -Zfeatures.
See the tracking issue and the nightly documentation of this option.

3 Likes

Great, thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.