rust-lang / rust-lang/rustfmt

In workspaces, `cargo fmt` only works in the top-level directory #8724

Open
#4,432 7 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Describe the bug

$ cargo fmt
Failed to find targets
cargo-fmt 1.4.18
This utility formats all bin and lib files of the current crate using rustfmt.

USAGE:
    cargo fmt [FLAGS] [OPTIONS] [-- <rustfmt-options>...]

FLAGS:
        --all        Format all packages (only usable in workspaces)
    -q, --quiet      No output printed to stdout
    -v, --verbose    Use verbose output
        --version    Print rustfmt version and exit

OPTIONS:
        --manifest-path <manifest-path>      Specify path to Cargo.toml
        --message-format <message-format>    Specify message-format: short|json|human
    -p, --package <package>...               Specify package to format (only usable in workspaces)

ARGS:
    <rustfmt-options>...    Options passed to rustfmt

To Reproduce

  1. cargo new outer && cd outer
  2. cargo new inner
  3. printf '[workspace]\nmembers = ["inner"]' >> Cargo.toml
  4. cd src
  5. cargo fmt

Expected behavior

cargo fmt should format all packages in the workspace, like it does from the top-level directory.

Meta

  • rustfmt version: rustfmt 1.4.21-nightly (01f2ead 2020-09-04), but the problem is also present on rustfmt 1.4.18-stable (8157a3f 2020-07-15)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the listed cargo new, workspace, and cargo fmt commands, especially from the inner src directory. Trace how cargo fmt discovers the workspace and packages when invoked below the top level; done means cargo fmt from that directory formats all workspace packages as it does from the workspace root.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.