rustrust-cargo

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?


Solution

  • 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