Skip to content

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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,16 @@ fn get_targets_root_only(
targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path)?;
// `workspace_root_path` is the path to the workspace's root directory
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
// `target_manifest` is the canonicalized path to a `Cargo.toml` file
let target_manifest = target_manifest.canonicalize()?;
(
workspace_root_path == target_manifest,
target_manifest.canonicalize()?,
target_manifest
.parent()
.is_some_and(|manifest| workspace_root_path == manifest),
Comment on lines +373 to +375
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.

target_manifest,
)
} else {
let current_dir = env::current_dir()?.canonicalize()?;
Expand Down
33 changes: 32 additions & 1 deletion src/cargo-fmt/test/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ mod all_targets {
source_root: &str,
exp_targets: &[ExpTarget],
exp_num_targets: usize,
strategy: CargoFmtStrategy,
) {
let root_path = Path::new("tests/cargo-fmt/source").join(source_root);
let get_path = |exp: &str| PathBuf::from(&root_path).join(exp).canonicalize().unwrap();
let manifest_path = Path::new(&root_path).join(manifest_suffix);
let targets = get_targets(&CargoFmtStrategy::All, Some(manifest_path.as_path()))
let targets = get_targets(&strategy, Some(manifest_path.as_path()))
.expect("Targets should have been loaded");

assert_eq!(targets.len(), exp_num_targets);
Expand Down Expand Up @@ -58,6 +59,7 @@ mod all_targets {
"divergent-crate-dir-names",
&exp_targets,
3,
CargoFmtStrategy::All,
);
}

Expand Down Expand Up @@ -113,6 +115,7 @@ mod all_targets {
"workspaces/path-dep-above",
&exp_targets,
6,
CargoFmtStrategy::All,
);
}

Expand All @@ -131,4 +134,32 @@ mod all_targets {
assert_correct_targets_loaded("ws/a/d/f/Cargo.toml");
}
}

mod cargo_fmt_strategy_root {
use super::*;

#[test]
fn assert_correct_targets_loaded_from_root() {
let exp_targets = &[
ExpTarget {
path: "inner_bin/src/main.rs",
edition: Edition::E2021,
kind: "bin",
},
ExpTarget {
path: "inner_lib/src/lib.rs",
edition: Edition::E2021,
kind: "lib",
},
];

super::assert_correct_targets_loaded(
"Cargo.toml",
"issues_6517",
exp_targets,
2,
CargoFmtStrategy::Root,
);
}
}
}
2 changes: 2 additions & 0 deletions tests/cargo-fmt/source/issues_6517/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["inner_bin", "inner_lib"]
6 changes: 6 additions & 0 deletions tests/cargo-fmt/source/issues_6517/inner_bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "inner_bin"
version = "0.1.0"
edition = "2021"

[dependencies]
3 changes: 3 additions & 0 deletions tests/cargo-fmt/source/issues_6517/inner_bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
6 changes: 6 additions & 0 deletions tests/cargo-fmt/source/issues_6517/inner_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "inner_lib"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions tests/cargo-fmt/source/issues_6517/inner_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Loading