-
Notifications
You must be signed in to change notification settings - Fork 925
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
topecongiro
merged 1 commit into
rust-lang:master
from
calebcartwright:disparate-dep-names
Jul 6, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()?; | ||
|
@@ -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)?; | ||
|
||
for package in metadata.packages { | ||
add_targets(&package.targets, &mut targets); | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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); | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 refactorcargo_metadata
so that thecargo_metadata::Dependency
contains the manifest location if the dependency is localThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree 👍