amont-runtime 1.42.0

The amont hook logic: registry, dispatchers, checks and the trust model
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! Make a fresh snapshot runnable: carry the untracked files a suite needs,
//! then prepare its JavaScript dependencies.
//!
//! A snapshot is a checkout, not a workspace — no `node_modules`, no `.env` —
//! and a gate started there fails having tested nothing. Both steps run in
//! [`crate::pushed_tree::PushedTree::prepare`], before `amont.snapshotPrepare`
//! (the escape hatch), and a failure in either is the snapshot's failure: no
//! suite runs, nothing is stamped.
//!
//! ## What a stamp may rest on
//!
//! A stamp claims the gate passed on the COMMITTED content. So nothing here
//! may put content from the developer's tree where committed content lives:
//! a carried path must be untracked in the snapshot's own index, and the
//! dependencies must be the ones the committed lockfile describes.
//!
//! The default, `install`, is what CI does: `npm ci`, `pnpm install
//! --frozen-lockfile`. `reuse` (pnpm only; see `reuse` for why npm is not
//! reusable) clones the working tree's `node_modules` and keeps the clone
//! only when it has pnpm's isolated layout and a frozen offline install
//! accepts it against the snapshot's manifests and lockfile — which catches
//! a missing or wrong-version package, a stray directory, and a manifest the
//! lockfile does not satisfy, and does NOT catch a package whose files were
//! edited in place without a version change. That limit is why reuse is
//! opt-in.
//!
//! Every question about a manifest goes to the package manager, through exit
//! codes and parseable output: this crate is dependency-free and has no JSON
//! or YAML parser, and a hand-rolled one reading `workspaces` would be a
//! second, divergent answer to a question npm already answers.

use std::path::{Component, Path, PathBuf};
use std::process::{Command, Stdio};

use crate::ui::{highlight, warning_sign};

const CARRY: &str = "amont.snapshotCarry";
const DEPS: &str = "amont.snapshotDeps";

/// `amont.snapshotDeps`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DepsMode {
    /// `npm ci` / `pnpm install --frozen-lockfile` in the snapshot.
    Install,
    /// Clone the working tree's install, keep it only if the manager accepts it.
    Reuse,
    /// Prepare nothing.
    Off,
}

pub fn deps_mode(settings: &crate::config::Settings) -> DepsMode {
    match crate::config::enumerated_or(settings, DEPS, &["install", "reuse", "off"], "install") {
        "reuse" => DepsMode::Reuse,
        "off" => DepsMode::Off,
        _ => DepsMode::Install,
    }
}

