Sub topic 5
One Cargo.lock and one target directory
This chapter assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and have run cargo build. It covers the two artifacts every workspace shares: the lock file and the build output directory.
One lock file for every member
A workspace has exactly one Cargo.lock, and it sits next to the root Cargo.toml. Members do not get their own lock files. If you find a Cargo.lock inside crates/textkit/, Cargo ignores it.
The lock file records the exact version of every dependency in the whole workspace. Two consequences follow:
- If
textkitandappboth depend onserde, they resolve to the same version. Cargo does not pick one version for each crate. - Updating a dependency affects every member that uses it.
cargo update -p serdechanges the single entry for all of them.
If two members need incompatible major versions of a crate, such as rand 0.7 and rand 0.8, the lock file holds both. Cargo only unifies versions that are semver-compatible.
Commit Cargo.lock to version control when the workspace contains binaries. For a workspace of libraries only, committing it is optional, but many teams do it anyway so CI builds are reproducible.
One target directory
Build output goes to a single target/ directory at the workspace root. Running cargo build from inside crates/app/ still writes to <root>/target/, not crates/app/target/.
Sharing the directory has two practical benefits:
- A dependency compiled once is reused by every member that needs it. Building ten crates that all use
serdecompilesserdeonce. - When
appdepends ontextkit, the compiledtextkitis already there and is not rebuilt forapp.
You can see the layout after building the example workspace:
cargo build --workspace
ls target/debugYou should see the app executable, the deps/ directory with compiled libraries, and the .fingerprint/ directory Cargo uses to decide what needs rebuilding.
How a build flows
Common problems
Lock file churn. Adding a dependency to any member can modify Cargo.lock. Review the lock file diff in pull requests, because a change in one crate may update versions used by others.
Cross-member cache invalidation. All members share one target/, so a change to a shared dependency's features or version triggers rebuilds across the workspace. Changing a feature in one member can recompile crates in another. The chapter on resolvers and feature unification explains why.
Concurrent builds block each other. Cargo takes a lock on the target directory. If two cargo commands run at once in the same workspace, the second prints Blocking waiting for file lock on build directory and waits. This often happens when an editor runs cargo check while you run cargo test.
Wrong output path. Scripts that look for crates/app/target/debug/app will fail. Use <root>/target/debug/app, or set CARGO_TARGET_DIR or build.target-dir in .cargo/config.toml to move the directory deliberately.
# .cargo/config.toml
[build]
target-dir = "build-output"With this setting, binaries land in build-output/debug/ instead of target/debug/.
Checking what is locked
Use cargo tree to see which version each member resolves to:
cargo tree -p app
cargo tree --workspace --duplicatesThe second command lists crates that appear at more than one version in the lock file. Duplicates are not errors, but they increase compile time and binary size, so they are worth reviewing.
Exercise
You run cargo build inside crates/app/ in a workspace. Where is the compiled app binary written, and where is the lock file that records its dependency versions?
Show answer
The binary is written to target/debug/app under the workspace root, not under crates/app/. The versions are recorded in the single Cargo.lock at the workspace root.
Exercise
Two members depend on rand. One requires 0.7 and the other requires 0.8. How many rand entries does Cargo.lock contain, and why?
Show answer
Two. Versions 0.7 and 0.8 are not semver-compatible, so Cargo cannot unify them and keeps both in the lock file.