Can Cargo warn about linking multiple versions of a crate?

This morning, our application quietly stopped uploading statistics to DataDog.

We're using the metrics crate, which requires users to call this setup function. The docs say:

Any metrics recorded before the call to set_recorder occurs will be completely ignored.

The function sets a global variable. There's :skull: shared mutable state :skull: inside the crate.

So if you accidentally get two versions of the metrics crate built into your application, like we did, whichever one receives the set_recorder call will get metrics, and the other will silently ignore all metrics!

I'm thinking about ways to help others avoid this. Is there anything the metrics authors could do to make Cargo warn at build time if this happens, maybe something like the links field, but for crates instead of native libraries? Or something we could do as users of metrics?

There is https://rust-lang.github.io/rust-clippy/master/#multiple_crate_versions. However, this warns on all duplicated crates, not just ones that matter, so it might be noisy.

IRLO post proposing a more precise mechanism:

4 Likes

You can use the links field. It doesn't do anything related to native libraries, any crate can use it.