/// `amont.snapshotCarry`, split on whitespace and commas.
pub fn carry_list(settings: &crate::config::Settings) -> Vec<String> {
    crate::config::string_value(settings, CARRY)
        .map(|v| {
            v.split(|c: char| c.is_whitespace() || c == ',')
                .filter(|s| !s.is_empty())
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

/// The snapshot's own tracked files, `-z`, with git's environment stripped:
/// a hook's `GIT_DIR`/`GIT_INDEX_FILE` would otherwise answer for the
/// developer's repository, and the pushed tip — not HEAD, not the index the
/// developer is staging into — decides what the snapshot holds.
pub fn snapshot_files(snapshot: &Path) -> Result<Vec<String>, String> {
    let mut cmd = Command::new("git");
    cmd.arg("-C")
        .arg(snapshot)
        .args(["ls-files", "-z"])
        .stdin(Stdio::null())
        .stderr(Stdio::null());
    crate::hooks::common::strip_git_env(&mut cmd);
    let out = cmd
        .output()
        .map_err(|e| format!("could not list the snapshot's files: {e}"))?;
    if !out.status.success() {
        return Err("could not list the snapshot's files".to_string());
    }
    Ok(crate::git::split_nul_paths(&out.stdout))
}

// ---------------------------------------------------------------------------
// carry
// ---------------------------------------------------------------------------

/// One validated carry entry.
struct Carried {
    entry: String,
    src: PathBuf,
    dst: PathBuf,
}

/// Why one carry entry is refused, or `None` when it is lexically a
/// repo-relative path with nothing to climb out through.
fn lexical_problem(entry: &str) -> Option<&'static str> {
    if entry.is_empty() {
        return Some("empty");
    }
    let bytes = entry.as_bytes();
    if entry.starts_with('/') || entry.starts_with('\\') || (bytes.len() >= 2 && bytes[1] == b':') {
        return Some("not a repo-relative path");
    }
    for part in entry.split(['/', '\\']) {
        if part.is_empty() {
            return Some("has an empty path component");
        }
        if part == "." || part == ".." {
            return Some("has a `.` or `..` component");
        }
        if part.eq_ignore_ascii_case(".git") {
            return Some("is inside .git");
        }
    }
    None
}

/// Is `entry` — or anything inside it, or any directory above it — tracked in
/// the snapshot? Carrying it would put the developer's copy over committed
/// content, and the stamp would vouch for a tree nobody committed.
fn tracked_collision(entry: &str, files: &[String]) -> Option<String> {
    let inside = format!("{entry}/");
    files
        .iter()
        .find(|f| {
            f.as_str() == entry || f.starts_with(&inside) || entry.starts_with(&format!("{f}/"))
        })
        .cloned()
}

fn is_symlink(p: &Path) -> bool {
    std::fs::symlink_metadata(p)
        .map(|m| m.file_type().is_symlink())
        .unwrap_or(false)
}

/// A symlinked directory between `root` and `rel`'s last component — which
/// would take a read, or a write, somewhere outside the tree it names.
fn symlinked_ancestor(root: &Path, rel: &Path) -> Option<PathBuf> {
    let mut at = root.to_path_buf();
    let parts: Vec<_> = rel.components().collect();
    for part in parts.iter().take(parts.len().saturating_sub(1)) {
        at.push(part.as_os_str());
        if is_symlink(&at) {
            return Some(at);
        }
    }
    None
}

/// A symlink anywhere inside `dir`, not followed.
fn symlink_inside(dir: &Path) -> std::io::Result<Option<PathBuf>> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let kind = entry.file_type()?;
        if kind.is_symlink() {
            return Ok(Some(entry.path()));
        }
        if kind.is_dir() {
            if let Some(found) = symlink_inside(&entry.path())? {
                return Ok(Some(found));
            }
        }
    }
    Ok(None)
}

fn copy_tree(src: &Path, dst: &Path) -> std::io::Result<()> {
    let meta = std::fs::symlink_metadata(src)?;
    if meta.is_dir() {
        std::fs::create_dir(dst)?;
        for entry in std::fs::read_dir(src)? {
            let entry = entry?;
            copy_tree(&entry.path(), &dst.join(entry.file_name()))?;
        }
        Ok(())
    } else {
        std::fs::copy(src, dst).map(|_| ())
    }
}

