Skip to content

Workspaces: All crates below the root folder must be members of the workspace #3159

@phil-opp

Description

@phil-opp

The workspace documentation says the following:

The package.workspace manifest key (described above) is used in member crates to point at a workspace's root crate. If this key is omitted then it is inferred to be the first crate whose manifest contains [workspace] upwards in the filesystem.

(emphasis mine)

This implies that all crates below the workspace root must be part of the workspace, even if there are no dependencies on them.

For example, imagine the following workspace layout.

…
├── Cargo.toml
├── src
├── sub1
│   ├── Cargo.toml
│   └── src
└── sub2
    ├── Cargo.toml
    └── src

The root Cargo.toml defines a workpace with member sub1:

[package]
#

[workspace]
members = ["sub1"]

# no dependencies

Now we can build the root project and sub1 without problems. But when we try to build sub2, the following error occurs:

> cargo build
error: current package believes it's in a workspace when it's not:
current:   /home/…/test/sub2/Cargo.toml
workspace: /home/…/test/Cargo.toml

this may be fixable by adding `sub2` to the `workspace.members` array of the manifest located at: /home/…/test/Cargo.toml

So sub2 blindly assumes that it's part of the workspace, even though it's not part of workspace.members.

Real World Example: Imagine working with git worktree. This command allows you to temporary check out a branch in a subfolder. For example, you might want to do a quick bugfix on master while working on a feature branch (let's say that you want to introduce cargo workspaces in your project).

So you check out the master branch in a bugfix subfolder. The you cd into it and try to build it. But it fails with the same error as above because your crate now thinks that it's part of the root workspace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions