Skip to content
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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Mar 29, 2025

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 on lines +373 to +375
target_manifest
.parent()
.is_some_and(|manifest| workspace_root_path == manifest),
Copy link
Contributor Author

@ytmimi ytmimi Mar 29, 2025

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.

Copy link
Contributor Author

@ytmimi ytmimi Mar 29, 2025

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.

ytmimi added 2 commits March 29, 2025 03:20
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.
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
2 participants