Hello everyone,
I am trying to deepen my understanding of how workspaces work during compilation, and have the following example I am puzzled with
Imagine the following workspace:
[workspace]
resolver = "3"
members = ["math", "add-example", "subtract-example"]
[workspace.dependencies]
math = { path = "./math" }
The add-example, and subtract-example are binary crates, and both depend on the math crate via different features. Here are the Cargo.toml files for each individual crate:
# Path: math/Cargo.toml
[package]
name = "math"
version = "0.1.0"
edition = "2024"
[features]
add = []
subtract = []
[package]
name = "add-example"
version = "0.1.0"
edition = "2024"
[dependencies]
math = { workspace = true, features = ["add"] }
[package]
name = "subtract-example"
version = "0.1.0"
edition = "2024"
[dependencies]
math = { workspace = true, features = ["subtract"] }
This is a simplified example of course, but has the following behavior;
If I compile the workspace then build add-example using the --package option, some compilation seems to occur for the add-example crate, as can be seen below.
romaincomeau@cerberus workspace-example % cargo build
Compiling math v0.1.0 (/Users/romaincomeau/sandbox/workspace-example/math)
Compiling add-example v0.1.0 (/Users/romaincomeau/sandbox/workspace-example/add-example)
Compiling subtract-example v0.1.0 (/Users/romaincomeau/sandbox/workspace-example/subtract-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
romaincomeau@cerberus workspace-example % cargo build -p add-example
Compiling math v0.1.0 (/Users/romaincomeau/sandbox/workspace-example/math)
Compiling add-example v0.1.0 (/Users/romaincomeau/sandbox/workspace-example/add-example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
romaincomeau@cerberus workspace-example %
This is surprising to me, and may be the reason why my organization workspace is quickly taking gigabytes of disk, for a relatively small workspace.
Is it the case that when I build add-example only, after building the workspace, create a smaller build which does not include the features required by subtract-example or else what is the reason behind it?
Thank you in advance for your time ![]()