Skip to content

fix: handling of --all when local dep name and dir name differ #3664

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

Merged
merged 1 commit into from
Jul 6, 2019
Merged
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
34 changes: 24 additions & 10 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Erro
}

fn get_targets_root_only(targets: &mut BTreeSet<Target>) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(None)?;
let metadata = get_cargo_metadata(None, false)?;
let current_dir = env::current_dir()?.canonicalize()?;
let current_dir_manifest = current_dir.join("Cargo.toml");
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
Expand Down Expand Up @@ -282,7 +282,8 @@ fn get_targets_recursive(
mut targets: &mut BTreeSet<Target>,
visited: &mut BTreeSet<String>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path)?;
let metadata = get_cargo_metadata(manifest_path, false)?;
let metadata_with_deps = get_cargo_metadata(manifest_path, true)?;
Copy link
Member Author

@calebcartwright calebcartwright Jun 30, 2019

Choose a reason for hiding this comment

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

The dependency struct returned from the cargo_metadata crate unsurprisingly does not include info about the manifest location of the dependency.

As such, I figured I could make another call to cargo_metadata to get the metadata including dependencies since that cargo metadata will include the correct manifest path for the dependency, regardless of what package name/directory name are used. That "correct" manifest path is then used below.

Copy link
Contributor

@topecongiro topecongiro Jul 1, 2019

Choose a reason for hiding this comment

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

Calling cargo_metadata twice is a bit unfortunate, but I think this is the best we can do right now. A possible approach in the future would be to refactor cargo_metadata so that the cargo_metadata::Dependency contains the manifest location if the dependency is local

Copy link
Member Author

Choose a reason for hiding this comment

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

Completely agree 👍


for package in metadata.packages {
add_targets(&package.targets, &mut targets);
Expand All @@ -293,11 +294,19 @@ fn get_targets_recursive(
continue;
}

let mut manifest_path = PathBuf::from(&package.manifest_path);

manifest_path.pop();
manifest_path.push(&dependency.name);
manifest_path.push("Cargo.toml");
let dependency_package = metadata_with_deps
.packages
.iter()
.find(|p| p.name == dependency.name);
let manifest_path = if dependency_package.is_some() {
PathBuf::from(&dependency_package.unwrap().manifest_path)
} else {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if there's a circumstance where the dependency name wouldn't be found/when the if block woudln't be executed, but have this else block to fallback to the previous behavior just in case.

let mut package_manifest_path = PathBuf::from(&package.manifest_path);
package_manifest_path.pop();
package_manifest_path.push(&dependency.name);
package_manifest_path.push("Cargo.toml");
package_manifest_path
};

if manifest_path.exists() {
visited.insert(dependency.name);
Expand All @@ -313,7 +322,7 @@ fn get_targets_with_hitlist(
hitlist: &[String],
targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(None)?;
let metadata = get_cargo_metadata(None, false)?;

let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist);

Expand Down Expand Up @@ -399,9 +408,14 @@ fn run_rustfmt(
.unwrap_or(SUCCESS))
}

fn get_cargo_metadata(manifest_path: Option<&Path>) -> Result<cargo_metadata::Metadata, io::Error> {
fn get_cargo_metadata(
manifest_path: Option<&Path>,
include_deps: bool,
) -> Result<cargo_metadata::Metadata, io::Error> {
let mut cmd = cargo_metadata::MetadataCommand::new();
cmd.no_deps();
if !include_deps {
cmd.no_deps();
}
if let Some(manifest_path) = manifest_path {
cmd.manifest_path(manifest_path);
}
Expand Down