mars-schema 0.35.1

Pure schema reader and editor for Mars agent packages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Which version of one source: the lock replay rules and the ladder of attempts (pin, lock,
//! tags, default branch), each an ordered fallback that warns instead of failing, except under
//! `--frozen`, where every fallback is an error.

use std::collections::HashSet;

use semver::Version;

use super::package::{Flow, Pending, Run, Stop};
use super::{
    Dependency, Failure, Level, LockedDependency, Mode, Need, Origin, Ref, Requester, SourceId,
    Spec,
};
use crate::git_url::canonicalize_git_url;
use crate::version::{
    Constraint, SelectError, SelectionPolicy, allows, keeps_lock, parse_tag, select, settle,
};

/// How a source's version is decided: the choice among tags, or `--frozen`, which replays the
/// lock exactly and is the resolver's own rule, not a way of choosing.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum VersionMode {
    Choose(SelectionPolicy),
    Frozen,
}

impl Mode {
    pub(super) fn version_mode(&self, name: &str) -> VersionMode {
        match self {
            Self::Frozen => VersionMode::Frozen,
            Self::Upgrade { targets } if targets.is_empty() || targets.contains(name) => {
                VersionMode::Choose(SelectionPolicy::LatestOnly)
            }
            Self::Sync | Self::Upgrade { .. } => {
                VersionMode::Choose(SelectionPolicy::PreferLockThenLatest)
            }
        }
    }
}

/// A selection can fail as a conflict, which the caller may report as the engine exclusions that
/// caused it.
pub(super) enum SingleError {
    Conflict(Box<Failure>),
    Stop(Stop),
}

impl From<Stop> for SingleError {
    fn from(stop: Stop) -> Self {
        Self::Stop(stop)
    }
}

impl SingleError {
    pub(super) fn stop(self, run: &mut Run<'_>) -> Stop {
        match self {
            Self::Stop(stop) => stop,
            Self::Conflict(failure) => match run.fail::<()>(*failure) {
                Err(stop) => stop,
                Ok(()) => unreachable!("a failure always leaves the branch"),
            },
        }
    }
}

type Single<T> = Result<T, SingleError>;

/// The path a package's files are read at: a plugin reads the whole repository (the marketplace's
/// root manifest, sentinel and local plugin folders), anything else its own folder.
fn read_path(dep: &Dependency) -> Option<&str> {
    if dep.plugin.is_some() {
        None
    } else {
        dep.subpath.as_ref().map(|subpath| subpath.as_str())
    }
}

impl<'a> Run<'a> {
    pub(super) fn resolve_single_source(&mut self, pending: &Pending<'_>) -> Single<Ref> {
        let dep = pending.dep;
        let mode = self.req.mode.version_mode(&dep.name);
        match &dep.spec {
            Spec::Path(path) => {
                let (canonical, _) = self.path_source(dep, path, None, dep.declared_in.as_ref())?;
                Ok(Ref {
                    version: None,
                    tag: None,
                    commit: None,
                    origin: Origin::Path { dir: canonical },
                })
            }
            Spec::Git { url, .. } => {
                // Upgrade targets intentionally ignore lock replay.
                let locked = match mode {
                    VersionMode::Choose(SelectionPolicy::LatestOnly) => None,
                    _ => self.locked_for(pending, mode)?,
                };
                self.resolve_git_source(pending, url, locked, mode)
            }
        }
    }

