user51
user51

Reputation: 10213

How can I build a specific package in a workspace?

I have two packages in my Cargo project. Cargo.toml looks like this:

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

When I run cargo build --all it compiles all the packages.

I want to build only the common package. If I do cd common and do cargo build it is working fine.

But in the root directory, if I do cargo build common, the build is giving this error:

error: Found argument 'common' which wasn't expected, or isn't valid in this context

USAGE:
    cargo build [OPTIONS]

For more information try --help

Is it possible to build a specific package?

Upvotes: 9

Views: 11638

Answers (4)

Maciej Skorski
Maciej Skorski

Reputation: 3354

To extend the answer provided: build with the package id that can be extracted from the manifest. For instance, I have

#Cargo.toml
[workspace]
members = [
    "crates/fpga",
    "crates/msm",
]

To build only msm I check the manifest of this module/workspace member

cargo pkgid --manifest-path crates/msm/Cargo.toml
#file:///home/maya-msm/crates/msm#[email protected]

And then I can use it in the build

cargo build -p file:///home/maya-msm/crates/msm#[email protected]

Upvotes: 0

d9ngle
d9ngle

Reputation: 1469

You can use the name used in your sub-project's Cargo.toml, e.g.:

# proj1/Cargo.toml
[package]
name = "project-1"

If you run:

cargo build -p proj1

you'll get:

error: package ID specification `proj1` matched no packages

So don't use folder name and use package name:

cargo build -p project-1

Upvotes: 3

Ken
Ken

Reputation: 1260

The first answer not work with:

error: package ID specification `foo` matched no packages

The correct way is:

find out pkgid first:

$ cd server
$ cargo pkgid

file:///dw/path/to/server:4.0.0-SNAPSHOT

Goto workspace folder and run build

$ cd -
cargo build -p file:///dw/path/to/server:4.0.0-SNAPSHOT

Upvotes: 1

Sven Marnach
Sven Marnach

Reputation: 602235

You can use the --package or -p flag to cargo build:

cargo build                # build packages listed in the default-members key
cargo build --all          # build all packages
cargo build --package foo  # build the package "foo"
cargo build -p foo         # ditto

Upvotes: 13

Related Questions