A workspace is a set of packages developed together, for example a library, its command-line tool and a few plugins. The members of a workspace share:
- one cx.lock, at the workspace root, so they always agree on the versions of shared dependencies;
- one target/, so a dependency is built once for all of them;
- one set of profiles, from the workspace root's cx.toml.
Defining a workspace
The workspace root's cx.toml has a [workspace] section:
# cx.toml at the repository root
[workspace]
members = ["core", "cli", "plugins/*"]
exclude = ["plugins/experimental"]
[workspace.dependencies] # declarations members can share
fmt = "^11.1"
# cli/cx.toml
[package]
name = "cli"
[dependencies]
core = { path = "../core" } # members depend on each other with path dependencies
fmt = { workspace = true } # the declaration from [workspace.dependencies]
- members: directories relative to the root. A * matches one directory level (plugins/*), ** any number. Every member needs a cx.toml.
- exclude: directories that match members (or sit inside the workspace) but aren't part of it. An excluded package is built on its own, with its own cx.lock and target/.
- Virtual workspaces: a root cx.toml with [workspace] but no [package] only lists members. It can also have [profile.*] sections, but no dependencies or targets.
- A root package: if the root cx.toml also has [package], that package is a member too.
A package inside the workspace's directory that members doesn't list (and exclude doesn't either) is an error, so nothing is silently built with the wrong lockfile. The message names the package and says which line to add.
Shared dependency declarations
[workspace.dependencies] takes the same forms as [dependencies]. Paths in it are relative to the workspace root. A member uses one with workspace = true, and can add to it:
[dependencies]
fmt = { workspace = true, features = ["color"], optional = true }
| Key next to workspace = true | Meaning |
| features | added to the workspace's list |
| optional | as for any dependency (declarations in [workspace.dependencies] can't be optional themselves) |
| default-features | replaces the workspace's setting |
Anything else (a version, git, path, ...) is an error: the source comes from the workspace. cx package writes the full declaration into the archive's cx.toml, since the workspace isn't part of the package.
Which members a command works on
| Where you run it | What it works on |
| the workspace root | every member |
| inside a member's directory | that member, with the workspace's cx.lock and target/ |
| anywhere, with -p <NAME> | that member |
| anywhere, with --workspace | every member |
- cx build: all members, or with -p only that member's library and binaries and what they need.
- cx test: every selected member's tests. In a workspace with several members, tests are named <member>/<file stem> (pass a filter after -- to run some of them).
- cx run: looks for the binary in the selected members. With several candidates, pick one with --bin.
- cx tree: one tree per member.
- cx add, cx remove, cx package, cx publish: edit or publish one package, so they run in a member's directory.
- --features, --no-default-features: apply to one member, so they need -p at the workspace root. --all-features works for every member.
Binaries and examples of different members go to the same target/<profile>/, so two members can't have a binary (or example) with the same name.
Resolution
The workspace is resolved as one graph whose root depends on every member, so a dependency shared by members gets one version. Each member is solved with all of its features on, as a single package is, so cx.lock doesn't depend on which member you build or which features you select. Failure reports call the root workspace.