@@ -19,8 +19,8 @@ import { getErrorMessage, getRequiredEnvParam } from "./util";
1919 * Caching configuration for a particular language.
2020 */
2121interface CacheConfig {
22- /** The paths of directories on the runner that should be included in the cache. */
23- paths : string [ ] ;
22+ /** Gets the paths of directories on the runner that should be included in the cache. */
23+ getDependencyPaths : ( ) => string [ ] ;
2424 /**
2525 * Patterns for the paths of files whose contents affect which dependencies are used
2626 * by a project. We find all files which match these patterns, calculate a hash for
@@ -42,47 +42,57 @@ export function getJavaTempDependencyDir(): string {
4242}
4343
4444/**
45- * Default caching configurations per language.
45+ * Returns an array of paths of directories on the runner that should be included in a dependency cache
46+ * for a Java analysis. It is important that this is a function, because we call `getTemporaryDirectory`
47+ * which would otherwise fail in tests if we haven't had a chance to initialise `RUNNER_TEMP`.
48+ *
49+ * @returns The paths of directories on the runner that should be included in a dependency cache
50+ * for a Java analysis.
4651 */
47- function getDefaultCacheConfig ( ) : { [ language : string ] : CacheConfig } {
48- return {
49- java : {
50- paths : [
51- // Maven
52- join ( os . homedir ( ) , ".m2" , "repository" ) ,
53- // Gradle
54- join ( os . homedir ( ) , ".gradle" , "caches" ) ,
55- // CodeQL Java build-mode: none
56- getJavaTempDependencyDir ( ) ,
57- ] ,
58- hash : [
59- // Maven
60- "**/pom.xml" ,
61- // Gradle
62- "**/*.gradle*" ,
63- "**/gradle-wrapper.properties" ,
64- "buildSrc/**/Versions.kt" ,
65- "buildSrc/**/Dependencies.kt" ,
66- "gradle/*.versions.toml" ,
67- "**/versions.properties" ,
68- ] ,
69- } ,
70- csharp : {
71- paths : [ join ( os . homedir ( ) , ".nuget" , "packages" ) ] ,
72- hash : [
73- // NuGet
74- "**/packages.lock.json" ,
75- // Paket
76- "**/paket.lock" ,
77- ] ,
78- } ,
79- go : {
80- paths : [ join ( os . homedir ( ) , "go" , "pkg" , "mod" ) ] ,
81- hash : [ "**/go.sum" ] ,
82- } ,
83- } ;
52+ export function getJavaDependencyDirs ( ) : string [ ] {
53+ return [
54+ // Maven
55+ join ( os . homedir ( ) , ".m2" , "repository" ) ,
56+ // Gradle
57+ join ( os . homedir ( ) , ".gradle" , "caches" ) ,
58+ // CodeQL Java build-mode: none
59+ getJavaTempDependencyDir ( ) ,
60+ ] ;
8461}
8562
63+ /**
64+ * Default caching configurations per language.
65+ */
66+ const defaultCacheConfigs : { [ language : string ] : CacheConfig } = {
67+ java : {
68+ getDependencyPaths : getJavaDependencyDirs ,
69+ hash : [
70+ // Maven
71+ "**/pom.xml" ,
72+ // Gradle
73+ "**/*.gradle*" ,
74+ "**/gradle-wrapper.properties" ,
75+ "buildSrc/**/Versions.kt" ,
76+ "buildSrc/**/Dependencies.kt" ,
77+ "gradle/*.versions.toml" ,
78+ "**/versions.properties" ,
79+ ] ,
80+ } ,
81+ csharp : {
82+ getDependencyPaths : ( ) => [ join ( os . homedir ( ) , ".nuget" , "packages" ) ] ,
83+ hash : [
84+ // NuGet
85+ "**/packages.lock.json" ,
86+ // Paket
87+ "**/paket.lock" ,
88+ ] ,
89+ } ,
90+ go : {
91+ getDependencyPaths : ( ) => [ join ( os . homedir ( ) , "go" , "pkg" , "mod" ) ] ,
92+ hash : [ "**/go.sum" ] ,
93+ } ,
94+ } ;
95+
8696async function makeGlobber ( patterns : string [ ] ) : Promise < glob . Globber > {
8797 return glob . create ( patterns . join ( "\n" ) ) ;
8898}
@@ -128,7 +138,7 @@ export async function downloadDependencyCaches(
128138 const status : DependencyCacheRestoreStatusReport = [ ] ;
129139
130140 for ( const language of languages ) {
131- const cacheConfig = getDefaultCacheConfig ( ) [ language ] ;
141+ const cacheConfig = defaultCacheConfigs [ language ] ;
132142
133143 if ( cacheConfig === undefined ) {
134144 logger . info (
@@ -162,7 +172,7 @@ export async function downloadDependencyCaches(
162172
163173 const start = performance . now ( ) ;
164174 const hitKey = await actionsCache . restoreCache (
165- cacheConfig . paths ,
175+ cacheConfig . getDependencyPaths ( ) ,
166176 primaryKey ,
167177 restoreKeys ,
168178 ) ;
@@ -223,7 +233,7 @@ export async function uploadDependencyCaches(
223233) : Promise < DependencyCacheUploadStatusReport > {
224234 const status : DependencyCacheUploadStatusReport = [ ] ;
225235 for ( const language of config . languages ) {
226- const cacheConfig = getDefaultCacheConfig ( ) [ language ] ;
236+ const cacheConfig = defaultCacheConfigs [ language ] ;
227237
228238 if ( cacheConfig === undefined ) {
229239 logger . info (
@@ -254,7 +264,11 @@ export async function uploadDependencyCaches(
254264 // use the cache quota that we compete with. In that case, we do not wish to use up all of the quota
255265 // with the dependency caches. For this, we could use the Cache API to check whether other workflows
256266 // are using the quota and how full it is.
257- const size = await getTotalCacheSize ( cacheConfig . paths , logger , true ) ;
267+ const size = await getTotalCacheSize (
268+ cacheConfig . getDependencyPaths ( ) ,
269+ logger ,
270+ true ,
271+ ) ;
258272
259273 // Skip uploading an empty cache.
260274 if ( size === 0 ) {
@@ -273,7 +287,7 @@ export async function uploadDependencyCaches(
273287
274288 try {
275289 const start = performance . now ( ) ;
276- await actionsCache . saveCache ( cacheConfig . paths , key ) ;
290+ await actionsCache . saveCache ( cacheConfig . getDependencyPaths ( ) , key ) ;
277291 const upload_duration_ms = Math . round ( performance . now ( ) - start ) ;
278292
279293 status . push ( {
0 commit comments