Akh
Akh

Reputation: 663

Cargo recompiles package in workspace with "-p" after compiling with "--workspace"

In general, I compile everything in my workspace with: cargo build from the workspace directory, or with cargo build --workspace

However, if I then compile a specific package with cargo build -p package or by running cargo build from that package's directory, it will recompile it, when actually it should be completely cached, right?

It seems that the things being rerun are mostly related to macros... recompiling syn, serde_derive, etc...

Is it somehow expected that these macro crates need to be recompiled because they might give different results? I feel like that really shouldn't be the case if we want reproducible builds!

What can I do to prevent cargo from recompiling like this?

(this is with cargo 1.51.0 on a raspberry pi 4 running Raspbian 10)

Upvotes: 2

Views: 454

Answers (1)

Ordoshsen
Ordoshsen

Reputation: 780

This can happen when different packages have different sets of features. When cargo build --workspace is run, the dependencies are built with union of the enabled features. When -p package-name is used, only the features needed by that package are used, resulting in recompilation of dependencies where some features are not turned on.

If I run cargo build --bin package-name instead, the number of recompiled crates is much lower although there still are a few.

There's a short discussion about this on the Rust forums.

Upvotes: 1

Related Questions