Don't assume the first package in the result of cargo metadata is the current crate.
Instead find the one with the manifest path that matches the --manifest-path argument or the current directory. Fixes #1247
This commit is contained in:
@@ -20,7 +20,7 @@ pub struct Package {
|
|||||||
pub dependencies: Vec<Dependency>,
|
pub dependencies: Vec<Dependency>,
|
||||||
pub targets: Vec<Target>,
|
pub targets: Vec<Target>,
|
||||||
features: HashMap<String, Vec<String>>,
|
features: HashMap<String, Vec<String>>,
|
||||||
manifest_path: String,
|
pub manifest_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcDecodable, Debug)]
|
#[derive(RustcDecodable, Debug)]
|
||||||
@@ -65,10 +65,10 @@ impl From<json::DecoderError> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn metadata(manifest_path: Option<String>) -> Result<Metadata, Error> {
|
pub fn metadata(manifest_path_arg: &Option<String>) -> Result<Metadata, Error> {
|
||||||
let mut cmd = Command::new("cargo");
|
let mut cmd = Command::new("cargo");
|
||||||
cmd.arg("metadata").arg("--no-deps");
|
cmd.arg("metadata").arg("--no-deps");
|
||||||
if let Some(ref mani) = manifest_path {
|
if let Some(ref mani) = *manifest_path_arg {
|
||||||
cmd.arg(mani);
|
cmd.arg(mani);
|
||||||
}
|
}
|
||||||
let output = cmd.output()?;
|
let output = cmd.output()?;
|
||||||
|
|||||||
26
src/main.rs
26
src/main.rs
@@ -138,10 +138,30 @@ pub fn main() {
|
|||||||
|
|
||||||
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
||||||
// this arm is executed on the initial call to `cargo clippy`
|
// this arm is executed on the initial call to `cargo clippy`
|
||||||
let manifest_path = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
|
let manifest_path_arg = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
|
||||||
let mut metadata = cargo::metadata(manifest_path).expect("could not obtain cargo metadata");
|
|
||||||
|
let mut metadata = cargo::metadata(&manifest_path_arg).expect("could not obtain cargo metadata");
|
||||||
assert_eq!(metadata.version, 1);
|
assert_eq!(metadata.version, 1);
|
||||||
for target in metadata.packages.remove(0).targets {
|
|
||||||
|
let manifest_path = manifest_path_arg.map(|arg| PathBuf::from(Path::new(&arg["--manifest-path=".len()..])));
|
||||||
|
|
||||||
|
let current_dir = std::env::current_dir();
|
||||||
|
|
||||||
|
let package_index = metadata.packages.iter()
|
||||||
|
.position(|package| {
|
||||||
|
let package_manifest_path = Path::new(&package.manifest_path);
|
||||||
|
if let Some(ref manifest_path) = manifest_path {
|
||||||
|
package_manifest_path == manifest_path
|
||||||
|
} else if let Ok(ref current_dir) = current_dir {
|
||||||
|
let package_manifest_directory = package_manifest_path.parent().expect("could not find parent directory of package manifest");
|
||||||
|
package_manifest_directory == current_dir
|
||||||
|
} else {
|
||||||
|
panic!("could not read current directory")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.expect("could not find matching package");
|
||||||
|
let package = metadata.packages.remove(package_index);
|
||||||
|
for target in package.targets {
|
||||||
let args = std::env::args().skip(2);
|
let args = std::env::args().skip(2);
|
||||||
if let Some(first) = target.kind.get(0) {
|
if let Some(first) = target.kind.get(0) {
|
||||||
if target.kind.len() > 1 || first.ends_with("lib") {
|
if target.kind.len() > 1 || first.ends_with("lib") {
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ use clippy_lints::utils::cargo;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
|
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
|
||||||
let clippy_meta = cargo::metadata(None).expect("could not obtain cargo metadata");
|
let clippy_meta = cargo::metadata(&None).expect("could not obtain cargo metadata");
|
||||||
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
|
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
|
||||||
let clippy_lints_meta = cargo::metadata(None).expect("could not obtain cargo metadata");
|
let clippy_lints_meta = cargo::metadata(&None).expect("could not obtain cargo metadata");
|
||||||
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
|
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
|
||||||
for package in &clippy_meta.packages[0].dependencies {
|
for package in &clippy_meta.packages[0].dependencies {
|
||||||
if package.name == "clippy_lints" {
|
if package.name == "clippy_lints" {
|
||||||
|
|||||||
Reference in New Issue
Block a user