Skip to content

Commit 20d958d

Browse files
committed
correctly compare directory paths in get_targets_root_only
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.
1 parent 7cb2548 commit 20d958d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/cargo-fmt/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,16 @@ fn get_targets_root_only(
364364
targets: &mut BTreeSet<Target>,
365365
) -> Result<(), io::Error> {
366366
let metadata = get_cargo_metadata(manifest_path)?;
367+
// `workspace_root_path` is the path to the workspace's root directory
367368
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
368369
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
370+
// `target_manifest` is the canonicalized path to a `Cargo.toml` file
371+
let target_manifest = target_manifest.canonicalize()?;
369372
(
370-
workspace_root_path == target_manifest,
371-
target_manifest.canonicalize()?,
373+
target_manifest
374+
.parent()
375+
.is_some_and(|manifest| workspace_root_path == manifest),
376+
target_manifest,
372377
)
373378
} else {
374379
let current_dir = env::current_dir()?.canonicalize()?;

0 commit comments

Comments
 (0)