Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve a cyclic dependency in Cargo?

I have the crates oauth2 (v4.1.0) and sqlx[json] (v0.5.5):

[dependencies]
oauth2 = "4.1.0"
sqlx = { version = "0.5.5", features = ["json"] }

When attempting to build, I am getting the following error:

error: cyclic package dependency: package `ahash v0.7.4` depends on itself. Cycle:
package `ahash v0.7.4`
    ... which is depended on by `hashbrown v0.11.2`
    ... which is depended on by `indexmap v1.7.0`
    ... which is depended on by `serde_json v1.0.64`
    ... which is depended on by `wasm-bindgen v0.2.74`
    ... which is depended on by `js-sys v0.3.51`
    ... which is depended on by `getrandom v0.2.3`
    ... which is depended on by `ahash v0.7.4`

This only happens when I activate the json feature flag on sqlx. How do I troubleshoot this kind of problem? Are there any workarounds to make these libs/features work together? What are the alternatives?

like image 911
Mauro Insacco Avatar asked Jul 15 '21 19:07

Mauro Insacco


People also ask

How do you get rid of dependency cycle?

To reduce or eliminate circular dependencies, architects must implement loose component coupling and isolate failures. One approach is to use abstraction to break the dependency chain. To do this, you introduce an abstracted service interface that delivers underlying functionality without direct component coupling.

What is cyclic dependency in Java?

A circular or cyclic dependency is a situation where two or more independent modules or components rely on each other to function properly. This is referred to as mutual recursion. Circular dependency generally occurs in a modular framework while defining a dependency between modules or components.


1 Answers

After trying a few things back and forth, the only solution I found, was to update the lockfile to the latest version, using:

cargo update

After that, cargo clean && cargo build worked like a charm! Seems like some older patch versions seem to have conflicts, but the changes in the Cargo.lock were too big to track down, which crate combination it was. I hope that helps anybody else!

EDIT: After more communication and searching, I got referred to an issue directly at the ahash github project: https://github.com/tkaitchuck/aHash/issues/95.

According to that, the actual official "workaround" was / is, to pin the indexmap crate to:

indexmap = "=1.6.2"

However when looking into my Cargo.lock file, it seems to work with now:

indexmap = "1.7"
like image 184
elbartus Avatar answered Oct 12 '22 12:10

elbartus