Skip to content

cargo fmt only works in root directory of project #3647

New issue

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

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

Already on GitHub? Sign in to your account

Closed
milkey-mouse opened this issue Jun 23, 2019 · 6 comments · Fixed by #3652
Closed

cargo fmt only works in root directory of project #3647

milkey-mouse opened this issue Jun 23, 2019 · 6 comments · Fixed by #3652
Labels
bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors
Milestone

Comments

@milkey-mouse
Copy link

milkey-mouse commented Jun 23, 2019

I often leave my shell in the project/src directory so I don't have to prepend src every time I open a file. Other cargo commands such as cargo run and cargo check work fine in the src directory, but cargo fmt does not:

~/projects/planets $ cargo fmt  # works
~/projects/planets $ cargo run  # works
~/projects/planets $ cd src
~/projects/planets/src $ cargo run  # still works
~/projects/planets/src $ cargo fmt
Failed to find targets
cargo-fmt 1.3.0
This utility formats all bin and lib files of the current crate using rustfmt.
# etc., etc.

cargo fmt would more closely resemble other cargo subcommands (and git subcommands, to name another tool with this behavior) if it recursed upwards to find the Cargo.toml first.

I'm using a recent nightly version of rustc, cargo, & rustfmt:

$ rustc --version
rustc 1.37.0-dev (8d6f4b96d 2019-06-21)
$ cargo --version
cargo 1.37.0-dev (807429e1b 2019-06-11)
$ rustfmt --version
rustfmt 1.3.0-dev (d3345024 2019-06-09)
$ cargo fmt --version
rustfmt 1.3.0-dev (d3345024 2019-06-09)
@topecongiro topecongiro added bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors labels Jun 24, 2019
@topecongiro topecongiro added this to the 1.3.1 milestone Jun 24, 2019
@calebcartwright
Copy link
Member

I suspect this has some relation to the workspace issues and the CargoFmtStrategy::Root strategy.

I'd be happy to work on this one @topecongiro

@topecongiro
Copy link
Contributor

@calebcartwright Great! IIRC cargo fmt searches Cargo.toml recursively only when --all is passed to it. We should change it so that cargo fmt will always look for Cargo.toml recursively.

@milkey-mouse
Copy link
Author

That was fast. Thanks!

@jeffw387
Copy link

This is happening to me in 1.4.14. It was happening in 1.4.13 as well, as I just updated to try to see if that fixed the issue. Is it possible there was a regression?

@lnshi
Copy link

lnshi commented Nov 24, 2021

sorry for commenting on the closed issue, but seems this is happening again?

rustc 1.56.1 (59eed8a2a 2021-11-01)
cargo 1.56.0 (4ed5d137b 2021-10-04)

my setup:

proj_root/
└── rsmain
    ├── Cargo.lock
    ├── Cargo.toml // worspace
    ├── core // crate
    ├── target
    └── testutils // crate

I want to run the cargo fmt and cargo test from the proj_root, which:

cargo test --manifest-path=rsmain/Cargo.toml works

but cargo fmt --manifest-path=rsmain/Cargo.toml gives

Failed to find targets
cargo-fmt 1.4.37

error, i have to do cargo fmt --all --manifest-path=rsmain/Cargo.toml then it works

@topecongiro @calebcartwright FYI

@calebcartwright
Copy link
Member

@lnshi - couple things to note

It's alright (in some cases) to post to a closed issue, but you don't need to tag maintainers on issues as GitHub already handles notifications. That's especially true for folks you've been tagging recently in various issues, like Nick and Seiichi, as they haven't been involved with the project for quite some time.

It looks like your post is in reference to a manifest path target that is a workspace, which is a different use case than the issue reported here, despite the similarity in symptoms. I suspect your report is a duplicate of #4432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants