verctl-core 0.9.7

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
//! Infer a lockfile follow-up from the tree. Same signals mise/corepack use.

use std::fs;
use std::path::{Path, PathBuf};

/// The command that owns a manifest's lockfile: what to run, where, and the
/// lockfile it rewrites.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Owner {
    /// The command, such as `cargo update --workspace`.
    pub argv: Vec<String>,
    /// Where it runs: the directory containing the lockfile, or the manifest's
    /// when no lockfile exists yet.
    pub dir: PathBuf,
    /// The lockfile the command rewrites, when the tree has one.
    pub lockfile: Option<PathBuf>,
}

impl Owner {
    /// The argv joined by spaces, as a report and a driver's `after` show it.
    #[must_use]
    pub fn command(&self) -> String {
        self.argv.join(" ")
    }

    fn new(command: &str, dir: &Path, lockfile: Option<PathBuf>) -> Self {
        let dir = lockfile
            .as_deref()
            .and_then(Path::parent)
            .map_or_else(|| dir.to_path_buf(), Path::to_path_buf);
        Self {
            argv: command.split_whitespace().map(str::to_owned).collect(),
            dir,
            lockfile,
        }
    }
}

/// The command that refreshes the lockfile after `manifest`'s version moves,
/// when the tree has one.
#[must_use]
pub fn follow_up(manifest: &Path) -> Option<String> {
    owner(manifest).map(|owner| owner.command())
}

/// The command that owns `manifest`'s lockfile, found by the signals of
/// [`follow_up`].
#[must_use]
pub fn owner(manifest: &Path) -> Option<Owner> {
    let dir = manifest.parent().unwrap_or(manifest);
    let file_name = manifest.file_name()?.to_str()?;
    match file_name {
        "Cargo.toml" => cargo_owner(dir),
        "package.json" => javascript_owner(dir),
        "pyproject.toml" => python_owner(dir),
        _ => None,
    }
}

fn cargo_owner(dir: &Path) -> Option<Owner> {
    // Not `generate-lockfile`: that re-resolves every dependency, so a bump
    // would carry unrelated moves. Only this package's version went stale.
    let lockfile = find(dir, "Cargo.lock")?;
    Some(Owner::new("cargo update --workspace", dir, Some(lockfile)))
}

/// `uv.lock` records the project's own version, so a bump leaves it stale.
/// `uv lock` keeps every locked dependency it can ([uv: upgrading locked
/// package versions]), so only that version moves.
///
/// [uv: upgrading locked package versions]: https://docs.astral.sh/uv/concepts/projects/sync/#upgrading-locked-package-versions
fn python_owner(dir: &Path) -> Option<Owner> {
    let lockfile = find(dir, "uv.lock")?;
    Some(Owner::new("uv lock", dir, Some(lockfile)))
}

/// The lockfiles each JavaScript package manager writes, in the order the
/// tree is searched.
const JAVASCRIPT_LOCKFILES: [(&str, &str); 6] = [
    ("bun", "bun.lock"),
    ("bun", "bun.lockb"),
    ("pnpm", "pnpm-lock.yaml"),
    ("yarn", "yarn.lock"),
    ("npm", "package-lock.json"),
    ("npm", "npm-shrinkwrap.json"),
];

fn javascript_owner(dir: &Path) -> Option<Owner> {
    if let Some(name) = package_manager_field(&dir.join("package.json")) {
        let lockfile = JAVASCRIPT_LOCKFILES
            .iter()
            .filter(|(manager, _)| *manager == name)
            .find_map(|(_, file_name)| find(dir, file_name));
        return Some(Owner::new(&install_command(&name), dir, lockfile));
    }
    for ancestor in scope(dir) {
        for (manager, file_name) in JAVASCRIPT_LOCKFILES {
            let lockfile = ancestor.join(file_name);
            if lockfile.is_file() {
                return Some(Owner::new(&install_command(manager), dir, Some(lockfile)));
            }
        }
    }
    None
}

/// `dir` and every ancestor up to the working tree that contains it.
///
/// The repository root is the ceiling: a lockfile above it belongs to
/// some other project that happens to contain this one on disk. Without
/// a repository the search never leaves `dir` at all, so a lockfile in a
/// parent directory is never matched to this manifest.
fn scope(dir: &Path) -> Vec<PathBuf> {
    let dir = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf());
    let ceiling = workdir_covering(&dir);
    let mut dirs = Vec::new();
    for ancestor in dir.ancestors() {
        dirs.push(ancestor.to_path_buf());
        if ceiling.as_deref().is_none_or(|ceiling| ancestor == ceiling) {
            break;
        }
    }
    dirs
}

/// The nearest `file_name` in [`scope`].
fn find(dir: &Path, file_name: &str) -> Option<PathBuf> {
    scope(dir)
        .into_iter()
        .map(|ancestor| ancestor.join(file_name))
        .find(|path| path.is_file())
}

fn package_manager_field(package_json: &Path) -> Option<String> {
    let raw = fs::read_to_string(package_json).ok()?;
    let value: serde_json::Value = serde_json::from_str(&raw).ok()?;
    let field = value.get("packageManager")?.as_str()?;
    let name = field.split('@').next()?.trim();
    (!name.is_empty()).then(|| name.to_owned())
}

fn install_command(manager: &str) -> String {
    match manager {
        "bun" => "bun install".into(),
        "pnpm" => "pnpm install".into(),
        "yarn" => "yarn install".into(),
        other => format!("{other} install"),
    }
}

/// Nearest ancestor of `dir` containing a `.git` entry, canonicalized.
fn workdir_covering(dir: &Path) -> Option<PathBuf> {
    let start = dir.canonicalize().ok()?;
    start
        .ancestors()
        .find(|ancestor| ancestor.join(".git").exists())
        .map(Path::to_path_buf)
}

#[cfg(test)]
mod tests;