From 535f5f8f1c91ed6b55b38ac41de2fca5ae368c32 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Thu, 11 May 2023 11:30:32 +0100 Subject: [PATCH] fix: cache key dependency on installed packages Add the installed packages to the environment element of the cache key so that CI tooling is considered. This ensures that rust CI tooling is cached correctly when changes occur. Prior to this a manual key change or cache expiry would need to occur before CI tools were correctly cached. --- README.md | 1 + dist/restore/index.js | 8 ++++++++ dist/save/index.js | 8 ++++++++ src/config.ts | 11 +++++++++++ 4 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 2a2461f..046e3f1 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ This cache is automatically keyed by: - the value of some compiler-specific environment variables (eg. RUSTFLAGS, etc), and - a hash of all `Cargo.lock` / `Cargo.toml` files found anywhere in the repository (if present). - a hash of all `rust-toolchain` / `rust-toolchain.toml` files in the root of the repository (if present). +- a hash of installed packages as generated by `cargo install --list`. An additional input `key` can be provided if the builtin keys are not sufficient. diff --git a/dist/restore/index.js b/dist/restore/index.js index 9e923e1..836487e 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -60055,6 +60055,9 @@ class CacheConfig { } } self.keyEnvs = keyEnvs; + // Installed packages and their versions are also considered for the key. + const packages = await getPackages(); + hasher.update(packages); key += `-${hasher.digest("hex")}`; self.restoreKey = key; // Construct the lockfiles portion of the key: @@ -60146,6 +60149,11 @@ async function getRustVersion() { .filter((s) => s.length === 2); return Object.fromEntries(splits); } +async function getPackages() { + let stdout = await getCmdOutput("cargo", ["install", "--list"]); + // Make OS independent. + return stdout.split(/[\n\r]+/).join("\n"); +} async function globFiles(pattern) { const globber = await glob.create(pattern, { followSymbolicLinks: false, diff --git a/dist/save/index.js b/dist/save/index.js index b1bc3cb..3f4fae3 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -60055,6 +60055,9 @@ class CacheConfig { } } self.keyEnvs = keyEnvs; + // Installed packages and their versions are also considered for the key. + const packages = await getPackages(); + hasher.update(packages); key += `-${hasher.digest("hex")}`; self.restoreKey = key; // Construct the lockfiles portion of the key: @@ -60146,6 +60149,11 @@ async function getRustVersion() { .filter((s) => s.length === 2); return Object.fromEntries(splits); } +async function getPackages() { + let stdout = await getCmdOutput("cargo", ["install", "--list"]); + // Make OS independent. + return stdout.split(/[\n\r]+/).join("\n"); +} async function globFiles(pattern) { const globber = await glob.create(pattern, { followSymbolicLinks: false, diff --git a/src/config.ts b/src/config.ts index 4164037..962028f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -103,6 +103,11 @@ export class CacheConfig { } self.keyEnvs = keyEnvs; + + // Installed packages and their versions are also considered for the key. + const packages = await getPackages(); + hasher.update(packages); + key += `-${hasher.digest("hex")}`; self.restoreKey = key; @@ -220,6 +225,12 @@ async function getRustVersion(): Promise { return Object.fromEntries(splits); } +async function getPackages(): Promise { + let stdout = await getCmdOutput("cargo", ["install", "--list"]); + // Make OS independent. + return stdout.split(/[\n\r]+/).join("\n"); +} + async function globFiles(pattern: string): Promise { const globber = await glob.create(pattern, { followSymbolicLinks: false,