Description
Describe the bug
When running cargo fmt
with the --all
argument, it will download all dependencies in the current workspace, including for targets not used on the current platform.
To Reproduce
- Create a new empty crate and add a dependency on
serde = "1"
. - Remove
serde-*.crate
from~/.cargo/registry/cache/github.com-*/
- Run
cargo fmt
- Check that no
serde-*.crate
files were downloaded into~/.cargo/registry/cache/
- Run
cargo fmt --all
- Observe that the call takes longer, and that
serde-*.crate
was downloaded into~/.cargo/registry/cache/
Furthermore, do the above with a cfg
-gated dependency on serde
using
[target.'cfg(bogus)'.dependencies]
serde = "0.2"
Notice that serde
is still downloaded with --all
, even though it does not even meet the target platform.
Expected behavior
cargo fmt
should never download dependencies, whether run with --all
or not.
Meta
- rustfmt version:
rustfmt 1.4.24-stable (eb894d53 2020-11-05)
- From where did you install rustfmt?:
rustup
- How do you run rustfmt:
cargo-fmt
Diagnosis
The origin of this issue is that --all
invokes cargo metadata
without --no-deps
Line 456 in 81ad114
This behavior was introduced in #3664, and the reasoning is spelled out in #3664 (comment). I think this probably requires a fix to upstream, but I'm not sure (ideas welcome!). I figured that regardless, a tracking issue would be useful.
The target
issue could conceivably be fixed by passing --filter-platform
to cargo metadata
(it considers all targets by default). But, that would still only work subject to rust-lang/cargo#8987, and would also mean that cfg
-guarded workspace members would not be walked by rustfmt
(I think?).