6

I have multi-workspace Cargo project. It has two workspaces, common and server. common is a lib project and server is a bin project.

The location of the project in Github is here.

Below is the project structure.

.
├── Cargo.toml
├── common
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── README.md
└── server
    ├── Cargo.toml
    └── src
        └── main.rs

4 directories, 6 files

And the file contents of ./Cargo.toml file is

[package]
name = "multi_module_cargo_project"
version = "0.1.0"
authors = ["rajkumar"]

[workspace]
members = ["common", "server"]

[dependencies]

When I run the command cargo build --all:

error: failed to parse manifest at `/home/rajkumar/Coding/Rust/ProgrammingRust/multi_module_cargo_project/Cargo.toml`

Caused by:
no targets specified in the manifest
either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

So I added below in Cargo.toml but still couldn't build the project.

[[bin]]
name = "server/src/main.rs"

How can I build the project. What I'm missing?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
user51
  • 8,843
  • 21
  • 79
  • 158
  • 1
    I think you should only put `[workspace]` inside the root manifest and so remove `[package]` and `[dependencies]`, put that only in the manifest inside `server` and `common`. – Stargateur Nov 03 '18 at 17:27
  • @Stargateur - You are right. That works. I would've accepted your answer if you answered the question. – user51 Nov 03 '18 at 18:35

1 Answers1

9

You included a [package] section in your main Cargo.toml file. This section indicates that you want to build a main package in addition to the packages in the workspace. However, you don't have any source files for the main package, so Cargo complains.

The solution is to simply omit the [package] section, and only include [workspace]. This configures a virtual workspace – a workspace that is only a container for member packages, but does not build a package itself.

See the main Cargo.toml file of Rocket for a real-world example of a virtual workspace, and Tokio for a real-world example of a workspace with a main package.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841