diff --git a/src/config.ts b/src/config.ts index c92f872..a6b45c3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -118,19 +118,43 @@ export class CacheConfig { let lockHash = core.getState(STATE_LOCKFILE_HASH); let keyFiles: Array = JSON.parse(core.getState(STATE_LOCKFILES) || "[]"); + // Constructs the workspace config and paths to restore: + // The workspaces are given using a `$workspace -> $target` syntax. + + const workspaces: Array = []; + const workspacesInput = core.getInput("workspaces") || "."; + for (const workspace of workspacesInput.trim().split("\n")) { + let [root, target = "target"] = workspace.split("->").map((s) => s.trim()); + root = path.resolve(root); + target = path.join(root, target); + workspaces.push(new Workspace(root, target)); + } + self.workspaces = workspaces; + if (!lockHash) { - const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", { - followSymbolicLinks: false, - }); - keyFiles = await globber.glob(); + hasher = crypto.createHash("sha1"); + + async function globHash(pattern: string): Promise { + const globber = await glob.create(pattern, { + followSymbolicLinks: false, + }); + return await globber.glob(); + } + + keyFiles = keyFiles.concat(await globHash("rust-toolchain\nrust-toolchain.toml")); + for (const workspace of workspaces) { + const root = workspace.root; + keyFiles = keyFiles.concat(await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)); + } + 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); @@ -138,22 +162,10 @@ export class CacheConfig { } 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: Array = []; - const workspacesInput = core.getInput("workspaces") || "."; - for (const workspace of workspacesInput.trim().split("\n")) { - let [root, target = "target"] = workspace.split("->").map((s) => s.trim()); - root = path.resolve(root); - target = path.join(root, target); - workspaces.push(new Workspace(root, target)); - } - self.workspaces = workspaces; - self.cachePaths = [CARGO_HOME]; const cacheTargets = core.getInput("cache-targets").toLowerCase(); if (cacheTargets === "true") {