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 and cargo test. It builds on the workspace from the previous chapters: a virtual root manifest with crates/textkit (a library) and crates/app (a binary that depends on it).
Two files and directories are shared by every member of a workspace: the Cargo.lock file and the target directory. Both live at the workspace root. Understanding what they hold, and what they do not, explains most of the surprising behavior you see when you work with several crates at once.
The shared Cargo.lock
A single package has one Cargo.lock next to its Cargo.toml. A workspace also has exactly one, at the root, next to the root manifest. Members do not get their own lock files. If you find a Cargo.lock inside crates/textkit, it is stale. Cargo ignores it and you can delete it.
The lock file records the exact version of every package in the dependency graph of all members combined. That graph includes third-party crates from crates.io, git dependencies, and the member crates themselves (which appear with no source line, because they come from local paths).
What a lock entry looks like
After you add a dependency on serde to textkit and build, the lock file contains entries like these:
version = 4
[[package]]
name = "app"
version = "0.1.0"
dependencies = [
"textkit",
]
[[package]]
name = "textkit"
version = "0.1.0"
dependencies = [
"serde",
]The version number at the top is the lock file format, not a package version. The exact format number depends on your Cargo release. Each [[package]] entry lists the names of its direct dependencies, and a package with a registry source also carries source and checksum lines.
Why one lock file matters
Sharing a lock file gives you three properties.
- One version per semver-compatible range. If
appandtextkitboth depend onserde = "1", they get the sameserde 1.x.y. You cannot end up testingtextkitagainst oneserdeand shippingappwith another. - One place to update.
cargo updaterewrites the single file.cargo update -p serdeupdates onlyserde, and the change applies to every member that uses it. - A single reviewable diff. A pull request that bumps a dependency changes one file.
Different major versions can still coexist. If textkit needs rand 0.8 and app needs rand 0.9, the lock file holds both, because they are semver-incompatible. Cargo treats them as two separate packages.
Committing the lock file
For a workspace that contains a binary, commit Cargo.lock. This is what makes builds reproducible for your team and for CI. For a workspace that contains only libraries, committing it is also reasonable. It makes your CI results repeatable and does not affect downstream users, because Cargo does not use a library's lock file when someone depends on that library. The consumer's own lock file decides the versions.
Locked and frozen builds
Two flags help in CI:
--lockedmakes Cargo fail if the lock file would need to change. This catches the case where someone edited aCargo.tomland forgot to commit the updated lock file.--offlineforbids network access, and--frozenis--lockedplus--offline.
The shared target directory
All members build into one target directory at the workspace root. Inside it, Cargo keeps a subdirectory per profile (debug, release) and, when you pass --target, a subdirectory per target triple.
cargo build --workspace
ls target/debugYou will see the executable app, plus deps/, build/, incremental/ and .fingerprint/ subdirectories. The library textkit is compiled into target/debug/deps/ as an .rlib file with a hash in its name, for example libtextkit-1a2b3c4d5e6f7a8b.rlib. Cargo picks the hash from the package, its features, the profile and the compiler settings.
Why share the directory
Sharing saves time and disk space for a clear reason. A third-party crate such as serde is compiled once, and every member that needs it with the same features and profile reuses the result. If each crate had its own target, you would compile serde once per member.
When app depends on textkit, Cargo builds textkit first and then links app against the resulting .rlib. Because they are in the same workspace, running cargo run -p app after editing textkit rebuilds only textkit and whatever depends on it.
What gets rebuilt
Cargo tracks a fingerprint for each unit of compilation. A unit is rebuilt when its source files, its dependencies, its features, its profile settings or the compiler version change. The practical consequences:
- Editing
textkit/src/lib.rsrebuildstextkitandapp, notserde. - Editing
app/src/main.rsrebuilds onlyapp. - Switching between
cargo buildandcargo build --releaseuses two separate subdirectories, so neither invalidates the other. - Changing a feature on a shared dependency can produce a second compiled copy of that dependency with a different hash, as the next section explains.
The diagram shows how the two shared items sit at the root and how the members use them.
Where Cargo looks for the root
When you run a command inside crates/textkit, Cargo does not treat that directory as the project root. It walks up the directory tree, looking for a Cargo.toml that contains a [workspace] table and lists the current package as a member. Once it finds one, that directory becomes the workspace root, and the lock file and target directory are used from there.
You can confirm the location with cargo metadata:
cd crates/textkit
cargo metadata --format-version 1 --no-deps | python3 -c "import json,sys; d=json.load(sys.stdin); print(d['workspace_root']); print(d['target_directory'])"Both lines print paths under the workspace root, not under crates/textkit. A build run from a member directory without -p builds that member only, but the output still lands in the root target.
Overriding the target directory
You can move the build output. The precedence, from highest to lowest, is the --target-dir flag, the CARGO_TARGET_DIR environment variable, and build.target-dir in a Cargo config file. A common use is pointing many projects at one directory to share compiled dependencies, or placing the output on a faster disk. Sharing across unrelated projects can cause lock contention and cache churn when their dependency features differ, so use it deliberately.
CARGO_TARGET_DIR=/tmp/ws-target cargo build --workspaceCleaning
cargo clean removes the whole root target directory. To remove the output of one member, and not the whole directory, use -p:
cargo clean -p textkit
cargo clean --releaseThe first command removes artifacts for textkit only. The second removes only the release profile output. Cleaning a single package is useful when you suspect a stale build but do not want to pay for recompiling every third-party dependency.
Common errors and gotchas
A stray Cargo.lock inside a member
Running cargo generate-lockfile or cargo build in an old standalone copy of a crate, then moving it under the workspace, leaves a Cargo.lock in the member. Cargo does not use it. Delete it so nobody assumes it pins anything.
"the lock file needs to be updated but --locked was passed"
You changed a dependency requirement, added a member, or bumped a member version, and did not commit the new lock file. Run cargo build locally without --locked, then commit the resulting Cargo.lock change. Note that bumping a member's own version does change the lock file, because members are listed there.
Two copies of the same crate
If cargo tree -d shows the same crate twice, check whether the versions are semver-incompatible or whether the duplicates differ only by features. The first case is two entries in the lock file. The second case is one lock entry but two compiled artifacts. Use cargo tree -d and cargo tree -e features -i <crate> to find out which members request which features.
Rebuilds that seem unnecessary
Running cargo test -p textkit and then cargo build -p app can recompile a shared dependency, because the two commands may unify features differently. When you select a subset of packages, Cargo computes features only from the selected packages, and a different selection can give a different feature set, and therefore a different hash. The next chapters on resolver versions cover this in detail. For now, remember that running the same selection command repeatedly avoids this churn, and CI should pick one consistent form.
Parallel Cargo commands block each other
Cargo takes a lock on the target directory (a file named .cargo-lock inside the profile directory). A second Cargo command in the same workspace waits with the message "Blocking waiting for file lock on build directory". This is normal. It protects the build output. A language server such as rust-analyzer can hold this lock while you run cargo build. Giving the editor its own target directory, through its settings, avoids the wait.
Quick reference
| Item | Location | Shared by members | Commit it |
|---|---|---|---|
| `Cargo.lock` | Workspace root | Yes | Yes for binaries, usually yes for libraries |
| `target/` | Workspace root | Yes | No, add it to `.gitignore` |
| Member `Cargo.toml` | Each member directory | No, one per member | Yes |
| Member `Cargo.lock` | Should not exist | Ignored | No |
Exercise
You run cargo build --workspace from inside crates/app. Where do the Cargo.lock file and the compiled app executable end up, and why would a Cargo.lock found inside crates/app be a problem?
Show answer
Both belong at the workspace root: the lock file is the root Cargo.lock, and the executable is at target/debug/app under the root. Cargo walks up from crates/app to find the manifest with the [workspace] table and uses that directory for the lock file and target. A Cargo.lock inside crates/app is ignored by Cargo, so it is stale and misleading, because readers may assume it pins versions when it does not.
Exercise
cargo tree -d shows rand twice, as rand v0.8.5 and rand v0.9.0. How many rand entries are in Cargo.lock, and what does this tell you about the members?
Show answer
Two entries, one for each version. The versions are semver-incompatible, so the lock file keeps both. It tells you that at least one member requires rand 0.8 and another requires rand 0.9. To remove the duplicate, align the requirement in the member that is behind, ideally by declaring rand once in [workspace.dependencies].