Skip to main content

bv_builder/
resolve.rs

1use std::collections::{HashSet, VecDeque};
2
3use anyhow::{Context, Result, bail};
4use reqwest::Client;
5
6use crate::spec::{BuildSpec, PackageSpec, Platform, ResolvedPackage, ResolvedSpec};
7
8/// Resolve a `BuildSpec` to a fully pinned `ResolvedSpec` using the conda
9/// repodata from the declared channels.
10///
11/// Resolution strategy:
12/// 1. Download `repodata.json` for each channel + subdir.
13/// 2. BFS from the declared packages, resolving each transitive dependency.
14/// 3. Return a deterministically sorted `ResolvedSpec`.
15pub async fn resolve(spec: &BuildSpec) -> Result<ResolvedSpec> {
16    let direct = spec.package_specs()?;
17    let subdir = platform_subdir(&spec.platform);
18
19    let client = Client::builder()
20        .user_agent("bv-builder/0.1")
21        .timeout(std::time::Duration::from_secs(600))
22        .connect_timeout(std::time::Duration::from_secs(30))
23        .build()
24        .context("build HTTP client")?;
25
26    // Cache repodata to avoid re-downloading per package.
27    let mut repodata_cache: std::collections::HashMap<String, RepodataIndex> =
28        std::collections::HashMap::new();
29
30    let mut resolved_packages: Vec<ResolvedPackage> = Vec::new();
31    let mut resolved_names: HashSet<String> = HashSet::new();
32
33    // (name, is_direct)
34    let mut queue: VecDeque<(PackageSpec, bool)> = direct.into_iter().map(|p| (p, true)).collect();
35
36    while let Some((pkg_spec, is_direct)) = queue.pop_front() {
37        if resolved_names.contains(&pkg_spec.name) || is_virtual_package(&pkg_spec.name) {
38            continue;
39        }
40
41        let resolved = match resolve_package_cached(
42            &client,
43            &pkg_spec,
44            &spec.channels,
45            &subdir,
46            &mut repodata_cache,
47        )
48        .await
49        {
50            Ok(r) => r,
51            Err(e) if !is_direct => {
52                eprintln!("warning: skipping transitive dep '{}': {e}", pkg_spec.name);
53                resolved_names.insert(pkg_spec.name.clone());
54                continue;
55            }
56            Err(e) => return Err(e),
57        };
58
59        for dep_str in &resolved.depends {
60            if let Some(dep_spec) = parse_dep_spec(dep_str)
61                && !resolved_names.contains(&dep_spec.name)
62                && !is_virtual_package(&dep_spec.name)
63            {
64                queue.push_back((dep_spec, false));
65            }
66        }
67
68        resolved_names.insert(resolved.name.clone());
69        resolved_packages.push(resolved);
70    }
71
72    let base = spec.base.clone().or_else(|| {
73        Some(match &spec.platform {
74            crate::spec::Platform::LinuxAmd64 => {
75                "ghcr.io/tejasprabhune/bv-base/debian:12-slim".to_string()
76            }
77            crate::spec::Platform::LinuxArm64 => {
78                "ghcr.io/tejasprabhune/bv-base/debian:12-slim".to_string()
79            }
80        })
81    });
82
83    let mut out = ResolvedSpec {
84        name: spec.name.clone(),
85        version: spec.version.clone(),
86        platform: spec.platform.clone(),
87        channels: spec.channels.clone(),
88        packages: resolved_packages,
89        repodata_snapshot: None,
90        base,
91    };
92    out.sort_packages();
93    Ok(out)
94}
95
96/// Virtual/meta packages that don't have downloadable artifacts.
97// _openmp_mutex is excluded: though it ships no files, its libgomp dep must propagate.
98fn is_virtual_package(name: &str) -> bool {
99    name.starts_with("__") || matches!(name, "_libgcc_mutex" | "ca-certificates" | "certifi")
100}
101
102/// Parse a conda dependency string (e.g. "libgcc-ng >=12.3.0,<13.0a0") into a PackageSpec.
103fn parse_dep_spec(dep: &str) -> Option<PackageSpec> {
104    let dep = dep.trim();
105    // Strip trailing build string markers (e.g. " * nomkl")
106    let dep = dep.split(" * ").next().unwrap_or(dep);
107
108    let mut parts = dep.splitn(2, ' ');
109    let name = parts.next()?.trim().to_string();
110    if name.is_empty() {
111        return None;
112    }
113    let version_spec = parts.next().unwrap_or("*").trim().to_string();
114    Some(PackageSpec {
115        name,
116        version_spec: crate::spec::VersionSpec(version_spec),
117    })
118}
119
120/// Try each channel in order and return the first match, using a repodata cache.
121async fn resolve_package_cached(
122    client: &Client,
123    pkg_spec: &PackageSpec,
124    channels: &[String],
125    subdir: &str,
126    cache: &mut std::collections::HashMap<String, RepodataIndex>,
127) -> Result<ResolvedPackage> {
128    for channel in channels {
129        for try_subdir in [subdir, "noarch"] {
130            let repodata_url = format!("{channel}/{try_subdir}/repodata.json");
131            let repodata = if let Some(rd) = cache.get(&repodata_url) {
132                rd
133            } else {
134                let rd: RepodataIndex = match client.get(&repodata_url).send().await {
135                    Ok(resp) if resp.status().is_success() => resp
136                        .json()
137                        .await
138                        .with_context(|| format!("parse repodata from {repodata_url}"))?,
139                    _ => continue,
140                };
141                cache.insert(repodata_url.clone(), rd);
142                cache.get(&repodata_url).unwrap()
143            };
144
145            if let Some(pkg) = find_best_match(repodata, pkg_spec, channel, try_subdir) {
146                return Ok(pkg);
147            }
148        }
149    }
150    bail!(
151        "package '{}' with spec '{}' not found in any channel",
152        pkg_spec.name,
153        pkg_spec.version_spec
154    )
155}
156
157/// Find the best (latest) matching package entry in a repodata index.
158fn find_best_match(
159    repodata: &RepodataIndex,
160    pkg_spec: &PackageSpec,
161    channel: &str,
162    subdir: &str,
163) -> Option<ResolvedPackage> {
164    let spec_str = pkg_spec.version_spec.0.as_str();
165
166    let mut candidates: Vec<(&str, &RepodataPackageRecord)> = repodata
167        .packages_conda
168        .iter()
169        .chain(repodata.packages.iter())
170        .filter(|(_, rec)| rec.name == pkg_spec.name && version_matches(&rec.version, spec_str))
171        .map(|(fname, rec)| (fname.as_str(), rec))
172        .collect();
173
174    // Sort by version descending, then build descending → pick latest.
175    candidates.sort_by(|(_, a), (_, b)| {
176        compare_conda_version(&b.version, &a.version).then(b.build_number.cmp(&a.build_number))
177    });
178
179    candidates.first().map(|(filename, rec)| {
180        let url = format!("{channel}/{subdir}/{filename}");
181        ResolvedPackage {
182            name: rec.name.clone(),
183            version: rec.version.clone(),
184            build: rec.build.clone(),
185            channel: channel.to_string(),
186            url,
187            sha256: rec.sha256.clone().unwrap_or_default(),
188            filename: filename.to_string(),
189            depends: rec.depends.clone(),
190        }
191    })
192}
193
194/// Check if `version` satisfies a conda-style constraint spec.
195/// Handles `*`, `==X`, `>=X`, `>X`, `<=X`, `<X`, and comma-separated combinations.
196fn version_matches(version: &str, spec: &str) -> bool {
197    let spec = spec.trim();
198    if spec.is_empty() || spec == "*" {
199        return true;
200    }
201    for part in spec.split(',') {
202        let part = part.trim();
203        if let Some(bound) = part.strip_prefix(">=") {
204            if compare_conda_version(version, bound.trim()) == std::cmp::Ordering::Less {
205                return false;
206            }
207        } else if let Some(bound) = part.strip_prefix('>') {
208            if compare_conda_version(version, bound.trim()) != std::cmp::Ordering::Greater {
209                return false;
210            }
211        } else if let Some(bound) = part.strip_prefix("<=") {
212            if compare_conda_version(version, bound.trim()) == std::cmp::Ordering::Greater {
213                return false;
214            }
215        } else if let Some(bound) = part.strip_prefix('<') {
216            if compare_conda_version(version, bound.trim()) != std::cmp::Ordering::Less {
217                return false;
218            }
219        } else if let Some(exact) = part.strip_prefix("==")
220            && version != exact.trim()
221        {
222            return false;
223        } else if let Some(ne) = part.strip_prefix("!=")
224            && version == ne.trim()
225        {
226            return false;
227        }
228        // Unknown operator: skip conservatively
229    }
230    true
231}
232
233/// Compare two conda version strings using numeric segment ordering.
234///
235/// Splits on "." and compares each segment numerically.  Segments with a
236/// non-numeric suffix (e.g. "0a0", "0b1", "0rc1") are treated as
237/// pre-releases and sort before the matching numeric-only segment:
238///   "1.22.0a0" < "1.22.0"
239/// This matches conda's version ordering so that constraints like
240/// `<1.22.0a0` work correctly.
241fn compare_conda_version(a: &str, b: &str) -> std::cmp::Ordering {
242    let a_segs: Vec<(u64, bool)> = a.split('.').map(version_seg).collect();
243    let b_segs: Vec<(u64, bool)> = b.split('.').map(version_seg).collect();
244    let len = a_segs.len().max(b_segs.len());
245    for i in 0..len {
246        let (an, a_pre) = a_segs.get(i).copied().unwrap_or((0, false));
247        let (bn, b_pre) = b_segs.get(i).copied().unwrap_or((0, false));
248        match an.cmp(&bn) {
249            std::cmp::Ordering::Equal => match (a_pre, b_pre) {
250                (true, false) => return std::cmp::Ordering::Less,
251                (false, true) => return std::cmp::Ordering::Greater,
252                _ => {}
253            },
254            other => return other,
255        }
256    }
257    std::cmp::Ordering::Equal
258}
259
260/// Parse one dot-separated version segment into (numeric_value, is_prerelease).
261/// "21" → (21, false), "0a0" → (0, true), "0rc1" → (0, true).
262fn version_seg(seg: &str) -> (u64, bool) {
263    let digits: String = seg.chars().take_while(|c| c.is_ascii_digit()).collect();
264    let is_pre = digits.len() < seg.len();
265    (digits.parse().unwrap_or(0), is_pre)
266}
267
268fn platform_subdir(platform: &Platform) -> String {
269    match platform {
270        Platform::LinuxAmd64 => "linux-64".to_string(),
271        Platform::LinuxArm64 => "linux-aarch64".to_string(),
272    }
273}
274
275// Repodata index structures
276
277#[derive(Debug, serde::Deserialize)]
278struct RepodataIndex {
279    #[serde(default)]
280    pub packages: std::collections::HashMap<String, RepodataPackageRecord>,
281    #[serde(default, rename = "packages.conda")]
282    pub packages_conda: std::collections::HashMap<String, RepodataPackageRecord>,
283}
284
285#[derive(Debug, Clone, serde::Deserialize)]
286struct RepodataPackageRecord {
287    pub name: String,
288    pub version: String,
289    pub build: String,
290    #[serde(default)]
291    pub build_number: u32,
292    pub sha256: Option<String>,
293    #[serde(default)]
294    pub depends: Vec<String>,
295}
296
297#[cfg(test)]
298mod tests {
299    use super::*;
300
301    #[test]
302    fn version_matches_star() {
303        assert!(version_matches("1.19.2", "*"));
304        assert!(version_matches("1.19.2", ""));
305    }
306
307    #[test]
308    fn version_matches_exact() {
309        assert!(version_matches("1.19.2", "==1.19.2"));
310        assert!(!version_matches("1.18.0", "==1.19.2"));
311    }
312
313    #[test]
314    fn version_matches_gte() {
315        assert!(version_matches("1.21", ">=1.21"));
316        assert!(version_matches("1.21.0", ">=1.21"));
317        assert!(!version_matches("1.9", ">=1.21"));
318        assert!(!version_matches("1.20.5", ">=1.21"));
319    }
320
321    #[test]
322    fn version_matches_range() {
323        assert!(version_matches("1.21.0", ">=1.21,<1.22.0a0"));
324        assert!(!version_matches("1.9", ">=1.21,<1.22.0a0"));
325        assert!(!version_matches("1.22.0", ">=1.21,<1.22.0a0"));
326    }
327
328    #[test]
329    fn compare_numeric_version_order() {
330        use std::cmp::Ordering::*;
331        assert_eq!(compare_conda_version("1.21", "1.9"), Greater);
332        assert_eq!(compare_conda_version("1.9", "1.21"), Less);
333        assert_eq!(compare_conda_version("1.21.0", "1.21"), Equal);
334        assert_eq!(compare_conda_version("2.0.0", "1.99.99"), Greater);
335    }
336
337    #[test]
338    fn compare_prerelease_sorts_before_release() {
339        use std::cmp::Ordering::*;
340        assert_eq!(compare_conda_version("1.22.0a0", "1.22.0"), Less);
341        assert_eq!(compare_conda_version("1.22.0", "1.22.0a0"), Greater);
342        assert_eq!(compare_conda_version("1.21.0", "1.22.0a0"), Less);
343    }
344}