Skip to content
Merged
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
54 changes: 43 additions & 11 deletions src/cargo/ops/registry/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,40 @@ pub fn info(
// For workspace members, `cargo tree --package <SPEC> --invert` is useless. It only prints itself.
let suggest_cargo_tree_command = package_id.is_some() && !is_member;

let summaries = query_summaries(spec, &mut registry, &source_ids)?;
let (summaries, normalized_name) = query_summaries(spec, &mut registry, &source_ids)?;
let normalized_spec = match normalized_name {
Some(name) if name != spec.name() => {
let mut normalized_spec = PackageIdSpec::new(name);

if let Some(version) = spec.partial_version().cloned() {
normalized_spec = normalized_spec.with_version(version);
}

if let Some(url) = spec.url().cloned() {
normalized_spec = normalized_spec.with_url(url);
}

if let Some(kind) = spec.kind().cloned() {
normalized_spec = normalized_spec.with_kind(kind);
}

normalized_spec
}
_ => spec.clone(),
};
let package_id = match package_id {
Some(id) => id,
None => find_pkgid_in_summaries(&summaries, spec, &rustc_version, &source_ids)?,
None => find_pkgid_in_summaries(&summaries, &normalized_spec, &rustc_version, &source_ids)?,
};

if package_id.name() != spec.name() {
gctx.shell().warn(format!(
"translating `{}` to `{}`",
spec.name(),
package_id.name(),
))?;
}

let package = registry.get(&[package_id])?;
let package = package.get_one(package_id)?;
pretty_view(package, &summaries, suggest_cargo_tree_command, gctx)?;
Expand Down Expand Up @@ -143,13 +171,13 @@ fn find_pkgid_in_ws(

fn find_pkgid_in_summaries(
summaries: &[IndexSummary],
spec: &PackageIdSpec,
normalized_spec: &PackageIdSpec,
rustc_version: &PartialVersion,
source_ids: &RegistrySourceIds,
) -> CargoResult<PackageId> {
let summary = summaries
.iter()
.filter(|s| spec.matches(s.package_id()))
.filter(|s| normalized_spec.matches(s.package_id()))
.max_by(|s1, s2| {
// Check the MSRV compatibility.
let s1_matches = s1
Expand Down Expand Up @@ -177,7 +205,7 @@ fn find_pkgid_in_summaries(
None => {
anyhow::bail!(
"could not find `{}` in registry `{}`",
spec,
normalized_spec,
source_ids.original.url()
)
}
Expand All @@ -188,18 +216,22 @@ fn query_summaries(
spec: &PackageIdSpec,
registry: &mut PackageRegistry<'_>,
source_ids: &RegistrySourceIds,
) -> CargoResult<Vec<IndexSummary>> {
) -> CargoResult<(Vec<IndexSummary>, Option<String>)> {
// Query without version requirement to get all index summaries.
let dep = Dependency::parse(spec.name(), None, source_ids.original)?;
loop {
// Exact to avoid returning all for path/git
match registry.query_vec(&dep, QueryKind::Exact) {
let results = loop {
// Use normalized crate name lookup for user-provided package names.
match registry.query_vec(&dep, QueryKind::Normalized) {
std::task::Poll::Ready(res) => {
break res;
break res?;
}
std::task::Poll::Pending => registry.block_until_ready()?,
}
}
};

let normalized_name = results.first().map(|s| s.package_id().name().to_string());

Ok((results, normalized_name))
}

fn validate_locked_and_frozen_options(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::prelude::*;
use cargo_test_support::file;

use super::init_registry_without_token;

#[cargo_test]
fn case() {
init_registry_without_token();
cargo_test_support::registry::Package::new("my_crate", "0.1.0")
.file(
"Cargo.toml",
r#"
[package]
name = "my_crate"
version = "0.1.0"
description = "A package for testing"
repository = "https://github.com/hi-rustin/cargo-infromation"
documentation = "https://docs.rs/my-package/0.1.0"
license = "MIT"
edition = "2018"
rust-version = "1.50.0"
keywords = ["foo", "bar", "baz"]

[features]
default = ["feature1"]
feature1 = []
feature2 = []

[dependencies]
foo = "0.1.0"
bar = "0.2.0"
baz = { version = "0.3.0", optional = true }

[[bin]]
name = "my_bin"

[lib]
name = "my_lib"
"#,
)
.file("src/bin/my_bin.rs", "")
.file("src/lib.rs", "")
.publish();

snapbox::cmd::Command::cargo_ui()
.arg("info")
.arg("my-crate") //hyphen on purpose to show the error
.arg("--registry=dummy-registry")
.assert()
.success()
.stdout_eq(file!["stdout.term.svg"])
.stderr_eq(file!["stderr.term.svg"]);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::prelude::*;
use cargo_test_support::file;

use super::init_registry_without_token;

#[cargo_test]
fn case() {
init_registry_without_token();
cargo_test_support::registry::Package::new("my-package", "0.1.0")
.file(
"Cargo.toml",
r#"
[package]
name = "my-package"
version = "0.1.0"
description = "A package for testing"
repository = "https://github.com/hi-rustin/cargo-infromation"
documentation = "https://docs.rs/my-package/0.1.0"
license = "MIT"
edition = "2018"
rust-version = "1.50.0"
keywords = ["foo", "bar", "baz"]

[features]
default = ["feature1"]
feature1 = []
feature2 = []

[dependencies]
foo = "0.1.0"
bar = "0.2.0"
baz = { version = "0.3.0", optional = true }

[[bin]]
name = "my_bin"

[lib]
name = "my_lib"
"#,
)
.file("src/bin/my_bin.rs", "")
.file("src/lib.rs", "")
.publish();

snapbox::cmd::Command::cargo_ui()
.arg("info")
.arg("my_package") //underscore on purpose to show the error
.arg("--registry=dummy-registry")
.assert()
.success()
.stdout_eq(file!["stdout.term.svg"])
.stderr_eq(file!["stderr.term.svg"]);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod basic;
mod crate_name_normalization_from_hyphen_to_underscore;
mod crate_name_normalization_from_underscore_to_hyphen;
mod features;
mod features_activated_over_limit;
mod features_activated_over_limit_verbose;
Expand Down