Sub topic 6
Connecting crates with path dependencies
This chapter assumes you can read a single-package Cargo.toml, know the difference between library and binary crates, and can declare dependencies and features. Here you will see how one workspace member uses another, what Cargo does with that declaration, and which errors you are likely to hit.
What a path dependency is
A path dependency points at a crate in a directory on disk instead of a registry such as crates.io. You write it with the path key:
[dependencies]
textkit = { path = "../textkit" }The path is relative to the Cargo.toml that contains the declaration, not to the directory where you run cargo. The target directory must hold a Cargo.toml with a [package] table. Cargo reads the package name from that manifest. The dependency key on the left (textkit) must match that name unless you use the package key to rename it.
Inside a workspace, path dependencies are how members use each other. Cargo already knows every member from the [workspace] table, so it builds a dependency graph across them. When app depends on textkit, Cargo builds textkit first, then app, and rebuilds app whenever textkit changes.
The example workspace
The root manifest is virtual: it has no [package] table. It lists every crate under crates/ as a member and declares textkit once in [workspace.dependencies].
[workspace]
resolver = "2"
members = ["crates/*"]
[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[workspace.dependencies]
textkit = { path = "crates/textkit" }
[workspace.lints.rust]
unsafe_code = "forbid"The library member, crates/textkit/Cargo.toml, inherits its metadata and lints:
[package]
name = "textkit"
version.workspace = true
edition.workspace = true
license.workspace = true
[lints]
workspace = trueThe binary member, crates/app/Cargo.toml, depends on the library:
[package]
name = "app"
version.workspace = true
edition.workspace = true
license.workspace = true
[dependencies]
textkit.workspace = true
[lints]
workspace = trueThe library source, crates/textkit/src/lib.rs:
pub fn shout(input: &str) -> String {
format!("{}!", input.to_uppercase())
}The binary source, crates/app/src/main.rs:
fn main() {
println!("{}", textkit::shout("hello"));
}Running cargo run -p app from anywhere inside the workspace prints HELLO!.
Two ways to write the dependency
The line textkit.workspace = true is shorthand. It tells Cargo to look up textkit in the root [workspace.dependencies] table and use that entry. The entry's path, crates/textkit, is relative to the root manifest. Cargo resolves it once, so every member that inherits it points at the same directory regardless of how deeply the member is nested.
You can also skip inheritance and write the path directly in each member:
[dependencies]
textkit = { path = "../textkit" }The table compares the two styles.
| Style | Path is relative to | Good for | Drawback |
|---|---|---|---|
| `textkit = { path = "../textkit" }` | The member's own manifest | Small workspaces, one-off links | Every member repeats the path. Moving a crate means editing each one. |
| `textkit.workspace = true` | The workspace root manifest | Workspaces with several consumers | Needs an entry in `[workspace.dependencies]`. |
Both produce the same dependency graph. Inheritance only changes where the declaration lives. The next chapter-level topic, centralizing dependency versions, builds on this.
Adding a version for publishing
A path-only dependency works locally but cannot be published. The registry has no way to resolve a path on your disk. When you publish app, Cargo requires a version next to the path. During local builds Cargo uses the path. After publishing, consumers get the version from the registry.
[workspace.dependencies]
textkit = { path = "crates/textkit", version = "0.1.0" }If you leave out version and try cargo publish -p app, Cargo rejects the package because the dependency has no version requirement. Publishing is covered in a later chapter. For now, remember that adding version early costs nothing and avoids that error.
A related point is that the version is a requirement, not a pin. version = "0.1.0" means ^0.1.0, so it accepts 0.1.x releases at or above 0.1.0. If textkit in your workspace is at 0.1.0, the path version satisfies the requirement. If you bump textkit to 0.2.0 but leave the requirement at 0.1.0, Cargo reports a mismatch between the path crate and the requirement, because 0.2.0 does not satisfy ^0.1.0.
What Cargo does with the link
Cargo treats a path dependency that lives inside the workspace as a member. Three consequences follow.
- It is built from source in the shared
targetdirectory, so a change intextkittriggers a rebuild ofappand nothing else. - It does not appear as a registry package in
Cargo.lock. Path dependencies are listed without asourceline, because there is nothing to download or checksum. - Its own dependencies are resolved together with the rest of the workspace, so
appandtextkitagree on one version of any shared crate.
You can see the graph with:
cargo tree -p appThe output lists textkit v0.1.0 (/path/to/workspace/crates/textkit). The path in parentheses marks a path dependency.
Rules about paths that are not members
A path dependency does not have to be listed in members. Cargo adds any path dependency that sits inside the workspace root directory to the workspace automatically. That is called an implicit member, and it is the reason a crate can build even when you forgot to list it.
Be careful with this. Implicit membership applies only to crates whose directories are below the root. A path dependency that points outside the workspace root, such as ../shared-lib, is not a member. It still builds, but it is not governed by the workspace: it does not inherit settings and it uses its own lock behavior. For predictable builds, list the crate in members or move it under the root.
A crate excluded with exclude can still be a path dependency. In that case Cargo builds it as a normal dependency and ignores the exclusion for that purpose.
Common errors
The table lists errors you are likely to see when linking crates, with the usual cause and fix.
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `failed to read ... Cargo.toml` | The `path` is wrong. It was written relative to the wrong manifest. | Count `..` segments from the member's own directory, or use `workspace = true`. |
| `no matching package named X found` with a path | The dependency key does not match the target's `[package] name`. | Rename the key, or add `package = "real-name"`. |
| `dependency.textkit was not found in workspace.dependencies` | The member uses `textkit.workspace = true` but the root has no such entry. | Add the entry to `[workspace.dependencies]`. |
| `can't find crate for textkit` in code | The target is a binary-only crate with no `src/lib.rs`. | Move shared code into a library crate and depend on that. |
| `unresolved import` for a function | The function is not `pub`, or is not exported from the crate root. | Mark it `pub` and re-export it in `lib.rs`. |
| Cycle error: `cyclic package dependency` | Two members depend on each other. | Extract the shared code into a third crate that both use. |
Two of these deserve more explanation.
First, you cannot depend on a binary crate as a library. A package with only src/main.rs produces an executable, and other crates cannot import from it. The fix is the usual split: put logic in a library crate and keep the binary thin.
Second, cycles are never allowed in the package graph, even through dev-dependencies when they would create a build-time loop between normal dependencies. Cargo does allow a package's dev-dependency to point back at a crate that depends on it in some cases, but treat that as an exception. A new shared crate is the clearer design.
Dev-dependencies and build-dependencies
Path dependencies work in every dependency table. A test helper crate can be a dev-dependency, so it is built only for tests and examples:
[dev-dependencies]
test-support = { path = "../test-support" }With inheritance, you declare test-support in [workspace.dependencies] and write test-support.workspace = true under [dev-dependencies]. The same applies to [build-dependencies].
When you inherit a dependency, you can add to it but not remove from it. For example, you can enable a feature or mark it optional:
[dependencies]
textkit = { workspace = true, features = ["unicode"], optional = true }The features list is merged with whatever the root entry already enables. You cannot override the path or version at the member level. Also, default-features = false is honored only if it is set in the root entry. Setting it in a member while the root leaves defaults on has no effect, and recent Cargo versions warn about it.
Verifying the link
After you wire up two crates, check three things:
cargo build --workspace
cargo test -p textkit
cargo run -p app
cargo build --workspace --exclude appThe first builds every member. The second tests only the library. The third runs the binary and should print HELLO!. The fourth builds everything except app, which confirms textkit stands on its own. If the last command succeeds, the dependency points the right way: app needs textkit, and textkit does not need app.
Key points
- A
pathis relative to the manifest that contains it. In[workspace.dependencies]that means the root manifest. name.workspace = truereuses the root entry and lets you add features oroptional, but not change the path.- Add a
versionnext topathso the crate can be published later. - Path dependencies are built from source in the shared target directory and have no
sourceline inCargo.lock. - Dependencies cannot form cycles, and binary-only crates cannot be imported.
Exercise
In a workspace, crates/app/Cargo.toml contains textkit.workspace = true, and the root manifest has textkit = { path = "crates/textkit" } under [workspace.dependencies]. Cargo reports "failed to read crates/app/crates/textkit/Cargo.toml". Is the path in the root entry wrong, and what is the likely real cause?
Show answer
The root entry is correct, because paths there are relative to the root manifest. That error appears when the path was written directly in the member (textkit = { path = "crates/textkit" }), where it is resolved relative to crates/app/. Change the member line to textkit.workspace = true or use path = "../textkit".
Exercise
You want app to depend on textkit but also enable an optional unicode feature of textkit only for app. Write the dependency line in crates/app/Cargo.toml, and say whether you can also change its path there.
Show answer
Write textkit = { workspace = true, features = ["unicode"] } under [dependencies]. You cannot change the path in the member, because the path comes from the root [workspace.dependencies] entry. The features list is added to any features the root entry already enables.