From 88f224fae6df2da3a6f80709bd37f7d97e0543c5 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 5 Nov 2022 11:34:55 -0300 Subject: [PATCH] only hash Cargo.toml/Cargo.lock that belong to a configured workspace --- dist/restore/index.js | 45 ++++++++++++++++++++--------------- dist/save/index.js | 45 ++++++++++++++++++++--------------- src/config.ts | 55 +++++++++++++++++++++++++------------------ 3 files changed, 84 insertions(+), 61 deletions(-) diff --git a/dist/restore/index.js b/dist/restore/index.js index 2e43c55..4c18433 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -64603,25 +64603,6 @@ class CacheConfig { // might create/overwrite lockfiles. let lockHash = lib_core.getState(STATE_LOCKFILE_HASH); let keyFiles = JSON.parse(lib_core.getState(STATE_LOCKFILES) || "[]"); - if (!lockHash) { - const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", { - followSymbolicLinks: false, - }); - keyFiles = await globber.glob(); - keyFiles.sort((a, b) => a.localeCompare(b)); - hasher = external_crypto_default().createHash("sha1"); - for (const file of keyFiles) { - for await (const chunk of external_fs_default().createReadStream(file)) { - hasher.update(chunk); - } - } - lockHash = hasher.digest("hex"); - lib_core.saveState(STATE_LOCKFILE_HASH, lockHash); - lib_core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); - } - self.keyFiles = keyFiles; - key += `-${lockHash}`; - self.cacheKey = key; // Constructs the workspace config and paths to restore: // The workspaces are given using a `$workspace -> $target` syntax. const workspaces = []; @@ -64633,6 +64614,32 @@ class CacheConfig { workspaces.push(new Workspace(root, target)); } self.workspaces = workspaces; + if (!lockHash) { + hasher = external_crypto_default().createHash("sha1"); + async function globHash(pattern) { + const globber = await glob.create(pattern, { + followSymbolicLinks: false, + }); + keyFiles = keyFiles.concat(await globber.glob()); + keyFiles.sort((a, b) => a.localeCompare(b)); + for (const file of keyFiles) { + for await (const chunk of external_fs_default().createReadStream(file)) { + hasher.update(chunk); + } + } + } + await globHash("rust-toolchain\nrust-toolchain.toml"); + for (const workspace of workspaces) { + const root = workspace.root; + await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock`); + } + lockHash = hasher.digest("hex"); + lib_core.saveState(STATE_LOCKFILE_HASH, lockHash); + lib_core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); + } + self.keyFiles = keyFiles; + key += `-${lockHash}`; + self.cacheKey = key; self.cachePaths = [config_CARGO_HOME]; const cacheTargets = lib_core.getInput("cache-targets").toLowerCase(); if (cacheTargets === "true") { diff --git a/dist/save/index.js b/dist/save/index.js index 1177027..941a089 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -64603,25 +64603,6 @@ class CacheConfig { // might create/overwrite lockfiles. let lockHash = core.getState(STATE_LOCKFILE_HASH); let keyFiles = JSON.parse(core.getState(STATE_LOCKFILES) || "[]"); - if (!lockHash) { - const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", { - followSymbolicLinks: false, - }); - keyFiles = await globber.glob(); - keyFiles.sort((a, b) => a.localeCompare(b)); - hasher = external_crypto_default().createHash("sha1"); - for (const file of keyFiles) { - for await (const chunk of external_fs_default().createReadStream(file)) { - hasher.update(chunk); - } - } - lockHash = hasher.digest("hex"); - core.saveState(STATE_LOCKFILE_HASH, lockHash); - core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); - } - self.keyFiles = keyFiles; - key += `-${lockHash}`; - self.cacheKey = key; // Constructs the workspace config and paths to restore: // The workspaces are given using a `$workspace -> $target` syntax. const workspaces = []; @@ -64633,6 +64614,32 @@ class CacheConfig { workspaces.push(new Workspace(root, target)); } self.workspaces = workspaces; + if (!lockHash) { + hasher = external_crypto_default().createHash("sha1"); + async function globHash(pattern) { + const globber = await glob.create(pattern, { + followSymbolicLinks: false, + }); + keyFiles = keyFiles.concat(await globber.glob()); + keyFiles.sort((a, b) => a.localeCompare(b)); + for (const file of keyFiles) { + for await (const chunk of external_fs_default().createReadStream(file)) { + hasher.update(chunk); + } + } + } + await globHash("rust-toolchain\nrust-toolchain.toml"); + for (const workspace of workspaces) { + const root = workspace.root; + await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock`); + } + lockHash = hasher.digest("hex"); + core.saveState(STATE_LOCKFILE_HASH, lockHash); + core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); + } + self.keyFiles = keyFiles; + key += `-${lockHash}`; + self.cacheKey = key; self.cachePaths = [CARGO_HOME]; const cacheTargets = core.getInput("cache-targets").toLowerCase(); if (cacheTargets === "true") { diff --git a/src/config.ts b/src/config.ts index c92f872..4535b74 100644 --- a/src/config.ts +++ b/src/config.ts @@ -118,29 +118,6 @@ export class CacheConfig { let lockHash = core.getState(STATE_LOCKFILE_HASH); let keyFiles: Array = JSON.parse(core.getState(STATE_LOCKFILES) || "[]"); - if (!lockHash) { - const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", { - followSymbolicLinks: false, - }); - keyFiles = await globber.glob(); - keyFiles.sort((a, b) => a.localeCompare(b)); - - hasher = crypto.createHash("sha1"); - for (const file of keyFiles) { - for await (const chunk of fs.createReadStream(file)) { - hasher.update(chunk); - } - } - lockHash = hasher.digest("hex"); - - core.saveState(STATE_LOCKFILE_HASH, lockHash); - core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); - } - - self.keyFiles = keyFiles; - key += `-${lockHash}`; - self.cacheKey = key; - // Constructs the workspace config and paths to restore: // The workspaces are given using a `$workspace -> $target` syntax. @@ -154,6 +131,38 @@ export class CacheConfig { } self.workspaces = workspaces; + if (!lockHash) { + hasher = crypto.createHash("sha1"); + + async function globHash(pattern: string): Promise { + const globber = await glob.create(pattern, { + followSymbolicLinks: false, + }); + keyFiles = keyFiles.concat(await globber.glob()); + keyFiles.sort((a, b) => a.localeCompare(b)); + + for (const file of keyFiles) { + for await (const chunk of fs.createReadStream(file)) { + hasher.update(chunk); + } + } + } + + await globHash("rust-toolchain\nrust-toolchain.toml"); + for (const workspace of workspaces) { + const root = workspace.root; + await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock`); + } + lockHash = hasher.digest("hex"); + + core.saveState(STATE_LOCKFILE_HASH, lockHash); + core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles)); + } + + self.keyFiles = keyFiles; + key += `-${lockHash}`; + self.cacheKey = key; + self.cachePaths = [CARGO_HOME]; const cacheTargets = core.getInput("cache-targets").toLowerCase(); if (cacheTargets === "true") {