Workspace giving two different behaviors in Rust


#1

I am learning about Cargo workspaces and have set up the following structure:

Top-level:

[package]
name = "workspacer"
version = "0.1.0"
authors = ["ustulation <zzzzzz@gmail.com>"]

[workspace]
members = ["safe_core", "safe_authenticator", "safe_app"]

# If this is removed then each of the sub-projects will have thier own Cargo.lock file
# will build binaries/objects in their own target/ directories. With this present, it's
# always the parent-projects Cargo.lock and target/ directory used. Need to check if this
# is standard behaviour or some bug about to be fixed.
[lib]
crate_type = ["rlib", "cdylib", "staticlib"]

A lib called safe_core which only needs to produce a .rlib

[package]
authors = ["ustulation <zzzzzz@gmail.com>"]
name = "safe_core"
version = "0.1.0"

[dependencies]
maidsafe_utilities = "~0.10.0"

A lib called safe_app which depends on safe_core and needs to produce all 3 .rlib, .a and .so:

[package]
name = "safe_app"
version = "0.1.0"
authors = ["ustulation <zzzzzz@gmail.com>"]

[dependencies]
maidsafe_utilities = "~0.10.0"
safe_core = { path = "../safe_core" }

[lib]
crate_type = ["rlib", "cdylib", "staticlib"]

A lib called safe_authenticator which depends on safe_core and needs to produce all 3 .rlib, .a and .so:

[package]
name = "safe_authenticator"
version = "0.1.0"
authors = ["ustulation <zzzzzz@gmail.com>"]

[dependencies]
safe_core = { path = "../safe_core" }

[lib]
crate_type = ["rlib", "cdylib", "staticlib"]

The tree looks like:

workspacer
├── Cargo.toml
├── safe_app
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── safe_authenticator
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
└── safe_core
    ├── Cargo.toml
    └── src
        └── lib.rs

If I go to safe_core and build, it creates a target/ folder and Cargo.lock files inside the top level workspacer/, which is good.

If I go to safe_authenticator folder and build that it too uses the same target/ and Cargo.lock files and hence does not recompile safe_core which is what I want too. Same with safe_app.

However if I remove the [lib] section from the top-level workspacer/Cargo.toml, each of the sub-projects start creating their own Cargo.lock files and their own /target directories inside their respective sub-directories. I have mentioned this in the inline comment in the Cargo.toml of workspacer above (the 1st snippet above).

Is this an expected behavior or a bug or am I doing something wrong ?

~$ rustc --version && cargo --version
rustc 1.15.0-nightly (ba872f270 2016-11-17)
cargo 0.15.0-nightly (1877f59 2016-11-16)

#2

Looks like a bug to me! All members of workspace should share the same target directory no matter what!


#3

Although I fail to reproduce it. I use exact the same Cargo.tomls as you, and the same version of the nightly Cargo. Can you publish your example as repository and open the issue at http://github.com/rust-lang/cargo if it indeed reproduces?


#4

Sure - here you go: https://github.com/ustulation/workspacer

I just ran it with stable 1.13 to confirm:

~$ rustc --version && cargo --version
rustc 1.13.0 (2c6933acc 2016-11-07)
cargo 0.13.0-nightly (eca9e15 2016-11-01)

So remove the [lib] section from workspacer/Carg.toml as the question says in the inline comment, cd to safe_app/, run cargo test. It will produce safe_app/Cargo.lock and safe_app/target/....

I am on ubuntu 16.10 if that helps.


#5

Bug report: https://github.com/rust-lang/cargo/issues/3407