fix: cache key dependency on installed packages (#138)

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.
This commit is contained in:
Steven Hartland 2023-05-11 22:12:12 +01:00 committed by GitHub
parent 5e9fae966f
commit 827c240e23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -95,6 +95,7 @@ This cache is automatically keyed by:
- the value of some compiler-specific environment variables (eg. RUSTFLAGS, etc), and - 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 `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 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. An additional input `key` can be provided if the builtin keys are not sufficient.

View File

@ -60055,6 +60055,9 @@ class CacheConfig {
} }
} }
self.keyEnvs = keyEnvs; 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")}`; key += `-${hasher.digest("hex")}`;
self.restoreKey = key; self.restoreKey = key;
// Construct the lockfiles portion of the key: // Construct the lockfiles portion of the key:
@ -60146,6 +60149,11 @@ async function getRustVersion() {
.filter((s) => s.length === 2); .filter((s) => s.length === 2);
return Object.fromEntries(splits); 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) { async function globFiles(pattern) {
const globber = await glob.create(pattern, { const globber = await glob.create(pattern, {
followSymbolicLinks: false, followSymbolicLinks: false,

8
dist/save/index.js vendored
View File

@ -60055,6 +60055,9 @@ class CacheConfig {
} }
} }
self.keyEnvs = keyEnvs; 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")}`; key += `-${hasher.digest("hex")}`;
self.restoreKey = key; self.restoreKey = key;
// Construct the lockfiles portion of the key: // Construct the lockfiles portion of the key:
@ -60146,6 +60149,11 @@ async function getRustVersion() {
.filter((s) => s.length === 2); .filter((s) => s.length === 2);
return Object.fromEntries(splits); 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) { async function globFiles(pattern) {
const globber = await glob.create(pattern, { const globber = await glob.create(pattern, {
followSymbolicLinks: false, followSymbolicLinks: false,

View File

@ -103,6 +103,11 @@ export class CacheConfig {
} }
self.keyEnvs = keyEnvs; 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")}`; key += `-${hasher.digest("hex")}`;
self.restoreKey = key; self.restoreKey = key;
@ -220,6 +225,12 @@ async function getRustVersion(): Promise<RustVersion> {
return Object.fromEntries(splits); return Object.fromEntries(splits);
} }
async function getPackages(): Promise<string> {
let stdout = await getCmdOutput("cargo", ["install", "--list"]);
// Make OS independent.
return stdout.split(/[\n\r]+/).join("\n");
}
async function globFiles(pattern: string): Promise<string[]> { async function globFiles(pattern: string): Promise<string[]> {
const globber = await glob.create(pattern, { const globber = await glob.create(pattern, {
followSymbolicLinks: false, followSymbolicLinks: false,