-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[doc]: Add create workspace doc #13766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
heisen-li
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
heisen-li:workspace_example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Creating a New Workspace | ||
|
||
A [workspace][def-workspace] is a collection of one or more packages, | ||
called workspace members, that are managed together. | ||
|
||
In this chapter, we will create a workspace `new_workspace` containing | ||
binary member `foo` and library member `bar`. | ||
|
||
As mentioned in [`[workspace]` section][workspace-section], the workspace must | ||
have at least one member, either the [root package] or a [virtual manifest]. | ||
|
||
Next we create a workspace containing [root package]. | ||
For convenience, you can first create a package using the command `cargo new new_workspace`. | ||
Then add the `[workspace]` section to the `Cargo.toml` file in the root directory | ||
to make it a manifest of the workspace: | ||
|
||
```toml | ||
# [new_workspace]/Cargo.toml | ||
[workspace] | ||
|
||
[package] | ||
name = "new_workspace" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
``` | ||
|
||
Then, continue adding members `foo` and `bar` to the workspace: | ||
|
||
```console | ||
$ cd new_workspace | ||
$ cargo new foo | ||
$ cargo new bar --lib | ||
``` | ||
|
||
Cargo will automatically add members to `Cargo.toml`。 | ||
At this point, the workspace will contain three members: `foo` and `bar` and | ||
the default member `new_workspace`. | ||
|
||
```toml | ||
# [new_workspace]/Cargo.toml | ||
[workspace] | ||
members = [ "bar", "foo" ] | ||
|
||
[package] | ||
name = "new_workspace" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
``` | ||
|
||
The package at this point contains the following files: | ||
|
||
```console | ||
$ cd new_workspace | ||
$ tree . | ||
. | ||
├── bar | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── lib.rs | ||
├── Cargo.toml | ||
├── foo | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── main.rs | ||
└── src | ||
└── main.rs | ||
|
||
5 directories, 6 files | ||
``` | ||
|
||
Let's move on and create a virtual workspace. | ||
|
||
In the another `new_workspace` empty directory, create a new `Cargo.toml` file and | ||
add the `[workspace]` section: | ||
|
||
```toml | ||
# [new_workspace]/Cargo.toml | ||
[workspace] | ||
``` | ||
|
||
If using a virtual workspace, then the version of [resolver] needs to be specified | ||
in the table (if not, the default version of resolver for a workspace is `1`, | ||
even if the default resolver version for workspace members is `2`), for example: | ||
|
||
```toml | ||
# [new_workspace]/Cargo.toml | ||
[workspace] | ||
resolver = "2" | ||
``` | ||
|
||
Likewise, you can then use the `cargo new <package>` command to create | ||
binary member `foo` and library member `bar`. | ||
|
||
|
||
```toml | ||
# [new_workspace]/Cargo.toml | ||
[workspace] | ||
resolver = "2" | ||
members = [ "bar","foo"] | ||
|
||
``` | ||
|
||
The package at this point contains the following files: | ||
|
||
```console | ||
$ cd new_workspace | ||
$ tree . | ||
. | ||
├── bar | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── lib.rs | ||
├── Cargo.toml | ||
└── foo | ||
├── Cargo.toml | ||
└── src | ||
└── main.rs | ||
|
||
4 directories, 5 files | ||
``` | ||
|
||
Up to this point, we have a workspace with two members. | ||
Whenever you run `cargo build` under the workspace root directory, Cargo builds | ||
all member at once. | ||
Instead of building the entire workspace, you could use the `--package`/`-p` flag | ||
to select certain packages. | ||
For example, `cargo build -p foo` will build only `foo` package. | ||
|
||
[workspace-section]: ../reference/workspaces.md#the-workspace-section | ||
[root package]: ../reference/workspaces.md#root-package | ||
[virtual manifest]: ../reference/workspaces.md#virtual-workspace | ||
[def-workspace]: ../appendix/glossary.md#workspace '"workspace" (glossary entry)' | ||
[resolver]: ../reference/resolver.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels odd because
new_workspace
is actually a package. The step-by-step guide is not an ideal workflow, and even more confusing at this moment. Admittedly this is the tool's failure #8365. I wonder if we want to block this on that.