Handle cargo fmt --manifest-path when specifying the workspace's root Cargo.toml - #6524
Conversation
| target_manifest | ||
| .parent() | ||
| .is_some_and(|manifest| workspace_root_path == manifest), |
There was a problem hiding this comment.
The way this check is currently implemented a user could be in a sub package and run cargo fmt --manifest-path ../path/to/root/Cargo.toml and that would find all the targets in the workspace and format them. I'm not sure if that's something we want to allow, or if we should check that the user's env::current_dir() is also in the workspace's root.
There was a problem hiding this comment.
In fact, a user could be in a sub-package of one workspace (or even a non-cargo project) and specify cargo fmt --manifest-path ../../path/to/some/other/projects/Cargo.toml, and I think that'll format the files in the other project. Again, just want to double check if that's expected behavior.
There was a problem hiding this comment.
I feel like that is acceptable behavior, considering the user explicitly signalled their intent. But maybe it could be a bit confusing?
cargo fmt --manifest-path when running from the workspace root with the workspace's root Cargo.tomlcargo fmt --manifest-path when specifying the workspace's root Cargo.toml
There was a problem hiding this comment.
Thanks, this looks good to me with a nit
@rustbot author
|
Reminder, once the PR becomes ready for a review, use |
The test case loads package targets from the root of a workspace.
Previously, when running `cargo fmt --manifest-path Cargo.toml` from a cargo workspace's root directory `cargo fmt` would error with a message that read `Failed to find targets`. The issue stemmed from an incorrect comparison between the path to the workspace's root **directory** and the path to the specified `Cargo.toml` **file**. This lead `cargo fmt` to incorrectly determine that the command wasn't being run from the workspace's root.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@jieyouxu thank you for reviewing! |
Fixes #6517
The underlying issue was an incorrect comparison between a
Cargo.tomlfile path and the workspace's root directory path. Now that we're comparing two directory pathsin_workspace_rootgets set correctly, andcargo-fmtis able to find all the workspace's package targets.PR best reviewed 1 commit at a time.