Unverified Commit ec51a56d authored by Lily Foster's avatar Lily Foster
Browse files

prefetch-npm-deps: detect and error out when generating an empty cache

parent e4ad9895
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -43,6 +43,13 @@ impl Cache {
        Cache(path)
    }

    pub fn init(&self) -> anyhow::Result<()> {
        fs::create_dir_all(self.0.join("content-v2"))?;
        fs::create_dir_all(self.0.join("index-v5"))?;

        Ok(())
    }

    pub fn put(
        &self,
        key: String,
+6 −1
Original line number Diff line number Diff line
@@ -234,9 +234,14 @@ fn main() -> anyhow::Result<()> {
        (out_tempdir.path(), true)
    };

    let packages = parse::lockfile(&lock_content, env::var("FORCE_GIT_DEPS").is_ok())?;
    let packages = parse::lockfile(
        &lock_content,
        env::var("FORCE_GIT_DEPS").is_ok(),
        env::var("FORCE_EMPTY_CACHE").is_ok(),
    )?;

    let cache = Cache::new(out.join("_cacache"));
    cache.init()?;

    packages.into_par_iter().try_for_each(|package| {
        eprintln!("{}", package.name);
+16 −2
Original line number Diff line number Diff line
@@ -14,7 +14,11 @@ use crate::util;

pub mod lock;

pub fn lockfile(content: &str, force_git_deps: bool) -> anyhow::Result<Vec<Package>> {
pub fn lockfile(
    content: &str,
    force_git_deps: bool,
    force_empty_cache: bool,
) -> anyhow::Result<Vec<Package>> {
    let mut packages = lock::packages(content)
        .context("failed to extract packages from lockfile")?
        .into_par_iter()
@@ -25,6 +29,10 @@ pub fn lockfile(content: &str, force_git_deps: bool) -> anyhow::Result<Vec<Packa
        })
        .collect::<anyhow::Result<Vec<_>>>()?;

    if packages.is_empty() && !force_empty_cache {
        bail!("No cacheable dependencies were found. Please inspect the upstream `package-lock.json` file and ensure that remote dependencies have `resolved` URLs and `integrity` hashes. If the lockfile is missing this data, attempt to get upstream to fix it via a tool like <https://github.com/jeslie0/npm-lockfile-fix>. If generating an empty cache is intentional and you would like to do it anyways, set `forceEmptyCache = true`.");
    }

    let mut new = Vec::new();

    for pkg in packages
@@ -64,7 +72,13 @@ pub fn lockfile(content: &str, force_git_deps: bool) -> anyhow::Result<Vec<Packa
        }

        if let Ok(lockfile_contents) = lockfile_contents {
            new.append(&mut lockfile(&lockfile_contents, force_git_deps)?);
            new.append(&mut lockfile(
                &lockfile_contents,
                force_git_deps,
                // force_empty_cache is turned on here since recursively parsed lockfiles should be
                // allowed to have an empty cache without erroring by default
                true,
            )?);
        }
    }