/// Copy the untracked files `amont.snapshotCarry` names from `source` into
/// `snapshot`.
///
/// Every entry is validated before anything is copied, and every refusal is
/// reported in ONE message — `config::complain` says a key once, which would
/// hide all but the first bad entry. Any refusal fails preparation: a carry
/// the author got wrong must be loud, never a silently thinner snapshot that
/// still earns a stamp. An entry that is simply absent is skipped (a `.env`
/// exists on a laptop and not in CI); one that is present and cannot be
/// copied fails.
pub fn carry(
    entries: &[String],
    source: &Path,
    snapshot: &Path,
    files: &[String],
) -> Result<(), String> {
    let mut refused: Vec<String> = Vec::new();
    let mut missing: Vec<&str> = Vec::new();
    let mut plan: Vec<Carried> = Vec::new();
    for entry in entries {
        let norm = entry.replace('\\', "/");
        let norm = norm.trim_end_matches('/').to_string();
        if let Some(why) = lexical_problem(&norm) {
            refused.push(format!("{entry}: {why}"));
            continue;
        }
        if let Some(f) = tracked_collision(&norm, files) {
            refused.push(format!(
                "{entry}: tracked content ({f}) — only untracked files can be carried"
            ));
            continue;
        }
        let rel = PathBuf::from(&norm);
        let src = source.join(&rel);
        let dst = snapshot.join(&rel);
        if let Some(link) = symlinked_ancestor(source, &rel) {
            refused.push(format!(
                "{entry}: {} is a symlink in the working tree",
                link.display()
            ));
            continue;
        }
        if let Some(link) = symlinked_ancestor(snapshot, &rel) {
            refused.push(format!(
                "{entry}: {} is a symlink in the snapshot",
                link.display()
            ));
            continue;
        }
        let meta = match std::fs::symlink_metadata(&src) {
            Ok(m) => m,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                missing.push(entry);
                continue;
            }
            Err(e) => {
                refused.push(format!("{entry}: cannot be read: {e}"));
                continue;
            }
        };
        if meta.file_type().is_symlink() {
            refused.push(format!("{entry}: is a symlink"));
            continue;
        }
        if meta.is_dir() {
            match symlink_inside(&src) {
                Ok(Some(link)) => {
                    refused.push(format!("{entry}: holds a symlink ({})", link.display()));
                    continue;
                }
                Ok(None) => {}
                Err(e) => {
                    refused.push(format!("{entry}: cannot be read: {e}"));
                    continue;
                }
            }
        }
        if std::fs::symlink_metadata(&dst).is_ok() {
            refused.push(format!("{entry}: already exists in the snapshot"));
            continue;
        }
        plan.push(Carried {
            entry: entry.clone(),
            src,
            dst,
        });
    }
    if !refused.is_empty() {
        return Err(format!(
            "{CARRY} refused {}: {}",
            if refused.len() == 1 {
                "an entry".to_string()
            } else {
                format!("{} entries", refused.len())
            },
            refused.join("; ")
        ));
    }
    if !missing.is_empty() {
        println!(
            "snapshot: not carrying {} — not in the working tree",
            missing.join(", ")
        );
    }
    for c in &plan {
        if let Some(parent) = c.dst.parent() {
            std::fs::create_dir_all(parent).map_err(|e| format!("{CARRY}: {}: {e}", c.entry))?;
        }
        copy_tree(&c.src, &c.dst)
            .map_err(|e| format!("{CARRY}: {} could not be copied: {e}", c.entry))?;
    }
    if !plan.is_empty() {
        let names: Vec<&str> = plan.iter().map(|c| c.entry.as_str()).collect();
        println!("snapshot: carried {}", names.join(", "));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// dependencies
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Manager {
    Npm,
    Pnpm,
}

impl Manager {
    fn lockfile(self) -> &'static str {
        match self {
            Manager::Npm => "package-lock.json",
            Manager::Pnpm => "pnpm-lock.yaml",
        }
    }
    fn tool(self) -> &'static str {
        match self {
            Manager::Npm => "npm",
            Manager::Pnpm => "pnpm",
        }
    }
}

/// One installation unit: a directory holding a tracked lockfile, and the
/// manager that reads it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unit {
    /// Repo-relative, `""` for the root.
    pub dir: String,
    pub manager: Manager,
    /// pnpm only: a lockfile with no `pnpm-workspace.yaml` beside it is a
    /// standalone project, even inside a workspace (see `audit::pnpm_args`).
    pub standalone: bool,
}

/// The directories of `files` named exactly `name`, repo-relative (`""` for
/// the root), sorted and deduplicated. Pure: the caller decides WHICH index
/// the list comes from — the snapshot's, for a snapshot.
pub fn lockfile_dirs_in(files: &[String], name: &str) -> Vec<String> {
    let mut dirs: Vec<String> = files
        .iter()
        .filter(|p| p.rsplit('/').next().unwrap_or(p) == name)
        .map(|p| {
            p.rsplit_once('/')
                .map(|(d, _)| d.to_string())
                .unwrap_or_default()
        })
        .collect();
    dirs.sort();
    dirs.dedup();
    dirs
}

fn join_rel(dir: &str, name: &str) -> String {
    if dir.is_empty() {
        name.to_string()
    } else {
        format!("{dir}/{name}")
    }
}

/// The `packageManager` field of a package.json, by a targeted scan of that
/// one key — `Some("npm")`/`Some("pnpm")` when it names one of them without
/// ambiguity, `None` otherwise.
pub fn declared_manager(package_json: &str) -> Option<Manager> {
    let key = "\"packageManager\"";
    let mut hits = package_json.match_indices(key);
    let (at, _) = hits.next()?;
    if hits.next().is_some() {
        return None;
    }
    let rest = package_json[at + key.len()..].trim_start();
    let rest = rest.strip_prefix(':')?.trim_start();
    let rest = rest.strip_prefix('"')?;
    let value = &rest[..rest.find('"')?];
    if value.starts_with("pnpm@") || value == "pnpm" {
        Some(Manager::Pnpm)
    } else if value.starts_with("npm@") || value == "npm" {
        Some(Manager::Npm)
    } else {
        None
    }
}

