Skip to content

feat: allow searching for specific package versions using matchspec #3565

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions docs/reference/cli/pixi/search_extender
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pixi search pixi
pixi search --limit 30 "py*"
# search in a different channel and for a specific platform
pixi search -c robostack --platform linux-64 "plotjuggler*"
# search for a specific version of a package
pixi search "rattler-build<=0.35.4"
pixi search "rattler-build[build=ha8cf89e_0]"
```

--8<-- [end:example]
27 changes: 20 additions & 7 deletions src/cli/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use miette::{IntoDiagnostic, Report};
use pixi_config::{default_channel_config, Config};
use pixi_progress::await_in_progress;
use pixi_utils::reqwest::build_reqwest_clients;
use rattler_conda_types::{MatchSpec, PackageName, Platform, RepoDataRecord};
use rattler_conda_types::{MatchSpec, PackageName, ParseStrictness, Platform, RepoDataRecord};
use rattler_lock::Matches;
use rattler_repodata_gateway::{GatewayError, RepoData};
use regex::Regex;
use strsim::jaro;
Expand Down Expand Up @@ -188,8 +189,9 @@ pub async fn execute_impl<W: Write>(
// If package name filter doesn't contain * (wildcard), it will search and display specific
// package info (if any package is found)
else {
let package_name = PackageName::try_from(package_name_filter).into_diagnostic()?;
search_exact_package(package_name, all_names, repodata_query_func, out).await?
let package_spec = MatchSpec::from_str(&package_name_filter, ParseStrictness::Lenient)
.into_diagnostic()?;
search_exact_package(package_spec, all_names, repodata_query_func, out).await?
};

Ok(packages)
Expand All @@ -202,7 +204,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
}

async fn search_exact_package<W: Write, QF, FR>(
package_name: PackageName,
package_spec: MatchSpec,
all_repodata_names: Vec<PackageName>,
repodata_query_func: QF,
out: &mut W,
Expand All @@ -211,7 +213,10 @@ where
QF: Fn(Vec<MatchSpec>) -> FR,
FR: Future<Output = Result<Vec<RepoData>, GatewayError>>,
{
let package_name_search = package_name.clone();
let package_name_search = package_spec.name.clone().ok_or_else(|| {
miette::miette!("could not find package name in MatchSpec {}", package_spec)
})?;

let packages = search_package_by_filter(
&package_name_search,
all_repodata_names,
Expand All @@ -220,9 +225,16 @@ where
false,
)
.await?;

if packages.is_empty() {
let normalized_package_name = package_name_search.as_normalized();
return Err(miette::miette!("Package {normalized_package_name} not found, please use a wildcard '*' in the search name for a broader result."));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does that actually work?

Copy link
Contributor

Choose a reason for hiding this comment

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

If yes, please add a test for it :)

Copy link
Author

Choose a reason for hiding this comment

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

Hi, I moved this error message up unchanged from line 245 in order to differentiate between the package itself not being found and the match spec not matching. I did not implement providing a match spec for the wildcard search, only the exact one. Do you think the error message is now misleading in that regard? I'm happy to adjust it if you have suggestions for the wording. :)

}

// Sort packages by version, build number and build string
let packages = packages
.iter()
.filter(|&p| package_spec.matches(p))
.sorted_by(|a, b| {
Ord::cmp(
&(
Expand All @@ -241,8 +253,9 @@ where
.collect::<Vec<RepoDataRecord>>();

if packages.is_empty() {
let normalized_package_name = package_name.as_normalized();
return Err(miette::miette!("Package {normalized_package_name} not found, please use a wildcard '*' in the search name for a broader result."));
return Err(miette::miette!(
"Package found, but MatchSpec {package_spec} does not match any record."
));
}

let newest_package = packages.last();
Expand Down
70 changes: 70 additions & 0 deletions tests/integration_rust/search_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,76 @@ async fn search_return_latest_across_everything() {
assert_eq!(found_package.package_record.version.as_str(), "4");
}

#[tokio::test]
async fn search_using_match_spec() {
let mut package_database = PackageDatabase::default();

// Add a package `foo` with different versions and different builds
package_database.add_package(
Package::build("foo", "0.1.0")
.with_build("h60d57d3_0")
.finish(),
);
package_database.add_package(
Package::build("foo", "0.1.0")
.with_build("h60d57d3_1")
.finish(),
);
package_database.add_package(
Package::build("foo", "0.2.0")
.with_build("h60d57d3_0")
.finish(),
);
package_database.add_package(
Package::build("foo", "0.2.0")
.with_build("h60d57d3_1")
.finish(),
);

// Write the repodata to disk
let temp_dir = TempDir::new().unwrap();
let channel_dir = temp_dir.path().join("channel");
package_database.write_repodata(&channel_dir).await.unwrap();
let channel = Url::from_file_path(channel_dir).unwrap();
let platform = Platform::current();
let pixi = PixiControl::from_manifest(&format!(
r#"
[project]
name = "test-search-using-match-spec"
channels = ["{channel}"]
platforms = ["{platform}"]

"#
))
.unwrap();

// Without match spec the latest version is returned
let binding = pixi.search("foo".to_string()).await.unwrap().unwrap();
let found_package = binding.last().unwrap();
assert_eq!(found_package.package_record.version.as_str(), "0.2.0");
assert_eq!(found_package.package_record.build.as_str(), "h60d57d3_1");

// Search for a specific version
let binding = pixi
.search("foo<=0.1.0".to_string())
.await
.unwrap()
.unwrap();
let found_package = binding.last().unwrap();
assert_eq!(found_package.package_record.version.as_str(), "0.1.0");
assert_eq!(found_package.package_record.build.as_str(), "h60d57d3_1");

// Search for a specific build
let binding = pixi
.search("foo[build=h60d57d3_0]".to_string())
.await
.unwrap()
.unwrap();
let found_package = binding.last().unwrap();
assert_eq!(found_package.package_record.version.as_str(), "0.2.0");
assert_eq!(found_package.package_record.build.as_str(), "h60d57d3_0");
}

#[tokio::test]
async fn test_search_multiple_versions() {
let mut package_database = PackageDatabase::default();
Expand Down
Loading