Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
jyn514 opened this issue Sep 22, 2020 · 7 comments
Open

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

jyn514 opened this issue Sep 22, 2020 · 7 comments
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@jyn514
Copy link
Member

jyn514 commented Sep 22, 2020

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
@jyn514 jyn514 added the bug Panic, non-idempotency, invalid code, etc. label Sep 22, 2020
@calebcartwright
Copy link
Member

calebcartwright commented Sep 22, 2020

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

You need to include the --allflag if you want to run against the entire workspace from a sub directory. Some more info here

@jyn514
Copy link
Member Author

jyn514 commented Sep 22, 2020

It should say that in the error message, then. Right now it just looks like a bug.

@calebcartwright
Copy link
Member

It should say that in the error message, then. Right now it just looks like a bug.

I just wanted to clarify that the expected behavior you provided in the issue description was not in alignment with the expected cargo fmt behavior. Full workspace formatting from a subdirectory is supposed to be an opt-in via the flag so that folks have the ability to format just the local member as well as the entire workspace.

That being said, I do agree there's a bug and suspect it's a regression of an older one

@epage
Copy link
Contributor

epage commented Sep 29, 2021

@calebcartwright it is unclear to me what is considered the bug in this scenario and what the expected behavior since it sounds like some of the behavior is expected.

Personally, I would expect it to behave like cargo build and when run inside of a src directory, it would format that crate. Also worth noting that this behavior is different, depending on if you are in a workspace or not.

(came across this issue when looking at #5010)

@jyn514
Copy link
Member Author

jyn514 commented Nov 8, 2022

This also happens if you're not inside of any package inside the workspace, e.g. a .github folder. I would expect that to format the whole workspace since it's unambiguous.

@sanmai-NL
Copy link

See also #2280.

@ethanuppal
Copy link

What are the updates on this?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

5 participants