/// Every installation unit in `files`. A directory holding BOTH lockfiles is
/// settled by `packageManager`, or refused: guessing would install the wrong
/// tree and test it as if it were the right one.
pub fn units(files: &[String], snapshot: &Path) -> Result<Vec<Unit>, String> {
    let npm = lockfile_dirs_in(files, Manager::Npm.lockfile());
    let pnpm = lockfile_dirs_in(files, Manager::Pnpm.lockfile());
    let mut out = Vec::new();
    let mut dirs: Vec<&String> = npm.iter().chain(pnpm.iter()).collect();
    dirs.sort();
    dirs.dedup();
    for dir in dirs {
        let manager = match (npm.contains(dir), pnpm.contains(dir)) {
            (true, true) => {
                let manifest = snapshot.join(join_rel(dir, "package.json"));
                let body = std::fs::read_to_string(&manifest).unwrap_or_default();
                declared_manager(&body).ok_or_else(|| {
                    format!(
                        "both package-lock.json and pnpm-lock.yaml in {} and no \
                         `packageManager` to choose — set amont.snapshotPrepare",
                        if dir.is_empty() { "the root" } else { dir }
                    )
                })?
            }
            (true, false) => Manager::Npm,
            _ => Manager::Pnpm,
        };
        let standalone =
            manager == Manager::Pnpm && !files.contains(&join_rel(dir, "pnpm-workspace.yaml"));
        out.push(Unit {
            dir: dir.clone(),
            manager,
            standalone,
        });
    }
    Ok(out)
}

fn unit_label(u: &Unit) -> String {
    if u.dir.is_empty() {
        "the root".to_string()
    } else {
        format!("{}/", u.dir)
    }
}

fn manager_cmd(u: &Unit, at: &Path, args: &[&str]) -> Command {
    let mut cmd = Command::new(crate::hooks::common::program(u.manager.tool()));
    cmd.args(args).current_dir(at).stdin(Stdio::null());
    if u.manager == Manager::Pnpm {
        // A purge prompt cannot be answered from a hook, and aborts.
        cmd.arg("--config.confirmModulesPurge=false");
        if u.standalone {
            cmd.arg("--ignore-workspace");
        }
    }
    crate::hooks::common::strip_git_env(&mut cmd);
    cmd
}

/// Run a manager command quietly; `Ok(output)` on exit 0, `Err(output)`
/// otherwise — the output is what the caller quotes when it says why.
fn quiet(settings: &crate::config::Settings, mut cmd: Command) -> Result<String, String> {
    match crate::hooks::common::capture_within(settings, &mut cmd) {
        Some((crate::hooks::common::Ran::Status(s), text)) if s.success() => Ok(text),
        Some((_, text)) => Err(text),
        None => Err("could not start".to_string()),
    }
}

/// The last meaningful line of a tool's output, for a one-line reason.
fn last_line(text: &str) -> String {
    text.lines()
        .map(str::trim)
        .rfind(|l| !l.is_empty())
        .unwrap_or("no output")
        .chars()
        .take(200)
        .collect()
}

fn install(settings: &crate::config::Settings, u: &Unit, at: &Path) -> Result<(), String> {
    let args: &[&str] = match u.manager {
        Manager::Npm => &["ci", "--no-audit", "--no-fund", "--prefer-offline"],
        Manager::Pnpm => &["install", "--frozen-lockfile", "--prefer-offline"],
    };
    let mut cmd = manager_cmd(u, at, args);
    let what = format!("{} {}", u.manager.tool(), args[0]);
    if crate::hooks::common::bounded_success(settings, &mut cmd, &what) {
        Ok(())
    } else {
        Err(format!("{what} failed in {}", unit_label(u)))
    }
}

/// Clone a directory by spawning the platform's copy tool by ABSOLUTE path:
/// a GNU `cp` first on PATH has no `-c`. APFS clones with `-c`, btrfs/xfs
/// reflink with `--reflink=auto` (a plain copy elsewhere); symlinks are
/// copied as symlinks either way.
fn clone_dir(src: &Path, dst: &Path) -> Result<(), String> {
    #[cfg(target_os = "macos")]
    let attempts: &[&[&str]] = &[&["/bin/cp", "-cR"], &["/bin/cp", "-R"]];
    #[cfg(all(unix, not(target_os = "macos")))]
    let attempts: &[&[&str]] = &[
        &["/bin/cp", "-R", "--reflink=auto"],
        &["/usr/bin/cp", "-R", "--reflink=auto"],
    ];
    #[cfg(not(unix))]
    let attempts: &[&[&str]] = &[];
    for argv in attempts {
        if !Path::new(argv[0]).exists() {
            continue;
        }
        let ok = Command::new(argv[0])
            .args(&argv[1..])
            .arg(src)
            .arg(dst)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false);
        if ok {
            return Ok(());
        }
        let _ = std::fs::remove_dir_all(dst);
    }
    Err(format!("could not clone {}", src.display()))
}