    pub(super) fn locked_for(
        &mut self,
        pending: &Pending<'_>,
        mode: VersionMode,
    ) -> Single<Option<&'a LockedDependency>> {
        let req = self.req;
        let Some(locked) = req.locked.get(&pending.dep.name) else {
            return Ok(None);
        };
        if locked_matches(locked, pending.id) {
            return Ok(Some(locked));
        }
        let locked_identity = identity_string(locked);
        let expected = pending.id.to_string();
        if mode == VersionMode::Frozen {
            return Err(self
                .fail::<()>(Failure::Frozen {
                    message: format!(
                        "--frozen lock entry for source `{}` does not match current source identity (lock: {locked_identity}, current: {expected})",
                        pending.dep.name
                    ),
                })
                .unwrap_err()
                .into());
        }
        self.note(
            Level::Warning,
            "stale-lock-source-identity",
            format!(
                "ignoring stale lock entry for `{}`: lock identity {locked_identity} does not match current identity {expected}",
                pending.dep.name
            ),
            false,
        );
        Ok(None)
    }

    fn frozen<T>(&mut self, message: String) -> Single<T> {
        Err(self
            .fail::<T>(Failure::Frozen { message })
            .err()
            .expect("fail never returns a value")
            .into())
    }

    /// The commit's files, or `None` when the repository does not have it.
    fn fetch_commit(&mut self, dep: &Dependency, url: &str, commit: &str) -> Flow<Option<()>> {
        Ok(self.tree(url, commit, read_path(dep))?.map(|_| ()))
    }

    fn unreachable<T>(&mut self, dep: &Dependency, url: &str, commit: &str) -> Flow<T> {
        let package = self.current_package();
        self.fail(Failure::Unavailable {
            package,
            need: Need::Tree {
                url: url.to_string(),
                commit: commit.to_string(),
                path: read_path(dep).map(str::to_string),
            },
            reason: "the commit does not exist".to_string(),
        })
    }

    /// A commit known to exist, fetched as `commit` of `url`.
    fn at_commit(&mut self, dep: &Dependency, url: &str, commit: &str) -> Flow<Ref> {
        if self.fetch_commit(dep, url, commit)?.is_none() {
            return self.unreachable(dep, url, commit);
        }
        Ok(git_ref(url, commit))
    }

    fn fetch_ref(&mut self, dep: &Dependency, url: &str, name: &str) -> Flow<Ref> {
        let commit = self.reference(url, name)?;
        self.at_commit(dep, url, &commit)
    }

    /// The lock row names a commit the remote lacks: say so, and resolve from the tags.
    fn locked_unreachable(&mut self, url: &str, commit: &str) {
        self.note(
            Level::Warning,
            "locked-commit-unreachable",
            format!(
                "locked commit {commit} for {url} is unreachable; re-resolving from current tags"
            ),
            false,
        );
    }

    fn replay(
        &mut self,
        dep: &Dependency,
        url: &str,
        commit: &str,
        version: &Version,
        raw: Option<&str>,
    ) -> Flow<Option<Ref>> {
        if self.fetch_commit(dep, url, commit)?.is_none() {
            return Ok(None);
        }
        Ok(Some(Ref {
            version: Some(version.clone()),
            tag: Some(
                raw.map(ToOwned::to_owned)
                    .unwrap_or_else(|| format!("v{version}")),
            ),
            ..git_ref(url, commit)
        }))
    }

    fn resolve_ref_pin(
        &mut self,
        pending: &Pending<'_>,
        url: &str,
        ref_name: &str,
        locked: Option<&LockedDependency>,
        mode: VersionMode,
    ) -> Single<Ref> {
        let dep = pending.dep;
        let name = &dep.name;
        let locked_commit = locked.and_then(|source| source.commit.as_deref());
        let locked_selector = locked.and_then(|source| source.version.as_deref());
        let selector_matches_lock = locked_selector.is_some_and(|selector| selector == ref_name);
        let preferred = match mode {
            VersionMode::Choose(SelectionPolicy::LatestOnly) => None,
            VersionMode::Choose(SelectionPolicy::PreferLockThenLatest) => {
                if locked_commit.is_some() && !selector_matches_lock {
                    let selector = locked_selector.unwrap_or("<missing>");
                    self.note(
                        Level::Warning,
                        "locked-ref-selector-mismatch",
                        format!(
                            "ignoring locked commit for ref-pinned source `{name}` ({url}): lock selector `{selector}` does not match requested selector `{ref_name}`"
                        ),
                        false,
                    );
                    None
                } else {
                    locked_commit
                }
            }
            VersionMode::Frozen => {
                let Some(source) = locked else {
                    return self.frozen(format!(
                        "--frozen requires lock entry for ref-pinned source `{name}` ({url})"
                    ));
                };
                let Some(selector) = source.version.as_deref() else {
                    return self.frozen(format!(
                        "--frozen requires locked ref selector for ref-pinned source `{name}` ({url})"
                    ));
                };
                if selector != ref_name {
                    return self.frozen(format!(
                        "--frozen lock selector `{selector}` for ref-pinned source `{name}` ({url}) does not match requested selector `{ref_name}`"
                    ));
                }
                let Some(commit) = source.commit.as_deref() else {
                    return self.frozen(format!(
                        "--frozen requires locked commit for ref-pinned source `{name}` ({url})"
                    ));
                };
                Some(commit)
            }
        };

        // Ref-pinned resolutions are not semver selections, but the selector is kept as the tag
        // so lock replay can verify selector equality.
        let pinned = |resolved: Ref| Ref {
            version: None,
            tag: Some(ref_name.to_string()),
            ..resolved
        };
        if let Some(commit) = preferred {
            if self.fetch_commit(dep, url, commit)?.is_some() {
                return Ok(pinned(git_ref(url, commit)));
            }
            if mode == VersionMode::Frozen {
                return Err(self.unreachable::<()>(dep, url, commit).unwrap_err().into());
            }
            self.note(
                Level::Warning,
                "locked-commit-unreachable",
                format!(
                    "locked commit {commit} for {url} is unreachable; re-resolving ref `{ref_name}`"
                ),
                false,
            );
        }
        Ok(pinned(self.fetch_ref(dep, url, ref_name)?))
    }

    fn resolve_untagged(
        &mut self,
        dep: &Dependency,
        url: &str,
        locked_commit: Option<&str>,
        locked_unreachable: bool,
        policy: SelectionPolicy,
    ) -> Single<Ref> {
        let preferred = match policy {
            SelectionPolicy::LatestOnly => None,
            SelectionPolicy::PreferLockThenLatest if locked_unreachable => None,
            SelectionPolicy::PreferLockThenLatest => locked_commit,
        };
        if let Some(commit) = preferred {
            if self.fetch_commit(dep, url, commit)?.is_some() {
                return Ok(git_ref(url, commit));
            }
            self.note(
                Level::Warning,
                "locked-commit-unreachable",
                format!("locked commit {commit} for {url} is unreachable; re-resolving from HEAD"),
                false,
            );
        }
        Ok(self.fetch_ref(dep, url, "HEAD")?)
    }

    fn resolve_git_source(
        &mut self,
        pending: &Pending<'_>,
        url: &str,
        locked: Option<&LockedDependency>,
        mode: VersionMode,
    ) -> Single<Ref> {
        let dep = pending.dep;
        let name = &dep.name;
        let constraints = self.constraints_of(name);
        let requests: Vec<(&str, &Constraint)> = constraints
            .iter()
            .map(|(requester, constraint)| (requester.as_str(), constraint))
            .collect();
        let conflict = |detail: String| {
            SingleError::Conflict(Box::new(Failure::Conflict {
                name: name.clone(),
                requesters: constraints
                    .iter()
                    .map(|(by, constraint)| Requester {
                        by: by.clone(),
                        constraint: constraint.to_string(),
                    })
                    .collect(),
                detail,
            }))
        };
        if let Some(reference) = settle(&requests).map_err(|error| conflict(error.to_string()))? {
            return self.resolve_ref_pin(pending, url, reference, locked, mode);
        }

        let locked_commit = locked.and_then(|source| source.commit.as_deref());
        let locked_version_raw = locked.and_then(|source| source.version.as_deref());
        let locked_version = locked_version_raw.and_then(parse_tag);
        let excluded: HashSet<Version> = self
            .exclusions
            .keys()
            .filter(|(source, _)| source == pending.id)
            .map(|(_, version)| version.clone())
            .collect();

        let policy = match mode {
            VersionMode::Choose(policy) => policy,
            VersionMode::Frozen => {
                let has_semver = requests
                    .iter()
                    .any(|(_, constraint)| matches!(constraint, Constraint::Semver(_)));
                // A lock row with a selector that is no version was written under a ref request
                // the walk has not met yet (a package that asks for itself at a ref, later): the
                // commit is replayed, and that request checks the selector when it comes.
                if locked_version.is_none() && !has_semver {
                    let Some(commit) = locked_commit else {
                        return self.frozen(format!(
                            "--frozen requires locked commit for unpinned source `{name}` ({url})"
                        ));
                    };
                    return Ok(self.at_commit(dep, url, commit)?);
                }
                if locked.is_none() {
                    return self.frozen(format!(
                        "--frozen requires lock entry for source `{name}` ({url})"
                    ));
                }
                let Some(locked_version) = locked_version else {
                    return self.frozen(format!(
                        "--frozen requires parseable locked semver version for source `{name}` ({url}); found {locked_version_raw:?}"
                    ));
                };
                let Some(commit) = locked_commit else {
                    return self.frozen(format!(
                        "--frozen requires locked commit for semver source `{name}` ({url})"
                    ));
                };
                if !allows(&requests, &locked_version, &excluded) {
                    let why = if excluded.contains(&locked_version) {
                        "its engine requirements"
                    } else {
                        "current constraints"
                    };
                    return self.frozen(format!(
                        "--frozen lock version {locked_version} for `{name}` is incompatible with {why}"
                    ));
                }
                return match self.replay(dep, url, commit, &locked_version, locked_version_raw)? {
                    Some(resolved) => Ok(resolved),
                    None => Err(self.unreachable::<()>(dep, url, commit).unwrap_err().into()),
                };
            }
        };

        let mut locked_unreachable = false;
        if let (Some(locked_version), Some(commit)) = (locked_version.as_ref(), locked_commit)
            && keeps_lock(&requests, locked_version, policy, &excluded)
        {
            match self.replay(dep, url, commit, locked_version, locked_version_raw)? {
                Some(resolved) => return Ok(resolved),
                None => {
                    self.locked_unreachable(url, commit);
                    locked_unreachable = true;
                }
            }
        }

        let available = self.tags(url)?;
        let versions: Vec<Version> = available.iter().map(|tag| tag.version.clone()).collect();
        let selected = match select(
            &versions,
            &requests,
            locked_version.as_ref(),
            policy,
            &excluded,
        ) {
            Ok(index) => index.map(|index| &available[index]),
            Err(error @ SelectError::NoSatisfying { .. }) => {
                return Err(conflict(error.to_string()));
            }
        };
        let Some(selected) = selected else {
            return self.resolve_untagged(dep, url, locked_commit, locked_unreachable, policy);
        };
        // A `Latest` request does not keep the lock, but the newest may be the locked version: the
        // lock row stays as it is, so an existing lock keeps the id it holds (an older build
        // recorded a tag object's id, a new pin records the peeled commit).
        if let (Some(locked_version), Some(commit)) = (locked_version.as_ref(), locked_commit)
            && !locked_unreachable
            && *locked_version == selected.version
        {
            match self.replay(dep, url, commit, locked_version, locked_version_raw)? {
                Some(resolved) => return Ok(resolved),
                None => self.locked_unreachable(url, commit),
            }
        }
        let resolved = Ref {
            version: Some(selected.version.clone()),
            tag: Some(selected.tag.clone()),
            ..git_ref(url, &selected.commit)
        };
        let commit = selected.commit.clone();
        self.at_commit(dep, url, &commit)?;
        Ok(resolved)
    }
}

