mamba-org / rattler

Rust crates to work with the Conda ecosystem.
BSD 3-Clause "New" or "Revised" License
226 stars 42 forks source link

Gateway.query in Python returns all versions #755

Closed JeanChristopheMorinPerso closed 16 hours ago

JeanChristopheMorinPerso commented 2 weeks ago

Hello, I was trying out the Gateway.query method and I noticed that it seems to return all versions of a package even if I use a spec like _go_select[md5=73a9dbf6acbed92b5e45e336f0372bcf].

Example:

>>> import rattler
>>> gateway = rattler.Gateway()
>>> import asyncio
>>> asyncio.run(gateway.query(["https://repo.anaconda.com/pkgs/main"], ["linux-64", "noarch"], ["_go_select[md5=73a9dbf6acbed92b5e45e336f0372bcf]"]))

[[RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.1.0-precompiled.tar.bz2"), RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.2.0-nocgo.tar.bz2"), RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.3.0-cgo.tar.bz2"), RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.1.0-precompiled.conda"), RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.2.0-nocgo.conda"), RepoDataRecord(url="https://repo.anaconda.com/pkgs/main/linux-64/_go_select-2.3.0-cgo.conda")], []]

Shouldn't it return a single record in this case or at best 2 (the .tar.bz2 and the .conda)?

I also tried with a matchspec like _go_select=2.3.0=cgo and I get the same result.

baszalmstra commented 2 weeks ago

Yes! That seems like a bug.

JeanChristopheMorinPerso commented 2 weeks ago

Could the problem be that https://github.com/mamba-org/rattler/blob/1630c43f89496bb46cf876e76932ee08b47fbd30/crates/rattler_repodata_gateway/src/gateway/query.rs#L223-L253 doesn't filter fetched records based on the input spec?

baszalmstra commented 2 weeks ago

yeah indeed! it seems to only be used to filter fetching dependencies.

JeanChristopheMorinPerso commented 2 weeks ago

I'm currently learning rust (I'm very early in the process) and tried something. This patch seems to work, though it doesn't feel "right" to me.

diff --git a/crates/rattler_repodata_gateway/src/gateway/query.rs b/crates/rattler_repodata_gateway/src/gateway/query.rs
index 2a0847e..5d37e91 100644
--- a/crates/rattler_repodata_gateway/src/gateway/query.rs
+++ b/crates/rattler_repodata_gateway/src/gateway/query.rs
@@ -247,8 +247,15 @@ impl GatewayQuery {
                     // Add the records to the result
                     if records.len() > 0 {
                         let result = &mut result[result_idx];
-                        result.len += records.len();
-                        result.shards.push(records);
+
+                        for record in records.iter() {
+                            if !request_specs.iter().any(|spec| spec.matches(record)) {
+                                // Do not recurse into records that do not match to root spec.
+                                continue;
+                            }
+                            result.len += 1;
+                            result.shards.push(Arc::new([record.clone()]));
+                        }
                     }
                 }