/// Lexically resolve `p` — `.` and `..` folded, nothing touched on disk.
fn normalize(p: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for c in p.components() {
        match c {
            Component::ParentDir => {
                out.pop();
            }
            Component::CurDir => {}
            other => out.push(other.as_os_str()),
        }
    }
    out
}

/// The first symlink under `dir` that resolves into `source` — a clone
/// still reading the developer's tree through a link is not isolated,
/// whatever else it preserved.
fn link_into(dir: &Path, sources: &[PathBuf]) -> std::io::Result<Option<PathBuf>> {
    let mut stack = vec![dir.to_path_buf()];
    while let Some(at) = stack.pop() {
        for entry in std::fs::read_dir(&at)? {
            let entry = entry?;
            let kind = entry.file_type()?;
            let path = entry.path();
            if kind.is_symlink() {
                let target = std::fs::read_link(&path)?;
                let resolved = if target.is_absolute() {
                    normalize(&target)
                } else {
                    normalize(&at.join(target))
                };
                // Lexically AND through every link: `/var` is itself a
                // symlink on macOS, so the two spellings of one directory
                // must both be caught.
                let real = path.canonicalize().ok();
                if sources.iter().any(|s| {
                    resolved.starts_with(s) || real.as_ref().is_some_and(|r| r.starts_with(s))
                }) {
                    return Ok(Some(path));
                }
            } else if kind.is_dir() {
                stack.push(path);
            }
        }
    }
    Ok(None)
}

/// Workspace members of a pnpm unit, as pnpm lists them, relative to the
/// unit dir. Asked in the SNAPSHOT: the committed manifests decide.
fn members(settings: &crate::config::Settings, u: &Unit, at: &Path) -> Result<Vec<String>, String> {
    if u.standalone {
        return Ok(Vec::new());
    }
    let text = quiet(
        settings,
        manager_cmd(u, at, &["ls", "-r", "--depth", "-1", "--parseable"]),
    )
    .map_err(|t| format!("pnpm could not list the workspace: {}", last_line(&t)))?;
    let root = normalize(at);
    let canon = at.canonicalize().unwrap_or_else(|_| root.clone());
    Ok(text
        .lines()
        .filter_map(|l| {
            let p = PathBuf::from(l.trim());
            let rel = p
                .strip_prefix(&root)
                .or_else(|_| p.strip_prefix(&canon))
                .ok()?;
            let rel = rel.to_string_lossy().replace('\\', "/");
            (!rel.is_empty()).then_some(rel)
        })
        .collect())
}

/// Is `nm` laid out the way pnpm's default (`isolated`) linker lays it out —
/// every package a link, `@scope` directories holding only links? pnpm's own
/// check does not look at a directory it did not create, and a stray one
/// here is importable by the suite; so anything else is not reused.
fn pnpm_layout_problem(nm: &Path, is_root: bool) -> Option<String> {
    if is_root {
        let modules = std::fs::read_to_string(nm.join(".modules.yaml")).unwrap_or_default();
        let isolated = modules
            .lines()
            .any(|l| l.contains("nodeLinker") && l.contains("isolated"));
        if !isolated {
            return Some("not installed with pnpm's isolated linker".to_string());
        }
    }
    let entries = std::fs::read_dir(nm).ok()?;
    for e in entries.flatten() {
        let name = e.file_name().to_string_lossy().into_owned();
        if name.starts_with('.') {
            continue;
        }
        let kind = e.file_type().ok()?;
        if kind.is_symlink() {
            continue;
        }
        if name.starts_with('@') && kind.is_dir() {
            for inner in std::fs::read_dir(e.path()).ok()?.flatten() {
                if !inner.file_type().map(|k| k.is_symlink()).unwrap_or(false) {
                    return Some(format!(
                        "{} is not a link pnpm made",
                        inner.path().display()
                    ));
                }
            }
            continue;
        }
        return Some(format!("{} is not a link pnpm made", e.path().display()));
    }
    None
}

