Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?

Right now I have a Cargo workspace with three members.

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

If I run cargo run in the root directory, I get this error:

error: manifest path /home/lukas/dev/mahboi/Cargo.toml is a virtual manifest, but this command requires running against an actual package in this workspace

That makes sense. I can run cargo run -p foo and it works. But the thing is: foo is the only crate that is executable and I will execute it very often, so it would be nice if I could just run cargo run and execute it.

I tried to use the default-members key, but this didn't help:

default-members = ["foo"]

Is there another way to tell Cargo that cargo run should execute the foo crate (equivalent to running cargo run in the foo/ subdirectory)? I would also accept answers that make the root crate non virtual (i.e. add a [package] key).

like image 612
Lukas Kalbertodt Avatar asked Aug 10 '18 11:08

Lukas Kalbertodt


People also ask

Where does cargo build output go?

Output Options Defaults to target in the root of the workspace. Copy final artifacts to this directory. This option is unstable and available only on the nightly channel and requires the -Z unstable-options flag to enable. See https://github.com/rust-lang/cargo/issues/6790 for more information.

How does cargo run work?

When no target selection options are given, cargo run will run the binary target. If there are multiple binary targets, you must pass a target flag to choose one. Or, the default-run field may be specified in the [package] section of Cargo. toml to choose the name of the binary to run by default.

How do I install a specific version of cargo?

Install Options. Specify a version to install. This may be a version requirement, like ~1.2 , to have Cargo select the newest version from the given requirement. If the version does not have a requirement operator (such as ^ or ~ ), then it must be in the form MAJOR.

What is cargo workspace?

A workspace is a collection of one or more packages that share common dependency resolution (with a shared Cargo. lock ), output directory, and various settings such as profiles. Packages that are part of a workspaces are called workspace members.


1 Answers

Single Binary

This is available as of Rust 1.30. Here is the complete set of files I tested with:

Cargo.toml

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

foo/Cargo.toml

[package]
name = "foo"
version = "0.1.0"
authors = ["An Devloper <[email protected]>"]

[dependencies]

foo/src/main.rs

fn main() {
    println!("Hello, world!");
}

bar/Cargo.toml

[package]
name = "bar"
version = "0.1.0"
authors = ["An Devloper <[email protected]>"]

[dependencies]

bar/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

baz/Cargo.toml

[package]
name = "baz"
version = "0.1.0"
authors = ["An Devloper <[email protected]>"]

[dependencies]

baz/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}
$ tree .
.
├── Cargo.lock
├── Cargo.toml
├── bar
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── baz
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── foo
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── src
│   └── lib.rs
└── target
    └── ...
$ cargo run
   Compiling baz v0.1.0 (file:///private/tmp/example/baz)
   Compiling bar v0.1.0 (file:///private/tmp/example/bar)
   Compiling foo v0.1.0 (file:///private/tmp/example/foo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/foo`
Hello, world!

Multiple Binaries

As of Rust 1.37.0 you can use Cargo's "default-run" feature to specify which one to use.

foo/Cargo.toml

[package]
name = "foo"
version = "0.0.1"
authors = ["An Devloper <[email protected]>"]
default-run = "foo"
like image 183
Shepmaster Avatar answered Oct 14 '22 09:10

Shepmaster