3
3
A * workspace* is a collection of one or more packages that share common
4
4
dependency resolution (with a shared ` Cargo.lock ` ), output directory, and
5
5
various 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* .
8
7
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.
10
17
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
14
43
the * root package* of the workspace. The * workspace root* is the directory
15
44
where the workspace's ` Cargo.toml ` is located.
16
45
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
+
53
+ ```
54
+
55
+ #### Virtual workspace
18
56
19
57
Alternatively, a ` Cargo.toml ` file can be created with a ` [workspace] ` section
20
58
but without a [ ` [package] ` section] [ package ] . This is called a * virtual
21
59
manifest* . This is typically useful when there isn't a "primary" package, or
22
60
you want to keep all the packages organized in separate directories.
23
61
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
+ ```
27
67
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
+
74
+ ```
35
75
36
- ### The ` [workspace] ` section
76
+ ### The ` members ` and ` exclude ` fields
37
77
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
39
79
the workspace:
40
80
41
81
``` toml
@@ -56,26 +96,24 @@ workspace. This can be useful if some path dependencies aren't desired to be
56
96
in the workspace at all, or using a glob pattern and you want to remove a
57
97
directory.
58
98
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
-
64
99
When inside a subdirectory within the workspace, Cargo will automatically
65
100
search the parent directories for a ` Cargo.toml ` file with a ` [workspace] `
66
101
definition to determine which workspace to use. The [ ` package.workspace ` ]
67
102
manifest key can be used in member crates to point at a workspace's root to
68
103
override this automatic search. The manual setting can be useful if the member
69
104
is not inside a subdirectory of the workspace root.
70
105
71
- ### Package selection
106
+ #### Package Selection
72
107
73
108
In a workspace, package-related cargo commands like [ ` cargo build ` ] can use
74
109
the ` -p ` / ` --package ` or ` --workspace ` command-line flags to determine which
75
110
packages to operate on. If neither of those flags are specified, Cargo will
76
111
use 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
79
117
80
118
The optional ` default-members ` key can be specified to set the members to
81
119
operate 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"]
89
127
90
128
When specified, ` default-members ` must expand to a subset of ` members ` .
91
129
92
- ### The ` workspace. metadata` table
130
+ ### The ` metadata ` table
93
131
94
132
The ` workspace.metadata ` table is ignored by Cargo and will not be warned
95
133
about. 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
112
150
to the data in ` workspace.metadata ` if data is missing from ` package.metadata ` ,
113
151
if that makes sense for the tool in question.
114
152
115
- ### The ` workspace. package` table
153
+ ### The ` package ` table
116
154
117
155
The ` workspace.package ` table is where you define keys that can be
118
156
inherited by members of a workspace. These keys can be inherited by
@@ -157,7 +195,7 @@ description.workspace = true
157
195
documentation.workspace = true
158
196
```
159
197
160
- ### The ` workspace. dependencies` table
198
+ ### The ` dependencies ` table
161
199
162
200
The ` workspace.dependencies ` table is where you define dependencies to be
163
201
inherited by members of a workspace.
0 commit comments