/// Try to reuse the working tree's install for one unit. `Ok(())` when the
/// clone is in place and pnpm accepted it; `Err(reason)` — with the snapshot
/// left exactly as it was — when it cannot be.
///
/// npm is never reused. `npm ls` answers "is this dependency graph valid",
/// not "is this the tree the lockfile describes": a real repository with
/// peer-range conflicts `npm ci` installs without complaint fails `npm ls`
/// on a fresh install (application-landscape: 182 findings), so it can
/// neither accept a correct clone nor be trusted to reject a wrong one.
fn reuse(
    settings: &crate::config::Settings,
    u: &Unit,
    source: &Path,
    snapshot: &Path,
    unit_dirs: &[String],
) -> Result<(), String> {
    if u.manager == Manager::Npm {
        return Err(
            "npm cannot check an installed tree against its lockfile, so npm \
                    snapshots always install"
                .to_string(),
        );
    }
    let src_dir = source.join(&u.dir);
    let snap_dir = snapshot.join(&u.dir);
    let lock = u.manager.lockfile();
    let src_lock = std::fs::read(src_dir.join(lock))
        .map_err(|_| "no lockfile in the working tree".to_string())?;
    let snap_lock = std::fs::read(snap_dir.join(lock)).map_err(|e| format!("{lock}: {e}"))?;
    if src_lock != snap_lock {
        return Err("the working tree's lockfile differs from the commit's".to_string());
    }
    if !src_dir.join("node_modules").is_dir() {
        return Err("nothing installed in the working tree".to_string());
    }
    let mut set = vec![String::new()];
    for m in members(settings, u, &snap_dir)? {
        let repo_rel = join_rel(&u.dir, &m);
        // A member with a lockfile of its own is its own unit.
        if unit_dirs.contains(&repo_rel) {
            continue;
        }
        if src_dir.join(&m).join("node_modules").is_dir() {
            set.push(m);
        }
    }
    let mut cloned: Vec<PathBuf> = Vec::new();
    let rollback = |cloned: &[PathBuf]| {
        for d in cloned {
            let _ = std::fs::remove_dir_all(d);
        }
    };
    let sources = [
        normalize(source),
        source.canonicalize().unwrap_or_else(|_| normalize(source)),
    ];
    for m in &set {
        let from = src_dir.join(m).join("node_modules");
        let to = snap_dir.join(m).join("node_modules");
        if std::fs::symlink_metadata(&to).is_ok() {
            rollback(&cloned);
            return Err(format!("{} already exists in the snapshot", to.display()));
        }
        if let Err(e) = clone_dir(&from, &to) {
            rollback(&cloned);
            return Err(e);
        }
        cloned.push(to.clone());
        let problem = match link_into(&to, &sources) {
            Ok(Some(link)) => Some(format!(
                "{} links back into the working tree",
                link.display()
            )),
            Ok(None) => pnpm_layout_problem(&to, m.is_empty()),
            Err(e) => Some(format!("could not inspect the clone: {e}")),
        };
        if let Some(why) = problem {
            rollback(&cloned);
            return Err(why);
        }
    }
    // pnpm records the absolute paths of the tree it installed; the clone
    // must not carry the source's.
    if let Ok(rd) = std::fs::read_dir(snap_dir.join("node_modules")) {
        for e in rd.flatten() {
            if e.file_name()
                .to_string_lossy()
                .starts_with(".pnpm-workspace-state")
            {
                let _ = std::fs::remove_file(e.path());
            }
        }
    }
    // A frozen OFFLINE install is both the check and the repair: it fails on
    // a manifest the lockfile does not satisfy, relinks drift from the
    // store, and says "Already up to date" for a faithful clone.
    if let Err(text) = quiet(
        settings,
        manager_cmd(u, &snap_dir, &["install", "--frozen-lockfile", "--offline"]),
    ) {
        rollback(&cloned);
        return Err(format!(
            "pnpm did not accept the clone ({})",
            last_line(&text)
        ));
    }
    Ok(())
}

