33A * workspace* is a collection of one or more packages that share common
44dependency resolution (with a shared ` Cargo.lock ` ), output directory, and
55various settings such as profiles. Packages that are part of a workspaces are
6- called * workspace members* . There are two flavours of workspaces: as root
7- package or as virtual manifest.
6+ called * workspace members* .
87
9- ### Root package
8+ The key points of workspaces are:
9+
10+ * All packages share a common ` Cargo.lock ` file which resides in the
11+ * workspace root* .
12+ * All packages share a common [ output directory] , which defaults to a
13+ directory named ` target ` in the * workspace root* .
14+ * The [ ` [patch] ` ] [ patch ] , [ ` [replace] ` ] [ replace ] and [ ` [profile.*] ` ] [ profiles ]
15+ sections in ` Cargo.toml ` are only recognized in the * root* manifest, and
16+ ignored in member crates' manifests.
1017
11- A workspace can be created by adding a [ ` [workspace] `
12- section] ( #the-workspace-section ) to ` Cargo.toml ` . This can be added to a
13- ` Cargo.toml ` that already defines a ` [package] ` , in which case the package is
18+ In the ` Cargo.toml ` , the ` [workspace] ` table supports the following sections:
19+
20+ * [ ` [workspace] ` ] ( #the-workspace-section ) — Defines a workspace.
21+ * [ ` members ` ] ( #the-members-and-exclude-fields ) — Packages to include in the workspace.
22+ * [ ` exclude ` ] ( #the-members-and-exclude-fields ) — Packages to exclude from the workspace.
23+ * [ ` default-members ` ] ( #the-default-members-field ) — Packages to operate on when a specific package wasn't selected.
24+ * [ ` metadata ` ] ( #the-metadata-table ) — Extra settings for external tools.
25+ * [ ` package ` ] ( #the-package-table ) — Keys for inheriting in packages.
26+ * [ ` dependencies ` ] ( #the-dependencies-table ) — Keys for inheriting in package dependencies.
27+
28+ ### The ` [workspace] ` section
29+
30+ To create a workspace, you add the ` [workspace] ` table to a ` Cargo.toml ` :
31+ ``` toml
32+ [workspace ]
33+ # ...
34+ ```
35+
36+ At minimum, a workspace has to have a member, either with a root package or as
37+ a virtual manifest.
38+
39+ #### Root package
40+
41+ If the [ ` [workspace] ` section] ( #the-workspace-section ) is added to a
42+ ` Cargo.toml ` that already defines a ` [package] ` , the package is
1443the * root package* of the workspace. The * workspace root* is the directory
1544where the workspace's ` Cargo.toml ` is located.
1645
17- ### Virtual manifest
46+ ``` toml
47+ [workspace ]
48+
49+ [package ]
50+ name = " hello_world" # the name of the package
51+ version = " 0.1.0" # the current version, obeying semver
52+ authors = [" Alice <a@example.com>" , " Bob <b@example.com>" ]
53+ ```
54+
55+ #### Virtual workspace
1856
1957Alternatively, a ` Cargo.toml ` file can be created with a ` [workspace] ` section
2058but without a [ ` [package] ` section] [ package ] . This is called a * virtual
2159manifest* . This is typically useful when there isn't a "primary" package, or
2260you want to keep all the packages organized in separate directories.
2361
24- ### Key features
25-
26- The key points of workspaces are:
62+ ``` toml
63+ # [PROJECT_DIR]/Cargo.toml
64+ [workspace ]
65+ members = [" hello_world" ]
66+ ```
2767
28- * All packages share a common ` Cargo.lock ` file which resides in the
29- * workspace root * .
30- * All packages share a common [ output directory ] , which defaults to a
31- directory named ` target ` in the * workspace root * .
32- * The [ ` [patch] ` ] [ patch ] , [ ` [replace] ` ] [ replace ] and [ ` [profile.*] ` ] [ profiles ]
33- sections in ` Cargo.toml ` are only recognized in the * root * manifest, and
34- ignored in member crates' manifests.
68+ ``` toml
69+ # [PROJECT_DIR]/hello_world/Cargo.toml
70+ [ package ]
71+ name = " hello_world " # the name of the package
72+ version = " 0.1.0 " # the current version, obeying semver
73+ authors = [ " Alice <a@example.com> " , " Bob <b@example.com> " ]
74+ ```
3575
36- ### The ` [workspace] ` section
76+ ### The ` members ` and ` exclude ` fields
3777
38- The ` [workspace ] ` table in ` Cargo.toml ` defines which packages are members of
78+ The ` [members ] ` and ` exclude ` fields define which packages are members of
3979the workspace:
4080
4181``` toml
@@ -56,26 +96,24 @@ workspace. This can be useful if some path dependencies aren't desired to be
5696in the workspace at all, or using a glob pattern and you want to remove a
5797directory.
5898
59- An empty ` [workspace] ` table can be used with a ` [package] ` to conveniently
60- create a workspace with the package and all of its path dependencies.
61-
62- ### Workspace selection
63-
6499When inside a subdirectory within the workspace, Cargo will automatically
65100search the parent directories for a ` Cargo.toml ` file with a ` [workspace] `
66101definition to determine which workspace to use. The [ ` package.workspace ` ]
67102manifest key can be used in member crates to point at a workspace's root to
68103override this automatic search. The manual setting can be useful if the member
69104is not inside a subdirectory of the workspace root.
70105
71- ### Package selection
106+ #### Package Selection
72107
73108In a workspace, package-related cargo commands like [ ` cargo build ` ] can use
74109the ` -p ` / ` --package ` or ` --workspace ` command-line flags to determine which
75110packages to operate on. If neither of those flags are specified, Cargo will
76111use the package in the current working directory. If the current directory is
77- a virtual workspace, it will apply to all members (as if ` --workspace ` were
78- specified on the command-line).
112+ a [ virtual workspace] ( #virtual-workspace ) , it will apply to all members (as if
113+ ` --workspace ` were specified on the command-line). See also
114+ [ ` default-members ` ] ( #the-default-members-field ) .
115+
116+ ### The ` default-members ` field
79117
80118The optional ` default-members ` key can be specified to set the members to
81119operate on when in the workspace root and the package selection flags are not
@@ -89,7 +127,7 @@ default-members = ["path/to/member2", "path/to/member3/foo"]
89127
90128When specified, ` default-members ` must expand to a subset of ` members ` .
91129
92- ### The ` workspace. metadata` table
130+ ### The ` metadata ` table
93131
94132The ` workspace.metadata ` table is ignored by Cargo and will not be warned
95133about. This section can be used for tools that would like to store workspace
@@ -112,7 +150,7 @@ external tools may wish to use them in a consistent fashion, such as referring
112150to the data in ` workspace.metadata ` if data is missing from ` package.metadata ` ,
113151if that makes sense for the tool in question.
114152
115- ### The ` workspace. package` table
153+ ### The ` package ` table
116154
117155The ` workspace.package ` table is where you define keys that can be
118156inherited by members of a workspace. These keys can be inherited by
@@ -157,7 +195,7 @@ description.workspace = true
157195documentation.workspace = true
158196```
159197
160- ### The ` workspace. dependencies` table
198+ ### The ` dependencies ` table
161199
162200The ` workspace.dependencies ` table is where you define dependencies to be
163201inherited by members of a workspace.
0 commit comments