Skip to content

cargo fmt --all downloads dependencies #4599

Closed
@jonhoo

Description

@jonhoo

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

  1. Create a new empty crate and add a dependency on serde = "1".
  2. Remove serde-*.crate from ~/.cargo/registry/cache/github.com-*/
  3. Run cargo fmt
  4. Check that no serde-*.crate files were downloaded into ~/.cargo/registry/cache/
  5. Run cargo fmt --all
  6. 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

let metadata_with_deps = get_cargo_metadata(manifest_path, true)?;

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?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions