9

Using Rust 1.11 and Cargo 1.12 (nightly), I'm trying to create a [workspace] which contains a few libraries and a few executables.

In my root folder, I added my sub-crates with:

cargo new loader 
cargo new shell --bin

I then added the cargo.toml shown below to my root folder.

[package]
name = "rustenv"
version = "0.1.0"
authors = ["ME"]

[workspace]
members = [
    "loader"
    , "shell"
    ]

Running cargo build in my root folder yields:

PS E:\r\rustenv> cargo build 

error: failed to parse manifest at
`E:\r\rustenv\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

I am a bit confused about how this [workspace] feature is supposed to work given that I am a Visual Studio user by nature and there, I can simply add projects to a workspace. It appears there is something else I have to do with Cargo to get the same effect.

Vít Kotačka
  • 1,472
  • 1
  • 15
  • 40
BitTickler
  • 10,905
  • 5
  • 32
  • 53

1 Answers1

6

If your root project does not produce any artifacts (it is not a library/binary) then in terms of the Cargo workspaces RFC it is "virtual", it should contain only the workspace section:

[workspace]
members = [
    "loader"
    , "shell"
    ]

If cargo build does not work for it, you should

cd loader
cargo build
cd ..
cd shell
cargo build 

what works in such configuration? You have shared output directory "target" and "Cargo.lock", so if you type "cargo build" in "loader" subdirectory you have compiled library in "../target/"

Shell session that demonstrates how it works:

/tmp $ mkdir test
/tmp $ cd test
/tmp/test $ cargo new loader && cargo new shell --bin
     Created library `loader` project
     Created binary (application) `shell` project
/tmp/test $ cat > Cargo.toml
[workspace]
members = ["loader", "shell"]
/tmp/test $ cat loader/Cargo.toml 
[package]
name = "loader"
version = "0.1.0"
authors = ["me"]
workspace = ".."

[dependencies]
/tmp/test $ cat shell/Cargo.toml 
[package]
name = "shell"
version = "0.1.0"
authors = ["me"]
workspace = ".."

[dependencies]
/tmp/test/shell $ cargo build
   Compiling shell v0.1.0 (file:///tmp/test/shell)
    Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
evgeniy@localhost /tmp/test/shell $ ls
Cargo.toml  src
evgeniy@localhost /tmp/test/shell $ ls ../
Cargo.lock  Cargo.toml  loader  shell  target
/tmp/test $ cargo --version
cargo 0.13.0-nightly (cd8ad10 2016-08-15)

If you worked/work with Visual Studio, have look at the Rust plugin for Jetbrains IDE

Vít Kotačka
  • 1,472
  • 1
  • 15
  • 40
fghj
  • 8,898
  • 4
  • 28
  • 56
  • Without the package part (and only workspace part) I get: PS E:\r\rustenv> cargo build error: failed to parse manifest at `E:\r\rustenv\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 PS E:\r\rustenv> cargo build error: failed to parse manifest at `E:\r\rustenv\Cargo.toml` Caused by: no `package` or `project` section found. – BitTickler Aug 26 '16 at 17:00
  • @BitTickler As I wrote, and as official documentation mention, **it should not work**, this is how it works now. To build you need call `cargo build` in subdirectories of real projects, not virtual. – fghj Aug 26 '16 at 17:05
  • after adding ``package.workspace`` to the sub-crates, I get: PS E:\r\rustenv\loader> cargo build warning: unused manifest key: package.workspace – BitTickler Aug 26 '16 at 17:16
  • @BitTickler you should have something like `workspace = ".."` in your package sections in loader – fghj Aug 26 '16 at 17:19
  • That I have: Under ``package`` section: ``workspace = "../rustenv"`` – BitTickler Aug 26 '16 at 17:43
  • @BitTickler If you have structure like: `rustenv/loader` and `rustenv/shell`, then you should have `workspace = ".."` in both `loader` and `shell`, not `workspace = "../rustenv"` – fghj Aug 27 '16 at 08:42
  • Tried that as well - but build of sub-crates goes to a target inside sub-crate anyway. Win64 rust version, btw. – BitTickler Aug 27 '16 at 12:45
  • @BitTickler I add shell session to demonstrate how it works for me. – fghj Aug 27 '16 at 16:01
  • So it is a 0.13 cargo feature? I downloaded the most recent release for my (tier 0) platform right before I started this. I expected all tier 0 platforms had the same version... – BitTickler Aug 27 '16 at 21:00