From ab1c84236a1357778c5741c52f065d31a9fc7fe9 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Wed, 17 Sep 2025 15:04:55 +0100 Subject: [PATCH] Change `hash` to be a function that can use `Features` --- lib/analyze-action.js | 15 ++++++++------- lib/init-action.js | 15 ++++++++------- src/dependency-caching.ts | 24 +++++++++++++----------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/analyze-action.js b/lib/analyze-action.js index 303895508..e6770f089 100644 --- a/lib/analyze-action.js +++ b/lib/analyze-action.js @@ -91070,7 +91070,7 @@ function getJavaDependencyDirs() { var defaultCacheConfigs = { java: { getDependencyPaths: getJavaDependencyDirs, - hash: [ + getHashPatterns: async () => [ // Maven "**/pom.xml", // Gradle @@ -91084,7 +91084,7 @@ var defaultCacheConfigs = { }, csharp: { getDependencyPaths: () => [(0, import_path.join)(os3.homedir(), ".nuget", "packages")], - hash: [ + getHashPatterns: async () => [ // NuGet "**/packages.lock.json", // Paket @@ -91093,7 +91093,7 @@ var defaultCacheConfigs = { }, go: { getDependencyPaths: () => [(0, import_path.join)(os3.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"] + getHashPatterns: async () => ["**/go.sum"] } }; async function makeGlobber(patterns) { @@ -91109,7 +91109,8 @@ async function uploadDependencyCaches(codeql, features, config, logger) { ); continue; } - const globber = await makeGlobber(cacheConfig.hash); + const patterns = await cacheConfig.getHashPatterns(codeql, features); + const globber = await makeGlobber(patterns); if ((await globber.glob()).length === 0) { status.push({ language, result: "no-hash" /* NoHash */ }); logger.info( @@ -91129,7 +91130,7 @@ async function uploadDependencyCaches(codeql, features, config, logger) { ); continue; } - const key = await cacheKey2(codeql, features, language, cacheConfig); + const key = await cacheKey2(codeql, features, language, patterns); logger.info( `Uploading cache of size ${size} for ${language} with key ${key}...` ); @@ -91157,8 +91158,8 @@ async function uploadDependencyCaches(codeql, features, config, logger) { } return status; } -async function cacheKey2(codeql, features, language, cacheConfig) { - const hash2 = await glob.hashFiles(cacheConfig.hash.join("\n")); +async function cacheKey2(codeql, features, language, patterns) { + const hash2 = await glob.hashFiles(patterns.join("\n")); return `${await cachePrefix2(codeql, features, language)}${hash2}`; } async function cachePrefix2(codeql, features, language) { diff --git a/lib/init-action.js b/lib/init-action.js index 52b059d3e..648ffd01f 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -87257,7 +87257,7 @@ function getJavaDependencyDirs() { var defaultCacheConfigs = { java: { getDependencyPaths: getJavaDependencyDirs, - hash: [ + getHashPatterns: async () => [ // Maven "**/pom.xml", // Gradle @@ -87271,7 +87271,7 @@ var defaultCacheConfigs = { }, csharp: { getDependencyPaths: () => [(0, import_path.join)(os2.homedir(), ".nuget", "packages")], - hash: [ + getHashPatterns: async () => [ // NuGet "**/packages.lock.json", // Paket @@ -87280,7 +87280,7 @@ var defaultCacheConfigs = { }, go: { getDependencyPaths: () => [(0, import_path.join)(os2.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"] + getHashPatterns: async () => ["**/go.sum"] } }; async function makeGlobber(patterns) { @@ -87296,7 +87296,8 @@ async function downloadDependencyCaches(codeql, features, languages, logger) { ); continue; } - const globber = await makeGlobber(cacheConfig.hash); + const patterns = await cacheConfig.getHashPatterns(codeql, features); + const globber = await makeGlobber(patterns); if ((await globber.glob()).length === 0) { status.push({ language, hit_kind: "no-hash" /* NoHash */ }); logger.info( @@ -87304,7 +87305,7 @@ async function downloadDependencyCaches(codeql, features, languages, logger) { ); continue; } - const primaryKey = await cacheKey2(codeql, features, language, cacheConfig); + const primaryKey = await cacheKey2(codeql, features, language, patterns); const restoreKeys = [ await cachePrefix2(codeql, features, language) ]; @@ -87331,8 +87332,8 @@ async function downloadDependencyCaches(codeql, features, languages, logger) { } return status; } -async function cacheKey2(codeql, features, language, cacheConfig) { - const hash = await glob.hashFiles(cacheConfig.hash.join("\n")); +async function cacheKey2(codeql, features, language, patterns) { + const hash = await glob.hashFiles(patterns.join("\n")); return `${await cachePrefix2(codeql, features, language)}${hash}`; } async function cachePrefix2(codeql, features, language) { diff --git a/src/dependency-caching.ts b/src/dependency-caching.ts index a2205e7a7..1aef3f088 100644 --- a/src/dependency-caching.ts +++ b/src/dependency-caching.ts @@ -22,11 +22,11 @@ interface CacheConfig { /** Gets the paths of directories on the runner that should be included in the cache. */ getDependencyPaths: () => string[]; /** - * Patterns for the paths of files whose contents affect which dependencies are used + * Gets patterns for the paths of files whose contents affect which dependencies are used * by a project. We find all files which match these patterns, calculate a hash for * their contents, and use that hash as part of the cache key. */ - hash: string[]; + getHashPatterns: (codeql: CodeQL, features: Features) => Promise; } const CODEQL_DEPENDENCY_CACHE_PREFIX = "codeql-dependencies"; @@ -66,7 +66,7 @@ export function getJavaDependencyDirs(): string[] { const defaultCacheConfigs: { [language: string]: CacheConfig } = { java: { getDependencyPaths: getJavaDependencyDirs, - hash: [ + getHashPatterns: async () => [ // Maven "**/pom.xml", // Gradle @@ -80,7 +80,7 @@ const defaultCacheConfigs: { [language: string]: CacheConfig } = { }, csharp: { getDependencyPaths: () => [join(os.homedir(), ".nuget", "packages")], - hash: [ + getHashPatterns: async () => [ // NuGet "**/packages.lock.json", // Paket @@ -89,7 +89,7 @@ const defaultCacheConfigs: { [language: string]: CacheConfig } = { }, go: { getDependencyPaths: () => [join(os.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"], + getHashPatterns: async () => ["**/go.sum"], }, }; @@ -149,7 +149,8 @@ export async function downloadDependencyCaches( // Check that we can find files to calculate the hash for the cache key from, so we don't end up // with an empty string. - const globber = await makeGlobber(cacheConfig.hash); + const patterns = await cacheConfig.getHashPatterns(codeql, features); + const globber = await makeGlobber(patterns); if ((await globber.glob()).length === 0) { status.push({ language, hit_kind: CacheHitKind.NoHash }); @@ -159,7 +160,7 @@ export async function downloadDependencyCaches( continue; } - const primaryKey = await cacheKey(codeql, features, language, cacheConfig); + const primaryKey = await cacheKey(codeql, features, language, patterns); const restoreKeys: string[] = [ await cachePrefix(codeql, features, language), ]; @@ -244,7 +245,8 @@ export async function uploadDependencyCaches( // Check that we can find files to calculate the hash for the cache key from, so we don't end up // with an empty string. - const globber = await makeGlobber(cacheConfig.hash); + const patterns = await cacheConfig.getHashPatterns(codeql, features); + const globber = await makeGlobber(patterns); if ((await globber.glob()).length === 0) { status.push({ language, result: CacheStoreResult.NoHash }); @@ -279,7 +281,7 @@ export async function uploadDependencyCaches( continue; } - const key = await cacheKey(codeql, features, language, cacheConfig); + const key = await cacheKey(codeql, features, language, patterns); logger.info( `Uploading cache of size ${size} for ${language} with key ${key}...`, @@ -330,9 +332,9 @@ async function cacheKey( codeql: CodeQL, features: Features, language: Language, - cacheConfig: CacheConfig, + patterns: string[], ): Promise { - const hash = await glob.hashFiles(cacheConfig.hash.join("\n")); + const hash = await glob.hashFiles(patterns.join("\n")); return `${await cachePrefix(codeql, features, language)}${hash}`; }