Patching dependency versions with an in-development version

I'm in a situation where I want to patch a particular dependency with a local copy but cargo doesn't seem to want to use it because the local copy has a -dev suffix.

Given the following:

  • One of my dependencies has target-crate = "^0.9.0" in its Cargo.toml
  • the most recently released version of target-crate is 0.9.2
  • I want to override this with target-crate = { path = "./path/to/local/version" }, where the local version is 0.9.3-dev
  • the -dev suffix is automatically added by cargo release, and I don't want to remove the -dev suffix to make it 0.9.3
  • we get the 0.9.3-dev version number from a const VERSION: &str = env!("CARGO_PKG_VERSION") inside target-crate

Here are some things I've tried...

[package]
name = "generated-crate"

[dependencies]
some-dependency = "..." 
target-crate = "0.9.3-dev"

[patch.crates-io]
target-crate = { path = "./path/to/local/version" }

Fails with

error: failed to select a version for the requirement `target-crate = "^0.9.3-dev"`
candidate versions found which didn't match: 0.9.2, 0.9.1, 0.9.0, ...
location searched: crates.io index
required by package `generated-crate v0.0.0 (...)`

I also tried replacing the version under [dependencies] with the override itself.

[dependencies]
some-dependency = "..." 
target-crate = { path = "./path/to/local/version" }

[patch.crates-io]
target-crate = { path = "./path/to/local/version" }

But that results in compile errors because the generated code expects an object implementing target_crate::SomeTrait from target-crate v0.9.3-dev, but it finds an object implementing target_crate::SomeTrait from the incompatible target-crate v0.9.2... That implies the version used by some-dependency is still 0.9.2 and wasn't patched.

Has anyone else tried using the [patch] section like this?


To add a bit more context, "my crate" is a Rust crate I'm generating and compiling dynamically, target-crate contains common code that all generated crates use, and all this version trickery is to allow developers to hack on target-crate locally without needing to publish their changes to crates.io.

"^0.9.0" is not semver-compatible with pre-release versions (it can take 0.9.3, but not 0.9.3-blah). You need to make the local copy to declare being version 0.9.2 or 0.9.3. Cargo won't use the patch if the version isn't compatible.

Yeah, I had a feeling that would be the case but was hoping there would be some sort of workaround or hack that will let me keep both the -dev versions and local overrides.

This version number gets embedded in the compiled library so we know which version of the tool generated it. That way when you see a -dev suffix you can immediately tell whether something was generated by an official release or not.

You can use 0.9.2+dev, which isn't a pre-release. Semver ignores +.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.