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/// Workspace members of a pnpm unit, as pnpm lists them, relative to the
575/// unit dir. Asked in the SNAPSHOT: the committed manifests decide.
576fn members(settings: &crate::config::Settings, u: &Unit, at: &Path) -> Result<Vec<String>, String> {
577    if u.standalone {
578        return Ok(Vec::new());
579    }
580    let text = quiet(
581        settings,
582        manager_cmd(u, at, &["ls", "-r", "--depth", "-1", "--parseable"]),
583    )
584    .map_err(|t| format!("pnpm could not list the workspace: {}", last_line(&t)))?;
585    let root = normalize(at);
586    let canon = at.canonicalize().unwrap_or_else(|_| root.clone());
587    Ok(text
588        .lines()
589        .filter_map(|l| {
590            let p = PathBuf::from(l.trim());
591            let rel = p
592                .strip_prefix(&root)
593                .or_else(|_| p.strip_prefix(&canon))
594                .ok()?;
595            let rel = rel.to_string_lossy().replace('\\', "/");
596            (!rel.is_empty()).then_some(rel)
597        })
598        .collect())
599}
600
601/// Is `nm` laid out the way pnpm's default (`isolated`) linker lays it out —
602/// every package a link, `@scope` directories holding only links? pnpm's own
603/// check does not look at a directory it did not create, and a stray one
604/// here is importable by the suite; so anything else is not reused.
605fn pnpm_layout_problem(nm: &Path, is_root: bool) -> Option<String> {
606    if is_root {
607        let modules = std::fs::read_to_string(nm.join(".modules.yaml")).unwrap_or_default();
608        let isolated = modules
609            .lines()
610            .any(|l| l.contains("nodeLinker") && l.contains("isolated"));
611        if !isolated {
612            return Some("not installed with pnpm's isolated linker".to_string());
613        }
614    }
615    let entries = std::fs::read_dir(nm).ok()?;
616    for e in entries.flatten() {
617        let name = e.file_name().to_string_lossy().into_owned();
618        if name.starts_with('.') {
619            continue;
620        }
621        let kind = e.file_type().ok()?;
622        if kind.is_symlink() {
623            continue;
624        }
625        if name.starts_with('@') && kind.is_dir() {
626            for inner in std::fs::read_dir(e.path()).ok()?.flatten() {
627                if !inner.file_type().map(|k| k.is_symlink()).unwrap_or(false) {
628                    return Some(format!(
629                        "{} is not a link pnpm made",
630                        inner.path().display()
631                    ));
632                }
633            }
634            continue;
635        }
636        return Some(format!("{} is not a link pnpm made", e.path().display()));
637    }
638    None
639}
640
641/// Try to reuse the working tree's install for one unit. `Ok(())` when the
642/// clone is in place and pnpm accepted it; `Err(reason)` — with the snapshot
643/// left exactly as it was — when it cannot be.
644///
645/// npm is never reused. `npm ls` answers "is this dependency graph valid",
646/// not "is this the tree the lockfile describes": a real repository with
647/// peer-range conflicts `npm ci` installs without complaint fails `npm ls`
648/// on a fresh install (application-landscape: 182 findings), so it can
649/// neither accept a correct clone nor be trusted to reject a wrong one.
650fn reuse(
651    settings: &crate::config::Settings,
652    u: &Unit,
653    source: &Path,
654    snapshot: &Path,
655    unit_dirs: &[String],
656) -> Result<(), String> {
657    if u.manager == Manager::Npm {
658        return Err(
659            "npm cannot check an installed tree against its lockfile, so npm \
660                    snapshots always install"
661                .to_string(),
662        );
663    }
664    let src_dir = source.join(&u.dir);
665    let snap_dir = snapshot.join(&u.dir);
666    let lock = u.manager.lockfile();
667    let src_lock = std::fs::read(src_dir.join(lock))
668        .map_err(|_| "no lockfile in the working tree".to_string())?;
669    let snap_lock = std::fs::read(snap_dir.join(lock)).map_err(|e| format!("{lock}: {e}"))?;
670    if src_lock != snap_lock {
671        return Err("the working tree's lockfile differs from the commit's".to_string());
672    }
673    if !src_dir.join("node_modules").is_dir() {
674        return Err("nothing installed in the working tree".to_string());
675    }
676    let mut set = vec![String::new()];
677    for m in members(settings, u, &snap_dir)? {
678        let repo_rel = join_rel(&u.dir, &m);
679        // A member with a lockfile of its own is its own unit.
680        if unit_dirs.contains(&repo_rel) {
681            continue;
682        }
683        if src_dir.join(&m).join("node_modules").is_dir() {
684            set.push(m);
685        }
686    }
687    let mut cloned: Vec<PathBuf> = Vec::new();
688    let rollback = |cloned: &[PathBuf]| {
689        for d in cloned {
690            let _ = std::fs::remove_dir_all(d);
691        }
692    };
693    let sources = [
694        normalize(source),
695        source.canonicalize().unwrap_or_else(|_| normalize(source)),
696    ];
697    for m in &set {
698        let from = src_dir.join(m).join("node_modules");
699        let to = snap_dir.join(m).join("node_modules");
700        if std::fs::symlink_metadata(&to).is_ok() {
701            rollback(&cloned);
702            return Err(format!("{} already exists in the snapshot", to.display()));
703        }
704        if let Err(e) = clone_dir(&from, &to) {
705            rollback(&cloned);
706            return Err(e);
707        }
708        cloned.push(to.clone());
709        let problem = match link_into(&to, &sources) {
710            Ok(Some(link)) => Some(format!(
711                "{} links back into the working tree",
712                link.display()
713            )),
714            Ok(None) => pnpm_layout_problem(&to, m.is_empty()),
715            Err(e) => Some(format!("could not inspect the clone: {e}")),
716        };
717        if let Some(why) = problem {
718            rollback(&cloned);
719            return Err(why);
720        }
721    }
722    // pnpm records the absolute paths of the tree it installed; the clone
723    // must not carry the source's.
724    if let Ok(rd) = std::fs::read_dir(snap_dir.join("node_modules")) {
725        for e in rd.flatten() {
726            if e.file_name()
727                .to_string_lossy()
728                .starts_with(".pnpm-workspace-state")
729            {
730                let _ = std::fs::remove_file(e.path());
731            }
732        }
733    }
734    // A frozen OFFLINE install is both the check and the repair: it fails on
735    // a manifest the lockfile does not satisfy, relinks drift from the
736    // store, and says "Already up to date" for a faithful clone.
737    if let Err(text) = quiet(
738        settings,
739        manager_cmd(u, &snap_dir, &["install", "--frozen-lockfile", "--offline"]),
740    ) {
741        rollback(&cloned);
742        return Err(format!(
743            "pnpm did not accept the clone ({})",
744            last_line(&text)
745        ));
746    }
747    Ok(())
748}
749
750/// Prepare every unit's dependencies in `snapshot`.
751pub fn deps(
752    settings: &crate::config::Settings,
753    mode: DepsMode,
754    source: &Path,
755    snapshot: &Path,
756    files: &[String],
757) -> Result<(), String> {
758    if mode == DepsMode::Off {
759        return Ok(());
760    }
761    let units = units(files, snapshot)?;
762    let unit_dirs: Vec<String> = units.iter().map(|u| u.dir.clone()).collect();
763    for u in &units {
764        let at = snapshot.join(&u.dir);
765        let label = unit_label(u);
766        if mode == DepsMode::Reuse {
767            // No clone tool on Windows: `clone_dir` says so and this falls
768            // through to the install, like any other refusal.
769            match reuse(settings, u, source, snapshot, &unit_dirs) {
770                Ok(()) => {
771                    println!(
772                        "snapshot: node_modules in {label} reused from the working tree — {} \
773                         accepted it against the lockfile",
774                        u.manager.tool()
775                    );
776                    continue;
777                }
778                Err(why) => println!(
779                    "snapshot: {} in {label} — not reusing the working tree's install: {why}",
780                    highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
781                ),
782            }
783        } else {
784            println!(
785                "snapshot: {} in {label}",
786                highlight(&format!("{} {}", u.manager.tool(), install_verb(u.manager)))
787            );
788        }
789        install(settings, u, &at)?;
790    }
791    Ok(())
792}
793
794fn install_verb(m: Manager) -> &'static str {
795    match m {
796        Manager::Npm => "ci",
797        Manager::Pnpm => "install --frozen-lockfile",
798    }
799}
800
801/// Carry → dependencies → nothing else; `snapshotPrepare` runs after this,
802/// in `PushedTree::prepare`. Dependencies are skipped when a
803/// `snapshotPrepare` command exists: that command owns them.
804pub fn run(
805    settings: &crate::config::Settings,
806    source: &Path,
807    snapshot: &Path,
808    command_owns_deps: bool,
809) -> Result<(), String> {
810    let entries = carry_list(settings);
811    let mode = if command_owns_deps {
812        DepsMode::Off
813    } else {
814        deps_mode(settings)
815    };
816    if entries.is_empty() && mode == DepsMode::Off {
817        return Ok(());
818    }
819    let files = snapshot_files(snapshot)?;
820    if !entries.is_empty() {
821        carry(&entries, source, snapshot, &files)?;
822    }
823    deps(settings, mode, source, snapshot, &files).map_err(|e| {
824        println!("{} {e}", warning_sign());
825        e
826    })
827}
828
829#[cfg(test)]
830mod tests {
831    use super::*;
832
833    fn s(v: &[&str]) -> Vec<String> {
834        v.iter().map(|x| x.to_string()).collect()
835    }
836
837    #[test]
838    fn lexical_refusals() {
839        for bad in [
840            "",
841            "/etc/hosts",
842            "\\x",
843            "C:/x",
844            "a//b",
845            ".",
846            "..",
847            "a/../b",
848            ".git/config",
849            "x/.GIT/y",
850        ] {
851            assert!(lexical_problem(bad).is_some(), "{bad:?} must be refused");
852        }
853        for good in [".env", "config/local.json", ".npmrc", "a.b/c"] {
854            assert!(lexical_problem(good).is_none(), "{good:?} must pass");
855        }
856    }
857
858    #[test]
859    fn a_tracked_path_inside_above_or_equal_collides() {
860        let files = s(&["src/a.ts", "vendor", "README.md"]);
861        assert!(tracked_collision("src", &files).is_some());
862        assert!(tracked_collision("src/a.ts", &files).is_some());
863        assert!(tracked_collision("vendor/x", &files).is_some());
864        assert!(tracked_collision(".env", &files).is_none());
865        assert!(tracked_collision("sr", &files).is_none());
866    }
867
868    #[test]
869    fn lockfile_dirs_come_from_the_list_given() {
870        let files = s(&[
871            "package-lock.json",
872            "web/package-lock.json",
873            "web/x/pnpm-lock.yaml",
874            "notpackage-lock.json",
875        ]);
876        assert_eq!(
877            lockfile_dirs_in(&files, "package-lock.json"),
878            s(&["", "web"])
879        );
880        assert_eq!(lockfile_dirs_in(&files, "pnpm-lock.yaml"), s(&["web/x"]));
881    }
882
883    #[test]
884    fn package_manager_field_is_read_narrowly() {
885        assert_eq!(
886            declared_manager(r#"{"packageManager": "pnpm@10.1.0"}"#),
887            Some(Manager::Pnpm)
888        );
889        assert_eq!(
890            declared_manager(r#"{"packageManager":"npm@11"}"#),
891            Some(Manager::Npm)
892        );
893        assert_eq!(declared_manager(r#"{"packageManager":"yarn@4"}"#), None);
894        assert_eq!(declared_manager(r#"{"name":"x"}"#), None);
895        assert_eq!(
896            declared_manager(r#"{"packageManager":"npm@1","x":{"packageManager":"pnpm@1"}}"#),
897            None,
898            "two answers is no answer"
899        );
900    }
901
902    #[test]
903    fn normalize_folds_dots_lexically() {
904        assert_eq!(
905            normalize(Path::new("/a/b/../c/./d")),
906            PathBuf::from("/a/c/d")
907        );
908    }
909}