/// Prepare every unit's dependencies in `snapshot`.
pub fn deps(
    settings: &crate::config::Settings,
    mode: DepsMode,
    source: &Path,
    snapshot: &Path,
    files: &[String],
) -> Result<(), String> {
    if mode == DepsMode::Off {
        return Ok(());
    }
    let units = units(files, snapshot)?;
    let unit_dirs: Vec<String> = units.iter().map(|u| u.dir.clone()).collect();
    for u in &units {
        let at = snapshot.join(&u.dir);
        let label = unit_label(u);
        if mode == DepsMode::Reuse {
            // No clone tool on Windows: `clone_dir` says so and this falls
            // through to the install, like any other refusal.
            match reuse(settings, u, source, snapshot, &unit_dirs) {
                Ok(()) => {
                    println!(
                        "snapshot: node_modules in {label} reused from the working tree — {} \
                         accepted it against the lockfile",
                        u.manager.tool()
                    );
                    continue;
                }
                Err(why) => println!(
                    "snapshot: {} in {label} — not reusing the working tree's install: {why}",
                    highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
                ),
            }
        } else {
            println!(
                "snapshot: {} in {label}",
                highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
            );
        }
        install(settings, u, &at)?;
    }
    Ok(())
}

fn install_verb(m: Manager) -> &'static str {
    match m {
        Manager::Npm => "ci",
        Manager::Pnpm => "install --frozen-lockfile",
    }
}

/// Carry → dependencies → nothing else; `snapshotPrepare` runs after this,
/// in `PushedTree::prepare`. Dependencies are skipped when a
/// `snapshotPrepare` command exists: that command owns them.
pub fn run(
    settings: &crate::config::Settings,
    source: &Path,
    snapshot: &Path,
    command_owns_deps: bool,
) -> Result<(), String> {
    let entries = carry_list(settings);
    let mode = if command_owns_deps {
        DepsMode::Off
    } else {
        deps_mode(settings)
    };
    if entries.is_empty() && mode == DepsMode::Off {
        return Ok(());
    }
    let files = snapshot_files(snapshot)?;
    if !entries.is_empty() {
        carry(&entries, source, snapshot, &files)?;
    }
    deps(settings, mode, source, snapshot, &files).map_err(|e| {
        println!("{} {e}", warning_sign());
        e
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn s(v: &[&str]) -> Vec<String> {
        v.iter().map(|x| x.to_string()).collect()
    }

    #[test]
    fn lexical_refusals() {
        for bad in [
            "",
            "/etc/hosts",
            "\\x",
            "C:/x",
            "a//b",
            ".",
            "..",
            "a/../b",
            ".git/config",
            "x/.GIT/y",
        ] {
            assert!(lexical_problem(bad).is_some(), "{bad:?} must be refused");
        }
        for good in [".env", "config/local.json", ".npmrc", "a.b/c"] {
            assert!(lexical_problem(good).is_none(), "{good:?} must pass");
        }
    }

    #[test]
    fn a_tracked_path_inside_above_or_equal_collides() {
        let files = s(&["src/a.ts", "vendor", "README.md"]);
        assert!(tracked_collision("src", &files).is_some());
        assert!(tracked_collision("src/a.ts", &files).is_some());
        assert!(tracked_collision("vendor/x", &files).is_some());
        assert!(tracked_collision(".env", &files).is_none());
        assert!(tracked_collision("sr", &files).is_none());
    }

    #[test]
    fn lockfile_dirs_come_from_the_list_given() {
        let files = s(&[
            "package-lock.json",
            "web/package-lock.json",
            "web/x/pnpm-lock.yaml",
            "notpackage-lock.json",
        ]);
        assert_eq!(
            lockfile_dirs_in(&files, "package-lock.json"),
            s(&["", "web"])
        );
        assert_eq!(lockfile_dirs_in(&files, "pnpm-lock.yaml"), s(&["web/x"]));
    }

    #[test]
    fn package_manager_field_is_read_narrowly() {
        assert_eq!(
            declared_manager(r#"{"packageManager": "pnpm@10.1.0"}"#),
            Some(Manager::Pnpm)
        );
        assert_eq!(
            declared_manager(r#"{"packageManager":"npm@11"}"#),
            Some(Manager::Npm)
        );
        assert_eq!(declared_manager(r#"{"packageManager":"yarn@4"}"#), None);
        assert_eq!(declared_manager(r#"{"name":"x"}"#), None);
        assert_eq!(
            declared_manager(r#"{"packageManager":"npm@1","x":{"packageManager":"pnpm@1"}}"#),
            None,
            "two answers is no answer"
        );
    }

    #[test]
    fn normalize_folds_dots_lexically() {
        assert_eq!(
            normalize(Path::new("/a/b/../c/./d")),
            PathBuf::from("/a/c/d")
        );
    }
}