calvin
calvin

Reputation: 2935

How to let a package use the same version of dependencies as B?

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".

Upvotes: 0

Views: 1636

Answers (1)

eggyal
eggyal

Reputation: 125865

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

Upvotes: 2

Related Questions