Skip to content
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

Improve Workspaces docs page #13580

Open
rimutaka opened this issue Mar 14, 2024 · 7 comments
Open

Improve Workspaces docs page #13580

rimutaka opened this issue Mar 14, 2024 · 7 comments
Assignees
Labels
A-documenting-cargo-itself Area: Cargo's documentation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@rimutaka
Copy link
Contributor

rimutaka commented Mar 14, 2024

Problem

https://doc.rust-lang.org/cargo/reference/workspaces.html is comprehensive, but could benefit from some clarifications and examples of how to create a workspace

Anyone trying to create a workspace would have to piece together multiple code snippets and go by trial and error.
Most people probably get it right the first time, but making it clearer in the docs will help.

Proposed Solution

  • explain the behavior of a root package (root mainfest, it has different names even in the source code 😓).
  • explain the resolver part where it is first mentioned
  • add examples of creating a workspace step-by-step, manual and/or using cargo commands
  • consider https://matklad.github.io/2021/08/22/large-rust-workspaces.html

Copied from and earlier discussion (#13579 (comment))

Notes

Related:

@rimutaka rimutaka added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Mar 14, 2024
@weihanglo weihanglo added A-documenting-cargo-itself Area: Cargo's documentation S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. and removed S-triage Status: This issue is waiting on initial triage. labels Mar 28, 2024
@weihanglo
Copy link
Member

We can deal with each proposed solution per pull request.

  • explain the behavior of a root package (or primary package or root mainfest, it has different names even in the source code 😓).

This might be the easiest one, by expanding this Root package section, though not sure what needs to be captured. I can think of one: root package becomes the default workspace member when no other specified.

  • explain the resolver part where it is first mentioned

This could be as simple as a link to Resolver versions section, if I understand correctly.

These are covered by #11234 I believe.

While I am going to mark this as S-accepted, documenting Cargo's behavior often requires a deep understanding of Cargo. It implies more back-and-forth between contributors and maintainers in discussion if there are gaps between their familiarities in Cargo.

@weihanglo weihanglo added S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review and removed S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. labels Mar 29, 2024
@heisen-li
Copy link
Contributor

I may not have understood it accurately in this regard, but I'd like to try to address it. @rustbot claim

@heisen-li
Copy link
Contributor

heisen-li commented Apr 10, 2024

explain the resolver part where it is first mentioned

Cargo book workspace The chapters are already given a link when they first appear, so it seems like there's no need for extra explanation or link? Could it be that I'm missing something?

[workspace] — Defines a workspace.

  • resolver — Sets the dependency resolver to use.
    ...

@heisen-li
Copy link
Contributor

From the comment.

Cargo Guide:

This guide will give you all that you need to know about how to use Cargo to develop Rust packages.

Cargo Reference:

The reference covers the details of various areas of Cargo.

Maybe Cargo Reference is more appropriate?

But I seem to have found something else.I looked at the previous chapters and realized that in 3.1 Specifying Dependencies mentions a lot of workspaces and may need to reorder the chapters. For example, put the 3.3 Workspaces section in front.

add examples of creating a workspace step-by-step, manual and/or using cargo commands
consider https://matklad.github.io/2021/08/22/large-rust-workspaces.html

In addition, it occurred to me that I could add a sub-section Creating a Workspace Example to Workspaces, as in 3.4.1 Features Examples and 3.8.1 Build Script Examples

@heisen-li
Copy link
Contributor

@epage @weihanglo , I've written a rough post to show my plan and would like your advice.


Creating a New Workspace

In this chapter we implement the creation of a workspace new_workspace from scratch, in which two members foo-bin and bar-lib are included.

As mentioned in [workspace] section, the workspace must have at least one member, either the root package or a virtual manifest. Here we recommend setting the root of the workspace to be a virtual manifest. The reason is as follows:

  • The crate namespace at the Cargo level is flat. The tree layout creates another hierarchy and increases the possibility of inconsistencies.
  • With a flat structure, adding or splitting crates is very easy.
  • Even larger flattened lists are easier to see at a glance and easier to maintain than smaller tree structures.

Let's look at how a workspace containing a virtual inventory should be created.

Currently Cargo does not provide automatic creation of workspaces using the command line. You need to create the Cargo.toml file under the folder new_workspace:

# [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:

[workspace]
resolver = "2"

You can then proceed to create workspace members. As mentioned above, if you have a lot of workspace members, we recommend designing the workspace with a flat structure. So add all the workspace members in the directory new_workspace/crates.

There are several ways to add members to the workspace, we recommend using the command cargo new/init. For example, add the members foo-bin and bar-lib to the workspace:

$ cargo new crates/foo-bin
$ cargo new crates/bar-lib

Cargo will automatically add members to Cargo.toml:

# [new_workspace]/Cargo.toml
[workspace]
resolver = "2"
members = [ "bar-lib","foo-bin"]

[package]
...

The project at this point contains the following files:

$ cd new_workspace
$ tree .
.
├── Cargo.toml
└── crates
    ├── bar-lib
    │   ├── Cargo.toml
    │   └── src
    │       └── main.rs
    └── foo-bin
        ├── Cargo.toml
        └── src
            └── main.rs

5 directories, 5 files

Up to this point, we have achieved creating a workspace that contains multiple crate members through a step-by-step process.

@epage
Copy link
Contributor

epage commented Apr 12, 2024

Maybe Cargo Reference is more appropriate?

In addition, it occurred to me that I could add a sub-section Creating a Workspace Example to Workspaces, as in 3.4.1 Features Examples and 3.8.1 Build Script Examples

If we're adding a how-to to the reference, that feels backwards and out of place.

The existing "examples" sections are showing practices related to the reference, not tutorial-level documentation. We could likely add one of those for workspaces but that is different than "Creating a workspace". If we add a section hanging off of the reference, it would more likely be related to #11234. This would also accomplish the high level goal set out in this issue (showing a cohesive picture of workspaces).

But I seem to have found something else.I looked at the previous chapters and realized that in 3.1 Specifying Dependencies mentions a lot of workspaces and may need to reorder the chapters. For example, put the 3.3 Workspaces section in front.

Personally, I'm not too worried about the exact ordering of the reference as it is reference level material and cross-references itself all over the place. There isn't really a clear "start with this"

@heisen-li
Copy link
Contributor

So, is it possible to reach a consensus on the following.

The location of the new section is 2.3 Creating a New Workspace. And, if it involves something that's ahead of its time, use a link to refer to it. The general content of the addition is the content of my earlier first draft.

Are there any other suggestions to discuss? If not can I proceed to the next step and submit a PR?

epage added a commit to epage/cargo that referenced this issue Apr 23, 2024
For a user to read the reference and to understand when each type of
workspace might be right for them, they have to know to also read the
section on Package Selection.

This reframes the section on needing to set `resolver = "2"` to being
about differences when there isn't a root package and extends it to
summarize a part of Package Selection, linking out to it.
The hope is that this will make it all of the differences more
discoverable without retreading too much of the same ground within
Reference-style documentation.

Part of rust-lang#13580
epage added a commit to epage/cargo that referenced this issue Apr 25, 2024
For a user to read the reference and to understand when each type of
workspace might be right for them, they have to know to also read the
section on Package Selection.

This reframes the section on needing to set `resolver = "2"` to being
about differences when there isn't a root package and extends it to
summarize a part of Package Selection, linking out to it.
The hope is that this will make it all of the differences more
discoverable without retreading too much of the same ground within
Reference-style documentation.

Part of rust-lang#13580
epage added a commit to epage/cargo that referenced this issue Apr 25, 2024
For a user to read the reference and to understand when each type of
workspace might be right for them, they have to know to also read the
section on Package Selection.

This reframes the section on needing to set `resolver = "2"` to being
about differences when there isn't a root package and extends it to
summarize a part of Package Selection, linking out to it.
The hope is that this will make it all of the differences more
discoverable without retreading too much of the same ground within
Reference-style documentation.

Part of rust-lang#13580
bors added a commit that referenced this issue Apr 25, 2024
docs(ref): Index differences between virtual / real manifests

### What does this PR try to resolve?

For a user to read the reference and to understand when each type of workspace might be right for them, they have to know to also read the section on Package Selection.

This reframes the section on needing to set `resolver = "2"` to being about differences when there isn't a root package and extends it to summarize a part of Package Selection, linking out to it. The hope is that this will make it all of the differences more discoverable without retreading too much of the same ground within Reference-style documentation.

Part of #13580

### How should we test and review this PR?

### Additional information

r? `@weihanglo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documenting-cargo-itself Area: Cargo's documentation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

No branches or pull requests

4 participants