Skip to content

Handle cargo fmt --manifest-path when specifying the workspace's root Cargo.toml - #6524

Merged
ytmimi merged 3 commits into
rust-lang:mainfrom
ytmimi:issue_6517
Aug 11, 2026
Merged

Handle cargo fmt --manifest-path when specifying the workspace's root Cargo.toml#6524
ytmimi merged 3 commits into
rust-lang:mainfrom
ytmimi:issue_6517

Conversation

@ytmimi

@ytmimi ytmimi commented Mar 29, 2025

Copy link
Copy Markdown
Contributor

Fixes #6517

The underlying issue was an incorrect comparison between a Cargo.toml file path and the workspace's root directory path. Now that we're comparing two directory paths in_workspace_root gets set correctly, and cargo-fmt is able to find all the workspace's package targets.

PR best reviewed 1 commit at a time.

Comment thread src/cargo-fmt/main.rs Outdated
Comment on lines +373 to +375
target_manifest
.parent()
.is_some_and(|manifest| workspace_root_path == manifest),

@ytmimi ytmimi Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ytmimi ytmimi Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like that is acceptable behavior, considering the user explicitly signalled their intent. But maybe it could be a bit confusing?

@ytmimi ytmimi changed the title Correctly handle cargo fmt --manifest-path when running from the workspace root with the workspace's root Cargo.toml Handle cargo fmt --manifest-path when specifying the workspace's root Cargo.toml Mar 29, 2025
@jieyouxu jieyouxu added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed pr-not-reviewed labels Feb 21, 2026
@jieyouxu jieyouxu self-assigned this Jul 25, 2026

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me with a nit
@rustbot author

View changes since this review

Comment thread src/cargo-fmt/main.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 4, 2026
@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

ytmimi added 3 commits August 10, 2026 20:09
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.
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

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.

@ytmimi

ytmimi commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@jieyouxu thank you for reviewing!

@ytmimi
ytmimi added this pull request to the merge queue Aug 11, 2026
Merged via the queue into rust-lang:main with commit 98136fa Aug 11, 2026
27 checks passed
@rustbot rustbot added release-notes Needs an associated changelog entry and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes Needs an associated changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cargo fmt with specified manifest-path does not work in workspaces with bin and lib packages

3 participants