0

I have a project A and B like the following, B depend on A. A is a git submoudle.

repo
|- contrib
|--- A
|----- components
|------- package A
|------- package B
|----- Cargo.toml
|- B
|--- Cargo.toml

A have dependency like the following

// A/Cargo.toml
serde = "1.0"
serde_derive = "1.0"
serde_ignored = "0.1"
serde_json = "1.0"
tempfile = "3.0"
lazy_static = "1.3"

I want B have the same dependency version like A, like the following

// B/Cargo.toml

compA = { path = "../A/components/compA" }
compB = { path = "../A/components/compB" }

serde = "1.0"
serde_derive = "1.0"
serde_ignored = "0.1"
serde_json = "1.0"
tempfile = "3.0"
lazy_static = "1.3"

However, once A is updated, it may updates its dependencies. So A may use serde = "2.0" later. So how can B "automaticly" update its serde to 2.0?

I think I need something that says "B depends the serde which A is depending".

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
calvin
  • 2,125
  • 2
  • 21
  • 38
  • 1
    This is not a good idea: if the version is updated, and the new version isn't compatible, it may break the package. The very existence of SemVer is to prevent accidental breakage. – Chayim Friedman May 24 '22 at 16:36
  • If A use serde = 1.0 and B use serde = 2.0, since B depends A, then A and B are using different versions of serde, I think it may cause problems? – calvin May 24 '22 at 16:43
  • It definitely can, but only if they try to use the same types. If each uses serde for its own, that should be fine. – Chayim Friedman May 24 '22 at 16:49
  • Does this answer your question? [Import version of dependency which other dependency requires](https://stackoverflow.com/questions/63482188/import-version-of-dependency-which-other-dependency-requires) – Chayim Friedman May 24 '22 at 16:50
  • This answer gives a short example on what Cargo tries to do behind the scenes to resolve possible dependency problems: https://stackoverflow.com/questions/71946327/what-does-this-mean-this-is-precisely-because-a-library-should-not-be-determin/71946527#71946527 – Jeremy Meadows May 24 '22 at 16:55
  • @ChayimFriedman I am not sure if this can answer my question, since it involve something I am not familiar with now. I'd like to take a look at those first. – calvin May 24 '22 at 16:57
  • Why not have A expose/reexport those crates, and B use them via those exports? – eggyal May 25 '22 at 07:38
  • @eggyal do you mean something like `extern crate xxx`? – calvin May 25 '22 at 07:39
  • They mean in `A` to do `pub use serde;` etc. – Chayim Friedman May 25 '22 at 07:40
  • But `pub extern crate` also works. – Chayim Friedman May 25 '22 at 07:41

1 Answers1

2

I think I need something that says "B depends the serde which A is depending".

If they are part of the same Cargo workspace, then (since 1.64) you should use workspace dependencies.

Otherwise, A should expose/re-export the relevant crates (or parts thereof), for example:

pub use serde; // etc

and then B should use those exports rather than declaring each dependency in its Cargo.toml:

use A::serde; // etc
eggyal
  • 122,705
  • 18
  • 212
  • 237