From 292ef23e77da6b003b2a38809d583550374f980e Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 13 Oct 2020 13:52:55 +0200 Subject: [PATCH] avoid error when saving without git dependencies --- dist/restore/index.js | 93 +++++++++++++++++++++ dist/save/index.js | 189 ++++++++++++++++++++++-------------------- src/common.ts | 91 ++++++++++++++++++++ src/restore.ts | 9 +- src/save.ts | 103 +++-------------------- 5 files changed, 302 insertions(+), 183 deletions(-) diff --git a/dist/restore/index.js b/dist/restore/index.js index 0fce98e..0333114 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -54581,6 +54581,9 @@ var exec = __webpack_require__(1514); // EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js var glob = __webpack_require__(8090); +// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js +var io = __webpack_require__(7436); + // EXTERNAL MODULE: external "crypto" var external_crypto_ = __webpack_require__(6417); var external_crypto_default = /*#__PURE__*/__webpack_require__.n(external_crypto_); @@ -54612,6 +54615,7 @@ var __asyncValues = (undefined && undefined.__asyncValues) || function (o) { + const stateKey = "RUST_CACHE_KEY"; const stateHash = "RUST_CACHE_HASH"; const home = external_os_default().homedir(); @@ -54692,6 +54696,90 @@ async function getLockfileHash() { } return hasher.digest("hex").slice(0, 20); } +async function getPackages() { + const cwd = process.cwd(); + const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"])); + return meta.packages + .filter((p) => !p.manifest_path.startsWith(cwd)) + .map((p) => { + const targets = p.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); + return { name: p.name, version: p.version, targets, path: external_path_default().dirname(p.manifest_path) }; + }); +} +async function cleanTarget(packages) { + var e_2, _a; + await external_fs_default().promises.unlink("./target/.rustc_info.json"); + await io.rmRF("./target/debug/examples"); + await io.rmRF("./target/debug/incremental"); + let dir; + // remove all *files* from debug + dir = await external_fs_default().promises.opendir("./target/debug"); + try { + for (var dir_1 = __asyncValues(dir), dir_1_1; dir_1_1 = await dir_1.next(), !dir_1_1.done;) { + const dirent = dir_1_1.value; + if (dirent.isFile()) { + await rm(dir.path, dirent); + } + } + } + catch (e_2_1) { e_2 = { error: e_2_1 }; } + finally { + try { + if (dir_1_1 && !dir_1_1.done && (_a = dir_1.return)) await _a.call(dir_1); + } + finally { if (e_2) throw e_2.error; } + } + const keepPkg = new Set(packages.map((p) => p.name)); + await rmExcept("./target/debug/build", keepPkg); + await rmExcept("./target/debug/.fingerprint", keepPkg); + const keepDeps = new Set(packages.flatMap((p) => { + const names = []; + for (const n of [p.name, ...p.targets]) { + const name = n.replace(/-/g, "_"); + names.push(name, `lib${name}`); + } + return names; + })); + await rmExcept("./target/debug/deps", keepDeps); +} +const oneWeek = 7 * 24 * 3600 * 1000; +async function rmExcept(dirName, keepPrefix) { + var e_3, _a; + const dir = await external_fs_default().promises.opendir(dirName); + try { + for (var dir_2 = __asyncValues(dir), dir_2_1; dir_2_1 = await dir_2.next(), !dir_2_1.done;) { + const dirent = dir_2_1.value; + let name = dirent.name; + const idx = name.lastIndexOf("-"); + if (idx !== -1) { + name = name.slice(0, idx); + } + const fileName = external_path_default().join(dir.path, dirent.name); + const { mtime } = await external_fs_default().promises.stat(fileName); + // we don’t really know + if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) { + await rm(dir.path, dirent); + } + } + } + catch (e_3_1) { e_3 = { error: e_3_1 }; } + finally { + try { + if (dir_2_1 && !dir_2_1.done && (_a = dir_2.return)) await _a.call(dir_2); + } + finally { if (e_3) throw e_3.error; } + } +} +async function rm(parent, dirent) { + const fileName = external_path_default().join(parent, dirent.name); + core.debug(`deleting "${fileName}"`); + if (dirent.isFile()) { + await external_fs_default().promises.unlink(fileName); + } + else if (dirent.isDirectory()) { + await io.rmRF(fileName); + } +} // CONCATENATED MODULE: ./src/restore.ts @@ -54716,6 +54804,11 @@ async function run() { else { core.info("No cache found."); } + if (restoreKey !== key) { + // pre-clean the target directory on cache mismatch + const packages = await getPackages(); + await cleanTarget(packages); + } } catch (e) { core.info(`[warning] ${e.message}`); diff --git a/dist/save/index.js b/dist/save/index.js index 6ee6c87..e3d5a8c 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -54581,6 +54581,9 @@ var exec = __webpack_require__(1514); // EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js var glob = __webpack_require__(8090); +// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js +var io = __webpack_require__(7436); + // EXTERNAL MODULE: external "crypto" var external_crypto_ = __webpack_require__(6417); var external_crypto_default = /*#__PURE__*/__webpack_require__.n(external_crypto_); @@ -54589,9 +54592,6 @@ var external_crypto_default = /*#__PURE__*/__webpack_require__.n(external_crypto var external_fs_ = __webpack_require__(5747); var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_); -// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js -var io = __webpack_require__(7436); - // EXTERNAL MODULE: external "os" var external_os_ = __webpack_require__(2087); var external_os_default = /*#__PURE__*/__webpack_require__.n(external_os_); @@ -54615,6 +54615,7 @@ var __asyncValues = (undefined && undefined.__asyncValues) || function (o) { + const stateKey = "RUST_CACHE_KEY"; const stateHash = "RUST_CACHE_HASH"; const home = external_os_default().homedir(); @@ -54695,6 +54696,90 @@ async function getLockfileHash() { } return hasher.digest("hex").slice(0, 20); } +async function getPackages() { + const cwd = process.cwd(); + const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"])); + return meta.packages + .filter((p) => !p.manifest_path.startsWith(cwd)) + .map((p) => { + const targets = p.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); + return { name: p.name, version: p.version, targets, path: external_path_default().dirname(p.manifest_path) }; + }); +} +async function cleanTarget(packages) { + var e_2, _a; + await external_fs_default().promises.unlink("./target/.rustc_info.json"); + await io.rmRF("./target/debug/examples"); + await io.rmRF("./target/debug/incremental"); + let dir; + // remove all *files* from debug + dir = await external_fs_default().promises.opendir("./target/debug"); + try { + for (var dir_1 = __asyncValues(dir), dir_1_1; dir_1_1 = await dir_1.next(), !dir_1_1.done;) { + const dirent = dir_1_1.value; + if (dirent.isFile()) { + await rm(dir.path, dirent); + } + } + } + catch (e_2_1) { e_2 = { error: e_2_1 }; } + finally { + try { + if (dir_1_1 && !dir_1_1.done && (_a = dir_1.return)) await _a.call(dir_1); + } + finally { if (e_2) throw e_2.error; } + } + const keepPkg = new Set(packages.map((p) => p.name)); + await rmExcept("./target/debug/build", keepPkg); + await rmExcept("./target/debug/.fingerprint", keepPkg); + const keepDeps = new Set(packages.flatMap((p) => { + const names = []; + for (const n of [p.name, ...p.targets]) { + const name = n.replace(/-/g, "_"); + names.push(name, `lib${name}`); + } + return names; + })); + await rmExcept("./target/debug/deps", keepDeps); +} +const oneWeek = 7 * 24 * 3600 * 1000; +async function rmExcept(dirName, keepPrefix) { + var e_3, _a; + const dir = await external_fs_default().promises.opendir(dirName); + try { + for (var dir_2 = __asyncValues(dir), dir_2_1; dir_2_1 = await dir_2.next(), !dir_2_1.done;) { + const dirent = dir_2_1.value; + let name = dirent.name; + const idx = name.lastIndexOf("-"); + if (idx !== -1) { + name = name.slice(0, idx); + } + const fileName = external_path_default().join(dir.path, dirent.name); + const { mtime } = await external_fs_default().promises.stat(fileName); + // we don’t really know + if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) { + await rm(dir.path, dirent); + } + } + } + catch (e_3_1) { e_3 = { error: e_3_1 }; } + finally { + try { + if (dir_2_1 && !dir_2_1.done && (_a = dir_2.return)) await _a.call(dir_2); + } + finally { if (e_3) throw e_3.error; } + } +} +async function rm(parent, dirent) { + const fileName = external_path_default().join(parent, dirent.name); + core.debug(`deleting "${fileName}"`); + if (dirent.isFile()) { + await external_fs_default().promises.unlink(fileName); + } + else if (dirent.isDirectory()) { + await io.rmRF(fileName); + } +} // CONCATENATED MODULE: ./src/save.ts var save_asyncValues = (undefined && undefined.__asyncValues) || function (o) { @@ -54727,9 +54812,18 @@ async function run() { await macOsWorkaround(); const registryName = await getRegistryName(); const packages = await getPackages(); - await cleanRegistry(registryName, packages); - await cleanGit(packages); - await cleanTarget(packages); + try { + await cleanRegistry(registryName, packages); + } + catch (_a) { } + try { + await cleanGit(packages); + } + catch (_b) { } + try { + await cleanTarget(packages); + } + catch (_c) { } core.info(`Saving paths:\n ${savePaths.join("\n ")}`); core.info(`Using key "${key}".`); try { @@ -54757,16 +54851,6 @@ async function getRegistryName() { const first = files.shift(); return external_path_default().basename(external_path_default().dirname(first)); } -async function getPackages() { - const cwd = process.cwd(); - const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"])); - return meta.packages - .filter((p) => !p.manifest_path.startsWith(cwd)) - .map((p) => { - const targets = p.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); - return { name: p.name, version: p.version, targets, path: external_path_default().dirname(p.manifest_path) }; - }); -} async function cleanRegistry(registryName, packages) { var e_1, _a; await io.rmRF(external_path_default().join(paths.index, registryName, ".cache")); @@ -54865,79 +54949,6 @@ async function cleanGit(packages) { finally { if (e_3) throw e_3.error; } } } -async function cleanTarget(packages) { - var e_5, _a; - await external_fs_default().promises.unlink("./target/.rustc_info.json"); - await io.rmRF("./target/debug/examples"); - await io.rmRF("./target/debug/incremental"); - let dir; - // remove all *files* from debug - dir = await external_fs_default().promises.opendir("./target/debug"); - try { - for (var dir_4 = save_asyncValues(dir), dir_4_1; dir_4_1 = await dir_4.next(), !dir_4_1.done;) { - const dirent = dir_4_1.value; - if (dirent.isFile()) { - await rm(dir.path, dirent); - } - } - } - catch (e_5_1) { e_5 = { error: e_5_1 }; } - finally { - try { - if (dir_4_1 && !dir_4_1.done && (_a = dir_4.return)) await _a.call(dir_4); - } - finally { if (e_5) throw e_5.error; } - } - const keepPkg = new Set(packages.map((p) => p.name)); - await rmExcept("./target/debug/build", keepPkg); - await rmExcept("./target/debug/.fingerprint", keepPkg); - const keepDeps = new Set(packages.flatMap((p) => { - const names = []; - for (const n of [p.name, ...p.targets]) { - const name = n.replace(/-/g, "_"); - names.push(name, `lib${name}`); - } - return names; - })); - await rmExcept("./target/debug/deps", keepDeps); -} -const oneWeek = 7 * 24 * 3600 * 1000; -async function rmExcept(dirName, keepPrefix) { - var e_6, _a; - const dir = await external_fs_default().promises.opendir(dirName); - try { - for (var dir_5 = save_asyncValues(dir), dir_5_1; dir_5_1 = await dir_5.next(), !dir_5_1.done;) { - const dirent = dir_5_1.value; - let name = dirent.name; - const idx = name.lastIndexOf("-"); - if (idx !== -1) { - name = name.slice(0, idx); - } - const fileName = external_path_default().join(dir.path, dirent.name); - const { mtime } = await external_fs_default().promises.stat(fileName); - if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) { - await rm(dir.path, dirent); - } - } - } - catch (e_6_1) { e_6 = { error: e_6_1 }; } - finally { - try { - if (dir_5_1 && !dir_5_1.done && (_a = dir_5.return)) await _a.call(dir_5); - } - finally { if (e_6) throw e_6.error; } - } -} -async function rm(parent, dirent) { - const fileName = external_path_default().join(parent, dirent.name); - core.debug(`deleting "${fileName}"`); - if (dirent.isFile()) { - await external_fs_default().promises.unlink(fileName); - } - else if (dirent.isDirectory()) { - await io.rmRF(fileName); - } -} async function macOsWorkaround() { try { // Workaround for https://github.com/actions/cache/issues/403 diff --git a/src/common.ts b/src/common.ts index 9db6d2e..5e7cf4c 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,6 +1,7 @@ import * as core from "@actions/core"; import * as exec from "@actions/exec"; import * as glob from "@actions/glob"; +import * as io from "@actions/io"; import crypto from "crypto"; import fs from "fs"; import os from "os"; @@ -109,3 +110,93 @@ async function getLockfileHash(): Promise { } return hasher.digest("hex").slice(0, 20); } + +export interface PackageDefinition { + name: string; + version: string; + path: string; + targets: Array; +} + +export type Packages = Array; + +interface Meta { + packages: Array<{ + name: string; + version: string; + manifest_path: string; + targets: Array<{ kind: Array; name: string }>; + }>; +} + +export async function getPackages(): Promise { + const cwd = process.cwd(); + const meta: Meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"])); + + return meta.packages + .filter((p) => !p.manifest_path.startsWith(cwd)) + .map((p) => { + const targets = p.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); + return { name: p.name, version: p.version, targets, path: path.dirname(p.manifest_path) }; + }); +} + +export async function cleanTarget(packages: Packages) { + await fs.promises.unlink("./target/.rustc_info.json"); + await io.rmRF("./target/debug/examples"); + await io.rmRF("./target/debug/incremental"); + + let dir: fs.Dir; + // remove all *files* from debug + dir = await fs.promises.opendir("./target/debug"); + for await (const dirent of dir) { + if (dirent.isFile()) { + await rm(dir.path, dirent); + } + } + + const keepPkg = new Set(packages.map((p) => p.name)); + await rmExcept("./target/debug/build", keepPkg); + await rmExcept("./target/debug/.fingerprint", keepPkg); + + const keepDeps = new Set( + packages.flatMap((p) => { + const names = []; + for (const n of [p.name, ...p.targets]) { + const name = n.replace(/-/g, "_"); + names.push(name, `lib${name}`); + } + return names; + }), + ); + await rmExcept("./target/debug/deps", keepDeps); +} + +const oneWeek = 7 * 24 * 3600 * 1000; + +export async function rmExcept(dirName: string, keepPrefix: Set) { + const dir = await fs.promises.opendir(dirName); + for await (const dirent of dir) { + let name = dirent.name; + const idx = name.lastIndexOf("-"); + if (idx !== -1) { + name = name.slice(0, idx); + } + const fileName = path.join(dir.path, dirent.name); + const { mtime } = await fs.promises.stat(fileName); + // we don’t really know + if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) { + await rm(dir.path, dirent); + } + } +} + +export async function rm(parent: string, dirent: fs.Dirent) { + const fileName = path.join(parent, dirent.name); + core.debug(`deleting "${fileName}"`); + if (dirent.isFile()) { + await fs.promises.unlink(fileName); + } else if (dirent.isDirectory()) { + await io.rmRF(fileName); + } +} diff --git a/src/restore.ts b/src/restore.ts index 4eb5fba..dc46829 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -1,6 +1,6 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; -import { getCacheConfig, isValidEvent, stateKey } from "./common"; +import { cleanTarget, getCacheConfig, getPackages, isValidEvent, stateKey } from "./common"; async function run() { if (!isValidEvent()) { @@ -23,6 +23,13 @@ async function run() { } else { core.info("No cache found."); } + + if (restoreKey !== key) { + // pre-clean the target directory on cache mismatch + const packages = await getPackages(); + + await cleanTarget(packages); + } } catch (e) { core.info(`[warning] ${e.message}`); } diff --git a/src/save.ts b/src/save.ts index fc67473..0d1968c 100644 --- a/src/save.ts +++ b/src/save.ts @@ -5,7 +5,7 @@ import * as glob from "@actions/glob"; import * as io from "@actions/io"; import fs from "fs"; import path from "path"; -import { getCacheConfig, getCmdOutput, isValidEvent, paths, stateKey } from "./common"; +import { cleanTarget, getCacheConfig, getPackages, isValidEvent, Packages, paths, rm, stateKey } from "./common"; async function run() { if (!isValidEvent()) { @@ -27,11 +27,17 @@ async function run() { const registryName = await getRegistryName(); const packages = await getPackages(); - await cleanRegistry(registryName, packages); + try { + await cleanRegistry(registryName, packages); + } catch {} - await cleanGit(packages); + try { + await cleanGit(packages); + } catch {} - await cleanTarget(packages); + try { + await cleanTarget(packages); + } catch {} core.info(`Saving paths:\n ${savePaths.join("\n ")}`); core.info(`Using key "${key}".`); @@ -52,24 +58,6 @@ async function run() { run(); -interface PackageDefinition { - name: string; - version: string; - path: string; - targets: Array; -} - -type Packages = Array; - -interface Meta { - packages: Array<{ - name: string; - version: string; - manifest_path: string; - targets: Array<{ kind: Array; name: string }>; - }>; -} - async function getRegistryName(): Promise { const globber = await glob.create(`${paths.index}/**/.last-updated`, { followSymbolicLinks: false }); const files = await globber.glob(); @@ -81,18 +69,6 @@ async function getRegistryName(): Promise { return path.basename(path.dirname(first)); } -async function getPackages(): Promise { - const cwd = process.cwd(); - const meta: Meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"])); - - return meta.packages - .filter((p) => !p.manifest_path.startsWith(cwd)) - .map((p) => { - const targets = p.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); - return { name: p.name, version: p.version, targets, path: path.dirname(p.manifest_path) }; - }); -} - async function cleanRegistry(registryName: string, packages: Packages) { await io.rmRF(path.join(paths.index, registryName, ".cache")); @@ -155,65 +131,6 @@ async function cleanGit(packages: Packages) { } } -async function cleanTarget(packages: Packages) { - await fs.promises.unlink("./target/.rustc_info.json"); - await io.rmRF("./target/debug/examples"); - await io.rmRF("./target/debug/incremental"); - - let dir: fs.Dir; - // remove all *files* from debug - dir = await fs.promises.opendir("./target/debug"); - for await (const dirent of dir) { - if (dirent.isFile()) { - await rm(dir.path, dirent); - } - } - - const keepPkg = new Set(packages.map((p) => p.name)); - await rmExcept("./target/debug/build", keepPkg); - await rmExcept("./target/debug/.fingerprint", keepPkg); - - const keepDeps = new Set( - packages.flatMap((p) => { - const names = []; - for (const n of [p.name, ...p.targets]) { - const name = n.replace(/-/g, "_"); - names.push(name, `lib${name}`); - } - return names; - }), - ); - await rmExcept("./target/debug/deps", keepDeps); -} - -const oneWeek = 7 * 24 * 3600 * 1000; - -async function rmExcept(dirName: string, keepPrefix: Set) { - const dir = await fs.promises.opendir(dirName); - for await (const dirent of dir) { - let name = dirent.name; - const idx = name.lastIndexOf("-"); - if (idx !== -1) { - name = name.slice(0, idx); - } - const fileName = path.join(dir.path, dirent.name); - const { mtime } = await fs.promises.stat(fileName); - if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > oneWeek) { - await rm(dir.path, dirent); - } - } -} - -async function rm(parent: string, dirent: fs.Dirent) { - const fileName = path.join(parent, dirent.name); - core.debug(`deleting "${fileName}"`); - if (dirent.isFile()) { - await fs.promises.unlink(fileName); - } else if (dirent.isDirectory()) { - await io.rmRF(fileName); - } -} - async function macOsWorkaround() { try { // Workaround for https://github.com/actions/cache/issues/403