Sync root and workspaces dependencies versions

At some point, a Rust project may become big and leveraging the workspaces feature may be a good thing to do.

Working on a video game with a client-server architecture, I quickly ran into the need to sync 3 crates dependencies version:

  • The lib shared (root) crate
  • The client crate
  • The server crate

The Cargo Book explains how you can inherit a dependency from a workspace.

Basically, you can do this:
In the root package

[package]
name = "foo"
version = "0.2.0"

[workspace]
members = ["bar", "baz"]

[workspace.dependencies]
regex = "1.10.6"

In the workspace crates (valid for bar and baz)

[package]
name = "bar"
version = "0.2.0"

[dependencies]
regex.workspace = true # Will inherit the regex dependency from foo

But what if you also need the regex dependency in your root(foo) package?
This would be ugly and error-prone as it could lead to the usage of two different versions inside your root package and the workspace ones:

[package]
name = "foo"
version = "0.2.0"

[dependencies]
regex = "1.10.6" # Defines the foo regex dependency

[workspace]
members = ["bar", "baz"]

[workspace.dependencies]
regex = "1.10.6" # Defines the workspace regex dependency

AFAIK, The Cargo Book doesn't show any example on how to sync the root package dependencies with the workspace's ones.
The key thing to understand is that the root package is in the workspace, so this is the way to go if you need regex into foo, bar and baz packages:

[package]
name = "foo"
version = "0.2.0"

[dependencies]
regex.workspace = true # Will inherit the regex dependency from workspace!

[workspace]
members = ["bar", "baz"]

[workspace.dependencies]
regex = "1.10.6"

Comments

Be the first to post a comment!

Add a comment

Preview