I want to build a crate multiple times for different versions of a dependency.
The first idea I had was defining an optional dependency for each version in Cargo.toml
, e.g:
[dependencies]
pulldown-cmark_0_9_2 = { package = "pulldown-cmark", version = "=0.9.2", optional=true }
pulldown-cmark_0_8_0 = { package = "pulldown-cmark", version = "=0.8.0", optional=true }
There are several things I dislike about this approach:
-
It requires me to define imports as follows (which is just cumbersome and not DRY):
#[cfg(any(feature = "pulldown-cmark_0_8_0"))] use pulldown_cmark_0_8_0 as pulldown_cmark; #[cfg(any(feature = "pulldown-cmark_0_9_2"))] use pulldown_cmark_0_9_2 as pulldown_cmark;
-
If the API changes between versions the
cfg
attributes become very cumbersome ... ideally I could just define something like#[cfg(dep_version(pulldown_cmark >= "0.8.0"))]
. -
For some reason
cargo
cannot handle multiple dependencies on different minor versions of a crate?
The following fails witherror: failed to select a version for `pulldown-cmark`
for me:[dependencies] pulldown-cmark_0_9_2 = { package = "pulldown-cmark", version = "=0.9.2", optional=true } pulldown-cmark_0_9_1 = { package = "pulldown-cmark", version = "=0.9.1", optional=true }
So I am starting to think it might make more sense to automate this with an external build script that runs cargo add
but that does feel like a hack. This seems like a problem somebody would have already solved but the search engines are failing me. Do you have an idea? Ideally the build artifact names would contain the version number of the dependency.