Search code examples
rustrust-cargo

How to deal with multiple nested workspace roots?


How can you have multiple nested workspace with Cargo?

I have the following project structure:

myworkspace
├── project_a
│   └── Cargo.toml
├── project_b
│   └── Cargo.toml
│   └── project_b_dependency
|       └── Cargo.toml
└── Cargo.toml

Where project_b_dependency is a big library which is a git submodule which has a workspace by itself.

I get an error when I run cargo build because there is a workspace within a workspace.

$ cargo build
error: multiple workspace roots found in the same workspace:
  /myworkspace
  /myworkspace/project_b/project_b_dependency

Is there a simple work-around? I want to keep project_b_dependency in source control as a submodule.

This is not a duplicate of Refactoring to workspace structure causes extern crate imports to not work because I want to know how I can deal with nested workspaces.


Solution

  • It seems that nested workspaces are quite difficult to manage, so one possibility is to change the layout of you project:

    .
    ├── myworkspace
    │   ├── Cargo.lock
    │   ├── Cargo.toml
    │   ├── project_a
    │   │   ├── Cargo.toml
    │   │   └── src
    │   │       └── lib.rs
    │   ├── project_b
    │   │   ├── Cargo.toml
    │   │   └── src
    │   │       └── lib.rs
    │   └── src
    │       └── main.rs
    └── project_b_dependency
        ├── Cargo.toml
        └── src
            └── lib.rs
    

    in myworkspace/Cargo.toml:

    [workspace]
    members= ["project_a", "project_b"]
    

    In myworkspace/project_b/Cargo.toml

    [dependencies]
    project_b_dependency = {path = "../../project_b_dependency"}
    

    I've tried to use workspace.exclude property with your layout but without success.