Skip to main content

amont_runtime/
snapshot_prep.rs

1//! Make a fresh snapshot runnable: carry the untracked files a suite needs,
2//! then prepare its JavaScript dependencies.
3//!
4//! A snapshot is a checkout, not a workspace — no `node_modules`, no `.env` —
5//! and a gate started there fails having tested nothing. Both steps run in
6//! [`crate::pushed_tree::PushedTree::prepare`], before `amont.snapshotPrepare`
7//! (the escape hatch), and a failure in either is the snapshot's failure: no
8//! suite runs, nothing is stamped.
9//!
10//! ## What a stamp may rest on
11//!
12//! A stamp claims the gate passed on the COMMITTED content. So nothing here
13//! may put content from the developer's tree where committed content lives:
14//! a carried path must be untracked in the snapshot's own index, and the
15//! dependencies must be the ones the committed lockfile describes.
16//!
17//! The default, `install`, is what CI does: `npm ci`, `pnpm install
18//! --frozen-lockfile`. `reuse` (pnpm only; see `reuse` for why npm is not
19//! reusable) clones the working tree's `node_modules` and keeps the clone
20//! only when it has pnpm's isolated layout and a frozen offline install
21//! accepts it against the snapshot's manifests and lockfile — which catches
22//! a missing or wrong-version package, a stray directory, and a manifest the
23//! lockfile does not satisfy, and does NOT catch a package whose files were
24//! edited in place without a version change. That limit is why reuse is
25//! opt-in.
26//!
27//! Every question about a manifest goes to the package manager, through exit
28//! codes and parseable output: this crate is dependency-free and has no JSON
29//! or YAML parser, and a hand-rolled one reading `workspaces` would be a
30//! second, divergent answer to a question npm already answers.
31
32use std::path::{Component, Path, PathBuf};
33use std::process::{Command, Stdio};
34
35use crate::ui::{highlight, warning_sign};
36
37const CARRY: &str = "amont.snapshotCarry";
38const DEPS: &str = "amont.snapshotDeps";
39
40/// `amont.snapshotDeps`.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum DepsMode {
43    /// `npm ci` / `pnpm install --frozen-lockfile` in the snapshot.
44    Install,
45    /// Clone the working tree's install, keep it only if the manager accepts it.
46    Reuse,
47    /// Prepare nothing.
48    Off,
49}
50
51pub fn deps_mode(settings: &crate::config::Settings) -> DepsMode {
52    match crate::config::enumerated_or(settings, DEPS, &["install", "reuse", "off"], "install") {
53        "reuse" => DepsMode::Reuse,
54        "off" => DepsMode::Off,
55        _ => DepsMode::Install,
56    }
57}
58
59/// `amont.snapshotCarry`, split on whitespace and commas.
60pub fn carry_list(settings: &crate::config::Settings) -> Vec<String> {
61    crate::config::string_value(settings, CARRY)
62        .map(|v| {
63            v.split(|c: char| c.is_whitespace() || c == ',')
64                .filter(|s| !s.is_empty())
65                .map(str::to_string)
66                .collect()
67        })
68        .unwrap_or_default()
69}
70
71/// The snapshot's own tracked files, `-z`, with git's environment stripped:
72/// a hook's `GIT_DIR`/`GIT_INDEX_FILE` would otherwise answer for the
73/// developer's repository, and the pushed tip — not HEAD, not the index the
74/// developer is staging into — decides what the snapshot holds.
75pub fn snapshot_files(snapshot: &Path) -> Result<Vec<String>, String> {
76    let mut cmd = Command::new("git");
77    cmd.arg("-C")
78        .arg(snapshot)
79        .args(["ls-files", "-z"])
80        .stdin(Stdio::null())
81        .stderr(Stdio::null());
82    crate::hooks::common::strip_git_env(&mut cmd);
83    let out = cmd
84        .output()
85        .map_err(|e| format!("could not list the snapshot's files: {e}"))?;
86    if !out.status.success() {
87        return Err("could not list the snapshot's files".to_string());
88    }
89    Ok(crate::git::split_nul_paths(&out.stdout))
90}
91
92// ---------------------------------------------------------------------------
93// carry
94// ---------------------------------------------------------------------------
95
96/// One validated carry entry.
97struct Carried {
98    entry: String,
99    src: PathBuf,
100    dst: PathBuf,
101}
102
103/// Why one carry entry is refused, or `None` when it is lexically a
104/// repo-relative path with nothing to climb out through.
105fn lexical_problem(entry: &str) -> Option<&'static str> {
106    if entry.is_empty() {
107        return Some("empty");
108    }
109    let bytes = entry.as_bytes();
110    if entry.starts_with('/') || entry.starts_with('\\') || (bytes.len() >= 2 && bytes[1] == b':') {
111        return Some("not a repo-relative path");
112    }
113    for part in entry.split(['/', '\\']) {
114        if part.is_empty() {
115            return Some("has an empty path component");
116        }
117        if part == "." || part == ".." {
118            return Some("has a `.` or `..` component");
119        }
120        if part.eq_ignore_ascii_case(".git") {
121            return Some("is inside .git");
122        }
123    }
124    None
125}
126
127/// Is `entry` — or anything inside it, or any directory above it — tracked in
128/// the snapshot? Carrying it would put the developer's copy over committed
129/// content, and the stamp would vouch for a tree nobody committed.
130fn tracked_collision(entry: &str, files: &[String]) -> Option<String> {
131    let inside = format!("{entry}/");
132    files
133        .iter()
134        .find(|f| {
135            f.as_str() == entry || f.starts_with(&inside) || entry.starts_with(&format!("{f}/"))
136        })
137        .cloned()
138}
139
140fn is_symlink(p: &Path) -> bool {
141    std::fs::symlink_metadata(p)
142        .map(|m| m.file_type().is_symlink())
143        .unwrap_or(false)
144}
145
146/// A symlinked directory between `root` and `rel`'s last component — which
147/// would take a read, or a write, somewhere outside the tree it names.
148fn symlinked_ancestor(root: &Path, rel: &Path) -> Option<PathBuf> {
149    let mut at = root.to_path_buf();
150    let parts: Vec<_> = rel.components().collect();
151    for part in parts.iter().take(parts.len().saturating_sub(1)) {
152        at.push(part.as_os_str());
153        if is_symlink(&at) {
154            return Some(at);
155        }
156    }
157    None
158}
159
160/// A symlink anywhere inside `dir`, not followed.
161fn symlink_inside(dir: &Path) -> std::io::Result<Option<PathBuf>> {
162    for entry in std::fs::read_dir(dir)? {
163        let entry = entry?;
164        let kind = entry.file_type()?;
165        if kind.is_symlink() {
166            return Ok(Some(entry.path()));
167        }
168        if kind.is_dir() {
169            if let Some(found) = symlink_inside(&entry.path())? {
170                return Ok(Some(found));
171            }
172        }
173    }
174    Ok(None)
175}
176
177fn copy_tree(src: &Path, dst: &Path) -> std::io::Result<()> {
178    let meta = std::fs::symlink_metadata(src)?;
179    if meta.is_dir() {
180        std::fs::create_dir(dst)?;
181        for entry in std::fs::read_dir(src)? {
182            let entry = entry?;
183            copy_tree(&entry.path(), &dst.join(entry.file_name()))?;
184        }
185        Ok(())
186    } else {
187        std::fs::copy(src, dst).map(|_| ())
188    }
189}
190
191/// Copy the untracked files `amont.snapshotCarry` names from `source` into
192/// `snapshot`.
193///
194/// Every entry is validated before anything is copied, and every refusal is
195/// reported in ONE message — `config::complain` says a key once, which would
196/// hide all but the first bad entry. Any refusal fails preparation: a carry
197/// the author got wrong must be loud, never a silently thinner snapshot that
198/// still earns a stamp. An entry that is simply absent is skipped (a `.env`
199/// exists on a laptop and not in CI); one that is present and cannot be
200/// copied fails.
201pub fn carry(
202    entries: &[String],
203    source: &Path,
204    snapshot: &Path,
205    files: &[String],
206) -> Result<(), String> {
207    let mut refused: Vec<String> = Vec::new();
208    let mut missing: Vec<&str> = Vec::new();
209    let mut plan: Vec<Carried> = Vec::new();
210    for entry in entries {
211        let norm = entry.replace('\\', "/");
212        let norm = norm.trim_end_matches('/').to_string();
213        if let Some(why) = lexical_problem(&norm) {
214            refused.push(format!("{entry}: {why}"));
215            continue;
216        }
217        if let Some(f) = tracked_collision(&norm, files) {
218            refused.push(format!(
219                "{entry}: tracked content ({f}) — only untracked files can be carried"
220            ));
221            continue;
222        }
223        let rel = PathBuf::from(&norm);
224        let src = source.join(&rel);
225        let dst = snapshot.join(&rel);
226        if let Some(link) = symlinked_ancestor(source, &rel) {
227            refused.push(format!(
228                "{entry}: {} is a symlink in the working tree",
229                link.display()
230            ));
231            continue;
232        }
233        if let Some(link) = symlinked_ancestor(snapshot, &rel) {
234            refused.push(format!(
235                "{entry}: {} is a symlink in the snapshot",
236                link.display()
237            ));
238            continue;
239        }
240        let meta = match std::fs::symlink_metadata(&src) {
241            Ok(m) => m,
242            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
243                missing.push(entry);
244                continue;
245            }
246            Err(e) => {
247                refused.push(format!("{entry}: cannot be read: {e}"));
248                continue;
249            }
250        };
251        if meta.file_type().is_symlink() {
252            refused.push(format!("{entry}: is a symlink"));
253            continue;
254        }
255        if meta.is_dir() {
256            match symlink_inside(&src) {
257                Ok(Some(link)) => {
258                    refused.push(format!("{entry}: holds a symlink ({})", link.display()));
259                    continue;
260                }
261                Ok(None) => {}
262                Err(e) => {
263                    refused.push(format!("{entry}: cannot be read: {e}"));
264                    continue;
265                }
266            }
267        }
268        if std::fs::symlink_metadata(&dst).is_ok() {
269            refused.push(format!("{entry}: already exists in the snapshot"));
270            continue;
271        }
272        plan.push(Carried {
273            entry: entry.clone(),
274            src,
275            dst,
276        });
277    }
278    if !refused.is_empty() {
279        return Err(format!(
280            "{CARRY} refused {}: {}",
281            if refused.len() == 1 {
282                "an entry".to_string()
283            } else {
284                format!("{} entries", refused.len())
285            },
286            refused.join("; ")
287        ));
288    }
289    if !missing.is_empty() {
290        println!(
291            "snapshot: not carrying {} — not in the working tree",
292            missing.join(", ")
293        );
294    }
295    for c in &plan {
296        if let Some(parent) = c.dst.parent() {
297            std::fs::create_dir_all(parent).map_err(|e| format!("{CARRY}: {}: {e}", c.entry))?;
298        }
299        copy_tree(&c.src, &c.dst)
300            .map_err(|e| format!("{CARRY}: {} could not be copied: {e}", c.entry))?;
301    }
302    if !plan.is_empty() {
303        let names: Vec<&str> = plan.iter().map(|c| c.entry.as_str()).collect();
304        println!("snapshot: carried {}", names.join(", "));
305    }
306    Ok(())
307}
308
309// ---------------------------------------------------------------------------
310// dependencies
311// ---------------------------------------------------------------------------
312
313#[derive(Debug, Clone, Copy, PartialEq, Eq)]
314pub enum Manager {
315    Npm,
316    Pnpm,
317}
318
319impl Manager {
320    fn lockfile(self) -> &'static str {
321        match self {
322            Manager::Npm => "package-lock.json",
323            Manager::Pnpm => "pnpm-lock.yaml",
324        }
325    }
326    fn tool(self) -> &'static str {
327        match self {
328            Manager::Npm => "npm",
329            Manager::Pnpm => "pnpm",
330        }
331    }
332}
333
334/// One installation unit: a directory holding a tracked lockfile, and the
335/// manager that reads it.
336#[derive(Debug, Clone, PartialEq, Eq)]
337pub struct Unit {
338    /// Repo-relative, `""` for the root.
339    pub dir: String,
340    pub manager: Manager,
341    /// pnpm only: a lockfile with no `pnpm-workspace.yaml` beside it is a
342    /// standalone project, even inside a workspace (see `audit::pnpm_args`).
343    pub standalone: bool,
344}
345
346/// The directories of `files` named exactly `name`, repo-relative (`""` for
347/// the root), sorted and deduplicated. Pure: the caller decides WHICH index
348/// the list comes from — the snapshot's, for a snapshot.
349pub fn lockfile_dirs_in(files: &[String], name: &str) -> Vec<String> {
350    let mut dirs: Vec<String> = files
351        .iter()
352        .filter(|p| p.rsplit('/').next().unwrap_or(p) == name)
353        .map(|p| {
354            p.rsplit_once('/')
355                .map(|(d, _)| d.to_string())
356                .unwrap_or_default()
357        })
358        .collect();
359    dirs.sort();
360    dirs.dedup();
361    dirs
362}
363
364fn join_rel(dir: &str, name: &str) -> String {
365    if dir.is_empty() {
366        name.to_string()
367    } else {
368        format!("{dir}/{name}")
369    }
370}
371
372/// The `packageManager` field of a package.json, by a targeted scan of that
373/// one key — `Some("npm")`/`Some("pnpm")` when it names one of them without
374/// ambiguity, `None` otherwise.
375pub fn declared_manager(package_json: &str) -> Option<Manager> {
376    let key = "\"packageManager\"";
377    let mut hits = package_json.match_indices(key);
378    let (at, _) = hits.next()?;
379    if hits.next().is_some() {
380        return None;
381    }
382    let rest = package_json[at + key.len()..].trim_start();
383    let rest = rest.strip_prefix(':')?.trim_start();
384    let rest = rest.strip_prefix('"')?;
385    let value = &rest[..rest.find('"')?];
386    if value.starts_with("pnpm@") || value == "pnpm" {
387        Some(Manager::Pnpm)
388    } else if value.starts_with("npm@") || value == "npm" {
389        Some(Manager::Npm)
390    } else {
391        None
392    }
393}
394
395/// Every installation unit in `files`. A directory holding BOTH lockfiles is
396/// settled by `packageManager`, or refused: guessing would install the wrong
397/// tree and test it as if it were the right one.
398pub fn units(files: &[String], snapshot: &Path) -> Result<Vec<Unit>, String> {
399    let npm = lockfile_dirs_in(files, Manager::Npm.lockfile());
400    let pnpm = lockfile_dirs_in(files, Manager::Pnpm.lockfile());
401    let mut out = Vec::new();
402    let mut dirs: Vec<&String> = npm.iter().chain(pnpm.iter()).collect();
403    dirs.sort();
404    dirs.dedup();
405    for dir in dirs {
406        let manager = match (npm.contains(dir), pnpm.contains(dir)) {
407            (true, true) => {
408                let manifest = snapshot.join(join_rel(dir, "package.json"));
409                let body = std::fs::read_to_string(&manifest).unwrap_or_default();
410                declared_manager(&body).ok_or_else(|| {
411                    format!(
412                        "both package-lock.json and pnpm-lock.yaml in {} and no \
413                         `packageManager` to choose — set amont.snapshotPrepare",
414                        if dir.is_empty() { "the root" } else { dir }
415                    )
416                })?
417            }
418            (true, false) => Manager::Npm,
419            _ => Manager::Pnpm,
420        };
421        let standalone =
422            manager == Manager::Pnpm && !files.contains(&join_rel(dir, "pnpm-workspace.yaml"));
423        out.push(Unit {
424            dir: dir.clone(),
425            manager,
426            standalone,
427        });
428    }
429    Ok(out)
430}
431
432fn unit_label(u: &Unit) -> String {
433    if u.dir.is_empty() {
434        "the root".to_string()
435    } else {
436        format!("{}/", u.dir)
437    }
438}
439
440fn manager_cmd(u: &Unit, at: &Path, args: &[&str]) -> Command {
441    let mut cmd = Command::new(crate::hooks::common::program(u.manager.tool()));
442    cmd.args(args).current_dir(at).stdin(Stdio::null());
443    if u.manager == Manager::Pnpm {
444        // A purge prompt cannot be answered from a hook, and aborts.
445        cmd.arg("--config.confirmModulesPurge=false");
446        if u.standalone {
447            cmd.arg("--ignore-workspace");
448        }
449    }
450    crate::hooks::common::strip_git_env(&mut cmd);
451    cmd
452}
453
454/// Run a manager command quietly; `Ok(output)` on exit 0, `Err(output)`
455/// otherwise — the output is what the caller quotes when it says why.
456fn quiet(settings: &crate::config::Settings, mut cmd: Command) -> Result<String, String> {
457    match crate::hooks::common::capture_within(settings, &mut cmd) {
458        Some((crate::hooks::common::Ran::Status(s), text)) if s.success() => Ok(text),
459        Some((_, text)) => Err(text),
460        None => Err("could not start".to_string()),
461    }
462}
463
464/// The last meaningful line of a tool's output, for a one-line reason.
465fn last_line(text: &str) -> String {
466    text.lines()
467        .map(str::trim)
468        .rfind(|l| !l.is_empty())
469        .unwrap_or("no output")
470        .chars()
471        .take(200)
472        .collect()
473}
474
475fn install(settings: &crate::config::Settings, u: &Unit, at: &Path) -> Result<(), String> {
476    let args: &[&str] = match u.manager {
477        Manager::Npm => &["ci", "--no-audit", "--no-fund", "--prefer-offline"],
478        Manager::Pnpm => &["install", "--frozen-lockfile", "--prefer-offline"],
479    };
480    let mut cmd = manager_cmd(u, at, args);
481    let what = format!("{} {}", u.manager.tool(), args[0]);
482    if crate::hooks::common::bounded_success(settings, &mut cmd, &what) {
483        Ok(())
484    } else {
485        Err(format!("{what} failed in {}", unit_label(u)))
486    }
487}
488
489/// Clone a directory by spawning the platform's copy tool by ABSOLUTE path:
490/// a GNU `cp` first on PATH has no `-c`. APFS clones with `-c`, btrfs/xfs
491/// reflink with `--reflink=auto` (a plain copy elsewhere); symlinks are
492/// copied as symlinks either way.
493fn clone_dir(src: &Path, dst: &Path) -> Result<(), String> {
494    #[cfg(target_os = "macos")]
495    let attempts: &[&[&str]] = &[&["/bin/cp", "-cR"], &["/bin/cp", "-R"]];
496    #[cfg(all(unix, not(target_os = "macos")))]
497    let attempts: &[&[&str]] = &[
498        &["/bin/cp", "-R", "--reflink=auto"],
499        &["/usr/bin/cp", "-R", "--reflink=auto"],
500    ];
501    #[cfg(not(unix))]
502    let attempts: &[&[&str]] = &[];
503    for argv in attempts {
504        if !Path::new(argv[0]).exists() {
505            continue;
506        }
507        let ok = Command::new(argv[0])
508            .args(&argv[1..])
509            .arg(src)
510            .arg(dst)
511            .stdin(Stdio::null())
512            .stdout(Stdio::null())
513            .stderr(Stdio::null())
514            .status()
515            .map(|s| s.success())
516            .unwrap_or(false);
517        if ok {
518            return Ok(());
519        }
520        let _ = std::fs::remove_dir_all(dst);
521    }
522    Err(format!("could not clone {}", src.display()))
523}
524
525/// Lexically resolve `p` — `.` and `..` folded, nothing touched on disk.
526fn normalize(p: &Path) -> PathBuf {
527    let mut out = PathBuf::new();
528    for c in p.components() {
529        match c {
530            Component::ParentDir => {
531                out.pop();
532            }
533            Component::CurDir => {}
534            other => out.push(other.as_os_str()),
535        }
536    }
537    out
538}
539
540/// The first symlink under `dir` that resolves into `source` — a clone
541/// still reading the developer's tree through a link is not isolated,
542/// whatever else it preserved.
543fn link_into(dir: &Path, sources: &[PathBuf]) -> std::io::Result<Option<PathBuf>> {
544    let mut stack = vec![dir.to_path_buf()];
545    while let Some(at) = stack.pop() {
546        for entry in std::fs::read_dir(&at)? {
547            let entry = entry?;
548            let kind = entry.file_type()?;
549            let path = entry.path();
550            if kind.is_symlink() {
551                let target = std::fs::read_link(&path)?;
552                let resolved = if target.is_absolute() {
553                    normalize(&target)
554                } else {
555                    normalize(&at.join(target))
556                };
557                // Lexically AND through every link: `/var` is itself a
558                // symlink on macOS, so the two spellings of one directory
559                // must both be caught.
560                let real = path.canonicalize().ok();
561                if sources.iter().any(|s| {
562                    resolved.starts_with(s) || real.as_ref().is_some_and(|r| r.starts_with(s))
563                }) {
564                    return Ok(Some(path));
565                }
566            } else if kind.is_dir() {
567                stack.push(path);
568            }
569        }
570    }
571    Ok(None)
572}
573
574/// The first regular file under `nm` modified after `record`, pnpm's
575/// install record (`.modules.yaml`, rewritten as an install finishes).
576///
577/// Skipped, because an install or the tools legitimately write them after
578/// the record: every `.bin/` (pnpm relinks it last), `.cache/` and
579/// `.vite*/` (tool caches), and pnpm's own `.pnpm/lock.yaml`,
580/// `.pnpm-workspace-state*` and `.modules.yaml`. Symlinks are not followed —
581/// a top-level package is a link into `.pnpm`, which is walked itself.
582fn newer_than(nm: &Path, record: std::time::SystemTime) -> std::io::Result<Option<PathBuf>> {
583    if !nm.is_dir() {
584        return Ok(None);
585    }
586    let mut stack = vec![nm.to_path_buf()];
587    while let Some(at) = stack.pop() {
588        for entry in std::fs::read_dir(&at)? {
589            let entry = entry?;
590            let name = entry.file_name();
591            let name = name.to_string_lossy();
592            let kind = entry.file_type()?;
593            if kind.is_dir() {
594                if name == ".bin" || name == ".cache" || name.starts_with(".vite") {
595                    continue;
596                }
597                stack.push(entry.path());
598            } else if kind.is_file() {
599                if name == ".modules.yaml"
600                    || name.starts_with(".pnpm-workspace-state")
601                    || (name == "lock.yaml" && at.file_name().is_some_and(|d| d == ".pnpm"))
602                {
603                    continue;
604                }
605                if entry.metadata()?.modified()? > record {
606                    return Ok(Some(entry.path()));
607                }
608            }
609        }
610    }
611    Ok(None)
612}
613
614/// Workspace members of a pnpm unit, as pnpm lists them, relative to the
615/// unit dir. Asked in the SNAPSHOT: the committed manifests decide.
616fn members(settings: &crate::config::Settings, u: &Unit, at: &Path) -> Result<Vec<String>, String> {
617    if u.standalone {
618        return Ok(Vec::new());
619    }
620    let text = quiet(
621        settings,
622        manager_cmd(u, at, &["ls", "-r", "--depth", "-1", "--parseable"]),
623    )
624    .map_err(|t| format!("pnpm could not list the workspace: {}", last_line(&t)))?;
625    let root = normalize(at);
626    let canon = at.canonicalize().unwrap_or_else(|_| root.clone());
627    Ok(text
628        .lines()
629        .filter_map(|l| {
630            let p = PathBuf::from(l.trim());
631            let rel = p
632                .strip_prefix(&root)
633                .or_else(|_| p.strip_prefix(&canon))
634                .ok()?;
635            let rel = rel.to_string_lossy().replace('\\', "/");
636            (!rel.is_empty()).then_some(rel)
637        })
638        .collect())
639}
640
641/// Is `nm` laid out the way pnpm's default (`isolated`) linker lays it out —
642/// every package a link, `@scope` directories holding only links? pnpm's own
643/// check does not look at a directory it did not create, and a stray one
644/// here is importable by the suite; so anything else is not reused.
645fn pnpm_layout_problem(nm: &Path, is_root: bool) -> Option<String> {
646    if is_root {
647        let modules = std::fs::read_to_string(nm.join(".modules.yaml")).unwrap_or_default();
648        let isolated = modules
649            .lines()
650            .any(|l| l.contains("nodeLinker") && l.contains("isolated"));
651        if !isolated {
652            return Some("not installed with pnpm's isolated linker".to_string());
653        }
654    }
655    let entries = std::fs::read_dir(nm).ok()?;
656    for e in entries.flatten() {
657        let name = e.file_name().to_string_lossy().into_owned();
658        if name.starts_with('.') {
659            continue;
660        }
661        let kind = e.file_type().ok()?;
662        if kind.is_symlink() {
663            continue;
664        }
665        if name.starts_with('@') && kind.is_dir() {
666            for inner in std::fs::read_dir(e.path()).ok()?.flatten() {
667                if !inner.file_type().map(|k| k.is_symlink()).unwrap_or(false) {
668                    return Some(format!(
669                        "{} is not a link pnpm made",
670                        inner.path().display()
671                    ));
672                }
673            }
674            continue;
675        }
676        return Some(format!("{} is not a link pnpm made", e.path().display()));
677    }
678    None
679}
680
681/// Try to reuse the working tree's install for one unit. `Ok(())` when the
682/// clone is in place and pnpm accepted it; `Err(reason)` — with the snapshot
683/// left exactly as it was — when it cannot be.
684///
685/// npm is never reused. `npm ls` answers "is this dependency graph valid",
686/// not "is this the tree the lockfile describes": a real repository with
687/// peer-range conflicts `npm ci` installs without complaint fails `npm ls`
688/// on a fresh install (application-landscape: 182 findings), so it can
689/// neither accept a correct clone nor be trusted to reject a wrong one.
690fn reuse(
691    settings: &crate::config::Settings,
692    u: &Unit,
693    source: &Path,
694    snapshot: &Path,
695    unit_dirs: &[String],
696) -> Result<(), String> {
697    if u.manager == Manager::Npm {
698        return Err(
699            "npm cannot check an installed tree against its lockfile, so npm \
700                    snapshots always install"
701                .to_string(),
702        );
703    }
704    let src_dir = source.join(&u.dir);
705    let snap_dir = snapshot.join(&u.dir);
706    let lock = u.manager.lockfile();
707    let src_lock = std::fs::read(src_dir.join(lock))
708        .map_err(|_| "no lockfile in the working tree".to_string())?;
709    let snap_lock = std::fs::read(snap_dir.join(lock)).map_err(|e| format!("{lock}: {e}"))?;
710    if src_lock != snap_lock {
711        return Err("the working tree's lockfile differs from the commit's".to_string());
712    }
713    if !src_dir.join("node_modules").is_dir() {
714        return Err("nothing installed in the working tree".to_string());
715    }
716    let mut set = vec![String::new()];
717    for m in members(settings, u, &snap_dir)? {
718        let repo_rel = join_rel(&u.dir, &m);
719        // A member with a lockfile of its own is its own unit.
720        if unit_dirs.contains(&repo_rel) {
721            continue;
722        }
723        if src_dir.join(&m).join("node_modules").is_dir() {
724            set.push(m);
725        }
726    }
727    // Checked on the SOURCE, before anything is copied: a file changed
728    // since pnpm finished installing is an edit pnpm's own check will not
729    // see, and a clone would carry it into a stamped snapshot.
730    let record = std::fs::metadata(src_dir.join("node_modules/.modules.yaml"))
731        .and_then(|m| m.modified())
732        .map_err(|_| "no pnpm install record (node_modules/.modules.yaml)".to_string())?;
733    for m in &set {
734        match newer_than(&src_dir.join(m).join("node_modules"), record) {
735            Ok(None) => {}
736            Ok(Some(f)) => {
737                let shown = f.strip_prefix(source).unwrap_or(&f);
738                return Err(format!(
739                    "{} changed after the install — an in-place edit pnpm does not check",
740                    shown.display()
741                ));
742            }
743            Err(e) => return Err(format!("could not inspect the working tree's install: {e}")),
744        }
745    }
746    let mut cloned: Vec<PathBuf> = Vec::new();
747    let rollback = |cloned: &[PathBuf]| {
748        for d in cloned {
749            let _ = std::fs::remove_dir_all(d);
750        }
751    };
752    let sources = [
753        normalize(source),
754        source.canonicalize().unwrap_or_else(|_| normalize(source)),
755    ];
756    for m in &set {
757        let from = src_dir.join(m).join("node_modules");
758        let to = snap_dir.join(m).join("node_modules");
759        if std::fs::symlink_metadata(&to).is_ok() {
760            rollback(&cloned);
761            return Err(format!("{} already exists in the snapshot", to.display()));
762        }
763        if let Err(e) = clone_dir(&from, &to) {
764            rollback(&cloned);
765            return Err(e);
766        }
767        cloned.push(to.clone());
768        let problem = match link_into(&to, &sources) {
769            Ok(Some(link)) => Some(format!(
770                "{} links back into the working tree",
771                link.display()
772            )),
773            Ok(None) => pnpm_layout_problem(&to, m.is_empty()),
774            Err(e) => Some(format!("could not inspect the clone: {e}")),
775        };
776        if let Some(why) = problem {
777            rollback(&cloned);
778            return Err(why);
779        }
780    }
781    // pnpm records the absolute paths of the tree it installed; the clone
782    // must not carry the source's.
783    if let Ok(rd) = std::fs::read_dir(snap_dir.join("node_modules")) {
784        for e in rd.flatten() {
785            if e.file_name()
786                .to_string_lossy()
787                .starts_with(".pnpm-workspace-state")
788            {
789                let _ = std::fs::remove_file(e.path());
790            }
791        }
792    }
793    // A frozen OFFLINE install is both the check and the repair: it fails on
794    // a manifest the lockfile does not satisfy, relinks drift from the
795    // store, and says "Already up to date" for a faithful clone.
796    if let Err(text) = quiet(
797        settings,
798        manager_cmd(u, &snap_dir, &["install", "--frozen-lockfile", "--offline"]),
799    ) {
800        rollback(&cloned);
801        return Err(format!(
802            "pnpm did not accept the clone ({})",
803            last_line(&text)
804        ));
805    }
806    Ok(())
807}
808
809/// The files `tip` changes relative to where it forked from its upstream —
810/// what the push is about. `None` when git cannot say (no upstream, a tip
811/// it does not know): the caller then prepares everything.
812pub fn changed_since_upstream(source: &Path, tip: &str) -> Option<Vec<String>> {
813    let git = |args: &[&str]| -> Option<Vec<u8>> {
814        let mut cmd = Command::new("git");
815        cmd.arg("-C")
816            .arg(source)
817            .args(args)
818            .stdin(Stdio::null())
819            .stderr(Stdio::null());
820        crate::hooks::common::strip_git_env(&mut cmd);
821        let out = cmd.output().ok()?;
822        out.status.success().then_some(out.stdout)
823    };
824    let base = git(&["merge-base", tip, "@{upstream}"])?;
825    let base = String::from_utf8_lossy(&base).trim().to_string();
826    if base.is_empty() {
827        return None;
828    }
829    let raw = git(&["diff", "--name-only", "-z", &base, tip])?;
830    Some(crate::git::split_nul_paths(&raw))
831}
832
833/// Which units a snapshot needs: the root one, and every unit that is the
834/// NEAREST enclosing unit of a changed file. A unit the push does not touch
835/// cannot hold a failure the push introduced — and nothing the gates run for
836/// this push reads it — so installing it is pure cost (website-builder: seven
837/// spikes, an install each, on every snapshot). `changed` is `None` when the
838/// range is unknown, and then every unit is kept.
839///
840/// Returns `(kept, skipped)`.
841pub fn select_units(units: Vec<Unit>, changed: Option<&[String]>) -> (Vec<Unit>, Vec<Unit>) {
842    let Some(changed) = changed else {
843        return (units, Vec::new());
844    };
845    let nearest = |f: &str| -> Option<&str> {
846        units
847            .iter()
848            .map(|u| u.dir.as_str())
849            .filter(|d| d.is_empty() || f.starts_with(&format!("{d}/")))
850            .max_by_key(|d| d.len())
851    };
852    let touched: Vec<&str> = changed.iter().filter_map(|f| nearest(f)).collect();
853    let (kept, skipped): (Vec<Unit>, Vec<Unit>) = units
854        .clone()
855        .into_iter()
856        .partition(|u| u.dir.is_empty() || touched.contains(&u.dir.as_str()));
857    (kept, skipped)
858}
859
860/// Prepare the dependencies of every unit the push needs in `snapshot`.
861pub fn deps(
862    settings: &crate::config::Settings,
863    mode: DepsMode,
864    source: &Path,
865    snapshot: &Path,
866    files: &[String],
867    changed: Option<&[String]>,
868) -> Result<(), String> {
869    if mode == DepsMode::Off {
870        return Ok(());
871    }
872    let all = units(files, snapshot)?;
873    let unit_dirs: Vec<String> = all.iter().map(|u| u.dir.clone()).collect();
874    let (units, skipped) = select_units(all, changed);
875    if !skipped.is_empty() {
876        let names: Vec<String> = skipped.iter().map(unit_label).collect();
877        println!(
878            "snapshot: not preparing {} — the push changes nothing there",
879            names.join(", ")
880        );
881    }
882    for u in &units {
883        let at = snapshot.join(&u.dir);
884        let label = unit_label(u);
885        if mode == DepsMode::Reuse {
886            // No clone tool on Windows: `clone_dir` says so and this falls
887            // through to the install, like any other refusal.
888            match reuse(settings, u, source, snapshot, &unit_dirs) {
889                Ok(()) => {
890                    println!(
891                        "snapshot: node_modules in {label} reused from the working tree — {} \
892                         accepted it against the lockfile",
893                        u.manager.tool()
894                    );
895                    continue;
896                }
897                Err(why) => println!(
898                    "snapshot: {} in {label} — not reusing the working tree's install: {why}",
899                    highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
900                ),
901            }
902        } else {
903            println!(
904                "snapshot: {} in {label}",
905                highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
906            );
907        }
908        install(settings, u, &at)?;
909    }
910    Ok(())
911}
912
913fn install_verb(m: Manager) -> &'static str {
914    match m {
915        Manager::Npm => "ci",
916        Manager::Pnpm => "install --frozen-lockfile",
917    }
918}
919
920/// Carry → dependencies → nothing else; `snapshotPrepare` runs after this,
921/// in `PushedTree::prepare`. Dependencies are skipped when a
922/// `snapshotPrepare` command exists: that command owns them.
923pub fn run(
924    settings: &crate::config::Settings,
925    source: &Path,
926    snapshot: &Path,
927    tip: &str,
928    command_owns_deps: bool,
929) -> Result<(), String> {
930    let entries = carry_list(settings);
931    let mode = if command_owns_deps {
932        DepsMode::Off
933    } else {
934        deps_mode(settings)
935    };
936    if entries.is_empty() && mode == DepsMode::Off {
937        return Ok(());
938    }
939    let files = snapshot_files(snapshot)?;
940    if !entries.is_empty() {
941        carry(&entries, source, snapshot, &files)?;
942    }
943    let changed = if mode == DepsMode::Off {
944        None
945    } else {
946        changed_since_upstream(source, tip)
947    };
948    deps(settings, mode, source, snapshot, &files, changed.as_deref()).map_err(|e| {
949        println!("{} {e}", warning_sign());
950        e
951    })
952}
953
954#[cfg(test)]
955mod tests {
956    use super::*;
957
958    fn s(v: &[&str]) -> Vec<String> {
959        v.iter().map(|x| x.to_string()).collect()
960    }
961
962    #[test]
963    fn lexical_refusals() {
964        for bad in [
965            "",
966            "/etc/hosts",
967            "\\x",
968            "C:/x",
969            "a//b",
970            ".",
971            "..",
972            "a/../b",
973            ".git/config",
974            "x/.GIT/y",
975        ] {
976            assert!(lexical_problem(bad).is_some(), "{bad:?} must be refused");
977        }
978        for good in [".env", "config/local.json", ".npmrc", "a.b/c"] {
979            assert!(lexical_problem(good).is_none(), "{good:?} must pass");
980        }
981    }
982
983    #[test]
984    fn a_tracked_path_inside_above_or_equal_collides() {
985        let files = s(&["src/a.ts", "vendor", "README.md"]);
986        assert!(tracked_collision("src", &files).is_some());
987        assert!(tracked_collision("src/a.ts", &files).is_some());
988        assert!(tracked_collision("vendor/x", &files).is_some());
989        assert!(tracked_collision(".env", &files).is_none());
990        assert!(tracked_collision("sr", &files).is_none());
991    }
992
993    #[test]
994    fn lockfile_dirs_come_from_the_list_given() {
995        let files = s(&[
996            "package-lock.json",
997            "web/package-lock.json",
998            "web/x/pnpm-lock.yaml",
999            "notpackage-lock.json",
1000        ]);
1001        assert_eq!(
1002            lockfile_dirs_in(&files, "package-lock.json"),
1003            s(&["", "web"])
1004        );
1005        assert_eq!(lockfile_dirs_in(&files, "pnpm-lock.yaml"), s(&["web/x"]));
1006    }
1007
1008    #[test]
1009    fn package_manager_field_is_read_narrowly() {
1010        assert_eq!(
1011            declared_manager(r#"{"packageManager": "pnpm@10.1.0"}"#),
1012            Some(Manager::Pnpm)
1013        );
1014        assert_eq!(
1015            declared_manager(r#"{"packageManager":"npm@11"}"#),
1016            Some(Manager::Npm)
1017        );
1018        assert_eq!(declared_manager(r#"{"packageManager":"yarn@4"}"#), None);
1019        assert_eq!(declared_manager(r#"{"name":"x"}"#), None);
1020        assert_eq!(
1021            declared_manager(r#"{"packageManager":"npm@1","x":{"packageManager":"pnpm@1"}}"#),
1022            None,
1023            "two answers is no answer"
1024        );
1025    }
1026
1027    fn unit(dir: &str) -> Unit {
1028        Unit {
1029            dir: dir.to_string(),
1030            manager: Manager::Pnpm,
1031            standalone: !dir.is_empty(),
1032        }
1033    }
1034
1035    #[test]
1036    fn only_the_root_and_the_touched_units_are_kept() {
1037        let all = vec![unit(""), unit("spikes/a"), unit("spikes/b"), unit("tools")];
1038        let changed = s(&["src/x.ts", "spikes/b/index.ts", "spikes/bb/y.ts"]);
1039        let (kept, skipped) = select_units(all.clone(), Some(&changed));
1040        let dirs = |v: &[Unit]| v.iter().map(|u| u.dir.clone()).collect::<Vec<_>>();
1041        assert_eq!(
1042            dirs(&kept),
1043            s(&["", "spikes/b"]),
1044            "spikes/bb is not spikes/b"
1045        );
1046        assert_eq!(dirs(&skipped), s(&["spikes/a", "tools"]));
1047        let (kept, skipped) = select_units(all.clone(), None);
1048        assert_eq!(kept.len(), 4, "an unknown range keeps everything");
1049        assert!(skipped.is_empty());
1050    }
1051
1052    #[test]
1053    fn a_nested_unit_claims_its_files_from_the_outer_one() {
1054        let all = vec![unit("web"), unit("web/nested")];
1055        let changed = s(&["web/nested/a.ts"]);
1056        let (kept, _) = select_units(all, Some(&changed));
1057        assert_eq!(kept.len(), 1);
1058        assert_eq!(kept[0].dir, "web/nested");
1059    }
1060
1061    #[test]
1062    fn a_file_newer_than_the_install_record_is_found() {
1063        let d = std::env::temp_dir().join(format!("newer-{}", std::process::id()));
1064        let _ = std::fs::remove_dir_all(&d);
1065        let pkg = d.join(".pnpm/a@1/node_modules/a");
1066        std::fs::create_dir_all(&pkg).unwrap();
1067        std::fs::create_dir_all(d.join(".bin")).unwrap();
1068        std::fs::create_dir_all(d.join(".cache")).unwrap();
1069        std::fs::write(pkg.join("index.js"), "old").unwrap();
1070        let record = std::time::SystemTime::now();
1071        std::thread::sleep(std::time::Duration::from_millis(20));
1072        for skipped in [".bin/tsc", ".cache/x", ".pnpm/lock.yaml", ".modules.yaml"] {
1073            std::fs::write(d.join(skipped), "new").unwrap();
1074        }
1075        assert_eq!(
1076            newer_than(&d, record).unwrap(),
1077            None,
1078            "the install's own writes"
1079        );
1080        std::fs::write(pkg.join("index.js"), "edited").unwrap();
1081        assert_eq!(newer_than(&d, record).unwrap(), Some(pkg.join("index.js")));
1082        let _ = std::fs::remove_dir_all(&d);
1083    }
1084
1085    #[test]
1086    fn normalize_folds_dots_lexically() {
1087        assert_eq!(
1088            normalize(Path::new("/a/b/../c/./d")),
1089            PathBuf::from("/a/c/d")
1090        );
1091    }
1092}