diff --git a/lib/analyze-action.js b/lib/analyze-action.js index 99e2697eb..303895508 100644 --- a/lib/analyze-action.js +++ b/lib/analyze-action.js @@ -91057,51 +91057,52 @@ var CODEQL_DEPENDENCY_CACHE_VERSION = 1; function getJavaTempDependencyDir() { return (0, import_path.join)(getTemporaryDirectory(), "codeql_java", "repository"); } -function getDefaultCacheConfig() { - return { - java: { - paths: [ - // Maven - (0, import_path.join)(os3.homedir(), ".m2", "repository"), - // Gradle - (0, import_path.join)(os3.homedir(), ".gradle", "caches"), - // CodeQL Java build-mode: none - getJavaTempDependencyDir() - ], - hash: [ - // Maven - "**/pom.xml", - // Gradle - "**/*.gradle*", - "**/gradle-wrapper.properties", - "buildSrc/**/Versions.kt", - "buildSrc/**/Dependencies.kt", - "gradle/*.versions.toml", - "**/versions.properties" - ] - }, - csharp: { - paths: [(0, import_path.join)(os3.homedir(), ".nuget", "packages")], - hash: [ - // NuGet - "**/packages.lock.json", - // Paket - "**/paket.lock" - ] - }, - go: { - paths: [(0, import_path.join)(os3.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"] - } - }; +function getJavaDependencyDirs() { + return [ + // Maven + (0, import_path.join)(os3.homedir(), ".m2", "repository"), + // Gradle + (0, import_path.join)(os3.homedir(), ".gradle", "caches"), + // CodeQL Java build-mode: none + getJavaTempDependencyDir() + ]; } +var defaultCacheConfigs = { + java: { + getDependencyPaths: getJavaDependencyDirs, + hash: [ + // Maven + "**/pom.xml", + // Gradle + "**/*.gradle*", + "**/gradle-wrapper.properties", + "buildSrc/**/Versions.kt", + "buildSrc/**/Dependencies.kt", + "gradle/*.versions.toml", + "**/versions.properties" + ] + }, + csharp: { + getDependencyPaths: () => [(0, import_path.join)(os3.homedir(), ".nuget", "packages")], + hash: [ + // NuGet + "**/packages.lock.json", + // Paket + "**/paket.lock" + ] + }, + go: { + getDependencyPaths: () => [(0, import_path.join)(os3.homedir(), "go", "pkg", "mod")], + hash: ["**/go.sum"] + } +}; async function makeGlobber(patterns) { return glob.create(patterns.join("\n")); } async function uploadDependencyCaches(codeql, features, config, logger) { const status = []; for (const language of config.languages) { - const cacheConfig = getDefaultCacheConfig()[language]; + const cacheConfig = defaultCacheConfigs[language]; if (cacheConfig === void 0) { logger.info( `Skipping upload of dependency cache for ${language} as we have no caching configuration for it.` @@ -91116,7 +91117,11 @@ async function uploadDependencyCaches(codeql, features, config, logger) { ); continue; } - const size = await getTotalCacheSize(cacheConfig.paths, logger, true); + const size = await getTotalCacheSize( + cacheConfig.getDependencyPaths(), + logger, + true + ); if (size === 0) { status.push({ language, result: "empty" /* Empty */ }); logger.info( @@ -91130,7 +91135,7 @@ async function uploadDependencyCaches(codeql, features, config, logger) { ); try { const start = performance.now(); - await actionsCache3.saveCache(cacheConfig.paths, key); + await actionsCache3.saveCache(cacheConfig.getDependencyPaths(), key); const upload_duration_ms = Math.round(performance.now() - start); status.push({ language, diff --git a/lib/init-action.js b/lib/init-action.js index 506360660..52b059d3e 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -87244,51 +87244,52 @@ var CODEQL_DEPENDENCY_CACHE_VERSION = 1; function getJavaTempDependencyDir() { return (0, import_path.join)(getTemporaryDirectory(), "codeql_java", "repository"); } -function getDefaultCacheConfig() { - return { - java: { - paths: [ - // Maven - (0, import_path.join)(os2.homedir(), ".m2", "repository"), - // Gradle - (0, import_path.join)(os2.homedir(), ".gradle", "caches"), - // CodeQL Java build-mode: none - getJavaTempDependencyDir() - ], - hash: [ - // Maven - "**/pom.xml", - // Gradle - "**/*.gradle*", - "**/gradle-wrapper.properties", - "buildSrc/**/Versions.kt", - "buildSrc/**/Dependencies.kt", - "gradle/*.versions.toml", - "**/versions.properties" - ] - }, - csharp: { - paths: [(0, import_path.join)(os2.homedir(), ".nuget", "packages")], - hash: [ - // NuGet - "**/packages.lock.json", - // Paket - "**/paket.lock" - ] - }, - go: { - paths: [(0, import_path.join)(os2.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"] - } - }; +function getJavaDependencyDirs() { + return [ + // Maven + (0, import_path.join)(os2.homedir(), ".m2", "repository"), + // Gradle + (0, import_path.join)(os2.homedir(), ".gradle", "caches"), + // CodeQL Java build-mode: none + getJavaTempDependencyDir() + ]; } +var defaultCacheConfigs = { + java: { + getDependencyPaths: getJavaDependencyDirs, + hash: [ + // Maven + "**/pom.xml", + // Gradle + "**/*.gradle*", + "**/gradle-wrapper.properties", + "buildSrc/**/Versions.kt", + "buildSrc/**/Dependencies.kt", + "gradle/*.versions.toml", + "**/versions.properties" + ] + }, + csharp: { + getDependencyPaths: () => [(0, import_path.join)(os2.homedir(), ".nuget", "packages")], + hash: [ + // NuGet + "**/packages.lock.json", + // Paket + "**/paket.lock" + ] + }, + go: { + getDependencyPaths: () => [(0, import_path.join)(os2.homedir(), "go", "pkg", "mod")], + hash: ["**/go.sum"] + } +}; async function makeGlobber(patterns) { return glob.create(patterns.join("\n")); } async function downloadDependencyCaches(codeql, features, languages, logger) { const status = []; for (const language of languages) { - const cacheConfig = getDefaultCacheConfig()[language]; + const cacheConfig = defaultCacheConfigs[language]; if (cacheConfig === void 0) { logger.info( `Skipping download of dependency cache for ${language} as we have no caching configuration for it.` @@ -87314,7 +87315,7 @@ async function downloadDependencyCaches(codeql, features, languages, logger) { ); const start = performance.now(); const hitKey = await actionsCache3.restoreCache( - cacheConfig.paths, + cacheConfig.getDependencyPaths(), primaryKey, restoreKeys ); diff --git a/src/dependency-caching.ts b/src/dependency-caching.ts index 58eb80e41..a2205e7a7 100644 --- a/src/dependency-caching.ts +++ b/src/dependency-caching.ts @@ -19,8 +19,8 @@ import { getErrorMessage, getRequiredEnvParam } from "./util"; * Caching configuration for a particular language. */ interface CacheConfig { - /** The paths of directories on the runner that should be included in the cache. */ - paths: string[]; + /** 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 * by a project. We find all files which match these patterns, calculate a hash for @@ -41,47 +41,57 @@ export function getJavaTempDependencyDir(): string { return join(getTemporaryDirectory(), "codeql_java", "repository"); } +/** + * Returns an array of paths of directories on the runner that should be included in a dependency cache + * for a Java analysis. It is important that this is a function, because we call `getTemporaryDirectory` + * which would otherwise fail in tests if we haven't had a chance to initialise `RUNNER_TEMP`. + * + * @returns The paths of directories on the runner that should be included in a dependency cache + * for a Java analysis. + */ +export function getJavaDependencyDirs(): string[] { + return [ + // Maven + join(os.homedir(), ".m2", "repository"), + // Gradle + join(os.homedir(), ".gradle", "caches"), + // CodeQL Java build-mode: none + getJavaTempDependencyDir(), + ]; +} + /** * Default caching configurations per language. */ -function getDefaultCacheConfig(): { [language: string]: CacheConfig } { - return { - java: { - paths: [ - // Maven - join(os.homedir(), ".m2", "repository"), - // Gradle - join(os.homedir(), ".gradle", "caches"), - // CodeQL Java build-mode: none - getJavaTempDependencyDir(), - ], - hash: [ - // Maven - "**/pom.xml", - // Gradle - "**/*.gradle*", - "**/gradle-wrapper.properties", - "buildSrc/**/Versions.kt", - "buildSrc/**/Dependencies.kt", - "gradle/*.versions.toml", - "**/versions.properties", - ], - }, - csharp: { - paths: [join(os.homedir(), ".nuget", "packages")], - hash: [ - // NuGet - "**/packages.lock.json", - // Paket - "**/paket.lock", - ], - }, - go: { - paths: [join(os.homedir(), "go", "pkg", "mod")], - hash: ["**/go.sum"], - }, - }; -} +const defaultCacheConfigs: { [language: string]: CacheConfig } = { + java: { + getDependencyPaths: getJavaDependencyDirs, + hash: [ + // Maven + "**/pom.xml", + // Gradle + "**/*.gradle*", + "**/gradle-wrapper.properties", + "buildSrc/**/Versions.kt", + "buildSrc/**/Dependencies.kt", + "gradle/*.versions.toml", + "**/versions.properties", + ], + }, + csharp: { + getDependencyPaths: () => [join(os.homedir(), ".nuget", "packages")], + hash: [ + // NuGet + "**/packages.lock.json", + // Paket + "**/paket.lock", + ], + }, + go: { + getDependencyPaths: () => [join(os.homedir(), "go", "pkg", "mod")], + hash: ["**/go.sum"], + }, +}; async function makeGlobber(patterns: string[]): Promise { return glob.create(patterns.join("\n")); @@ -128,7 +138,7 @@ export async function downloadDependencyCaches( const status: DependencyCacheRestoreStatusReport = []; for (const language of languages) { - const cacheConfig = getDefaultCacheConfig()[language]; + const cacheConfig = defaultCacheConfigs[language]; if (cacheConfig === undefined) { logger.info( @@ -162,7 +172,7 @@ export async function downloadDependencyCaches( const start = performance.now(); const hitKey = await actionsCache.restoreCache( - cacheConfig.paths, + cacheConfig.getDependencyPaths(), primaryKey, restoreKeys, ); @@ -223,7 +233,7 @@ export async function uploadDependencyCaches( ): Promise { const status: DependencyCacheUploadStatusReport = []; for (const language of config.languages) { - const cacheConfig = getDefaultCacheConfig()[language]; + const cacheConfig = defaultCacheConfigs[language]; if (cacheConfig === undefined) { logger.info( @@ -254,7 +264,11 @@ export async function uploadDependencyCaches( // use the cache quota that we compete with. In that case, we do not wish to use up all of the quota // with the dependency caches. For this, we could use the Cache API to check whether other workflows // are using the quota and how full it is. - const size = await getTotalCacheSize(cacheConfig.paths, logger, true); + const size = await getTotalCacheSize( + cacheConfig.getDependencyPaths(), + logger, + true, + ); // Skip uploading an empty cache. if (size === 0) { @@ -273,7 +287,7 @@ export async function uploadDependencyCaches( try { const start = performance.now(); - await actionsCache.saveCache(cacheConfig.paths, key); + await actionsCache.saveCache(cacheConfig.getDependencyPaths(), key); const upload_duration_ms = Math.round(performance.now() - start); status.push({