fn git_ref(url: &str, commit: &str) -> Ref {
    Ref {
        version: None,
        tag: None,
        commit: Some(commit.to_string()),
        origin: Origin::Git {
            url: url.to_string(),
            commit: commit.to_string(),
        },
    }
}

/// Whether the lock row describes this source: the same repository (or path), subpath and
/// plugin.
pub(super) fn locked_matches(locked: &LockedDependency, id: &SourceId) -> bool {
    match id {
        SourceId::Git {
            url,
            subpath,
            plugin,
        } => {
            locked.path.is_none()
                && locked.plugin.as_ref() == plugin.as_ref()
                && locked.url.as_ref().is_some_and(|locked_url| {
                    canonicalize_git_url(locked_url) == canonicalize_git_url(url)
                })
                && locked.subpath.as_ref() == subpath.as_ref()
        }
        SourceId::Path {
            canonical,
            subpath,
            plugin,
        } => {
            locked.url.is_none()
                && locked.plugin.as_ref() == plugin.as_ref()
                && locked.subpath.as_ref() == subpath.as_ref()
                && locked
                    .path
                    .as_deref()
                    .is_some_and(|locked_path| paths_equivalent(locked_path, canonical))
        }
    }
}

/// Paths compare as written, except that a Windows separator is a `/`.
fn paths_equivalent(a: &str, b: &str) -> bool {
    if cfg!(windows) {
        a.replace('\\', "/") == b.replace('\\', "/")
    } else {
        a == b
    }
}

fn identity_string(locked: &LockedDependency) -> String {
    let none = |value: &Option<crate::names::SourceSubpath>| {
        value
            .as_ref()
            .map_or("none".to_string(), |subpath| subpath.to_string())
    };
    let source = match (&locked.url, &locked.path, &locked.subpath) {
        (Some(url), None, Some(subpath)) => format!("git:{url}@{subpath}"),
        (Some(url), None, None) => format!("git:{url}"),
        (None, Some(path), Some(subpath)) => format!("path:{path}@{subpath}"),
        (None, Some(path), None) => format!("path:{path}"),
        (Some(url), Some(path), subpath) => {
            format!(
                "invalid-lock-entry(url={url}, path={path}, subpath={})",
                none(subpath)
            )
        }
        (None, None, subpath) => format!(
            "invalid-lock-entry(url=none, path=none, subpath={})",
            none(subpath)
        ),
    };
    match &locked.plugin {
        Some(plugin) => format!("{source} (plugin {plugin})"),
        None => source,
    }
}