Skip to content

Commit 65daa8e

Browse files
committed
docs(ref): Re-org workspace docs to be like package docs
In looking over rust-lang#10625, I remembered we've been having growing pains with the workspace documentation. It was originally written when there were a lot fewer workspace features. As more workspace features have been added, they've been tacked on to the documentation. This re-thinks the documentation by focusing on the schema, much like `manifest.md` does.
1 parent 0825039 commit 65daa8e

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

src/doc/src/reference/manifest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ external tools may wish to use them in a consistent fashion, such as referring
516516
to the data in `workspace.metadata` if data is missing from `package.metadata`,
517517
if that makes sense for the tool in question.
518518

519-
[workspace-metadata]: workspaces.md#the-workspacemetadata-table
519+
[workspace-metadata]: workspaces.md#the-metadata-table
520520

521521
#### The `default-run` field
522522

src/doc/src/reference/specifying-dependencies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ rand = { workspace = true, optional = true }
489489

490490
[crates.io]: https://crates.io/
491491
[dev-dependencies]: #development-dependencies
492-
[workspace.dependencies]: workspaces.md#the-workspacedependencies-table
492+
[workspace.dependencies]: workspaces.md#the-dependencies-table
493493
[optional]: features.md#optional-dependencies
494494
[features]: features.md
495495

src/doc/src/reference/unstable.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ for more information.
13581358
### Workspace Inheritance
13591359

13601360
Workspace Inheritance has been stabilized in the 1.64 release.
1361-
See [workspace.package](workspaces.md#the-workspacepackage-table),
1362-
[workspace.dependencies](workspaces.md#the-workspacedependencies-table),
1361+
See [workspace.package](workspaces.md#the-package-table),
1362+
[workspace.dependencies](workspaces.md#the-dependencies-table),
13631363
and [inheriting-a-dependency-from-a-workspace](specifying-dependencies.md#inheriting-a-dependency-from-a-workspace)
1364-
for more information.
1364+
for more information.

src/doc/src/reference/workspaces.md

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,79 @@
33
A *workspace* is a collection of one or more packages that share common
44
dependency resolution (with a shared `Cargo.lock`), output directory, and
55
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*.
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
1443
the *root package* of the workspace. The *workspace root* is the directory
1544
where 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 <[email protected]>", "Bob <[email protected]>"]
53+
```
54+
55+
#### Virtual workspace
1856

1957
Alternatively, a `Cargo.toml` file can be created with a `[workspace]` section
2058
but without a [`[package]` section][package]. This is called a *virtual
2159
manifest*. This is typically useful when there isn't a "primary" package, or
2260
you 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 <[email protected]>", "Bob <[email protected]>"]
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
3979
the workspace:
4080

4181
```toml
@@ -56,26 +96,24 @@ workspace. This can be useful if some path dependencies aren't desired to be
5696
in the workspace at all, or using a glob pattern and you want to remove a
5797
directory.
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-
6499
When inside a subdirectory within the workspace, Cargo will automatically
65100
search the parent directories for a `Cargo.toml` file with a `[workspace]`
66101
definition to determine which workspace to use. The [`package.workspace`]
67102
manifest key can be used in member crates to point at a workspace's root to
68103
override this automatic search. The manual setting can be useful if the member
69104
is not inside a subdirectory of the workspace root.
70105

71-
### Package selection
106+
#### Package Selection
72107

73108
In a workspace, package-related cargo commands like [`cargo build`] can use
74109
the `-p` / `--package` or `--workspace` command-line flags to determine which
75110
packages to operate on. If neither of those flags are specified, Cargo will
76111
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
79117

80118
The optional `default-members` key can be specified to set the members to
81119
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"]
89127

90128
When specified, `default-members` must expand to a subset of `members`.
91129

92-
### The `workspace.metadata` table
130+
### The `metadata` table
93131

94132
The `workspace.metadata` table is ignored by Cargo and will not be warned
95133
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
112150
to the data in `workspace.metadata` if data is missing from `package.metadata`,
113151
if that makes sense for the tool in question.
114152

115-
### The `workspace.package` table
153+
### The `package` table
116154

117155
The `workspace.package` table is where you define keys that can be
118156
inherited by members of a workspace. These keys can be inherited by
@@ -157,7 +195,7 @@ description.workspace = true
157195
documentation.workspace = true
158196
```
159197

160-
### The `workspace.dependencies` table
198+
### The `dependencies` table
161199

162200
The `workspace.dependencies` table is where you define dependencies to be
163201
inherited by members of a workspace.

0 commit comments

Comments
 (0)