How can I build a specific package in a workspace?

Viewed 2247

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?

3 Answers

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

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

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
Related