@@ -26,7 +26,7 @@ async function setupBazel() {
2626
2727 await setupBazelisk ( )
2828 await restoreCache ( config . bazeliskCache )
29- await restoreCache ( config . diskCache )
29+ await restoreDiskCache ( config . diskCache )
3030 await restoreCache ( config . repositoryCache )
3131 await restoreExternalCaches ( config . externalCache )
3232
@@ -143,7 +143,7 @@ async function restoreExternalCaches(cacheConfig) {
143143 }
144144}
145145
146- async function restoreCache ( cacheConfig ) {
146+ async function restoreCacheImpl ( cacheConfig , primaryKey , restoreKeys ) {
147147 if ( ! cacheConfig . enabled ) {
148148 return
149149 }
@@ -152,16 +152,13 @@ async function restoreCache(cacheConfig) {
152152 await setTimeout ( delay , async function ( ) {
153153 core . startGroup ( `Restore cache for ${ cacheConfig . name } ` )
154154
155- const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
156155 const name = cacheConfig . name
157156 const paths = cacheConfig . paths
158- const restoreKey = `${ config . baseCacheKey } -${ name } -`
159- const key = `${ restoreKey } ${ hash } `
160157
161158 core . debug ( `Attempting to restore ${ name } cache from ${ key } ` )
162159
163160 const restoredKey = await cache . restoreCache (
164- paths , key , [ restoreKey ] ,
161+ paths , primaryKey , restoreKeys ,
165162 { segmentTimeoutInMs : 300000 } // 5 minutes
166163 )
167164
@@ -179,4 +176,22 @@ async function restoreCache(cacheConfig) {
179176 } ( ) )
180177}
181178
179+ async function restoreCache ( cacheConfig ) {
180+ const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
181+ const restoreKey = `${ config . baseCacheKey } -${ cacheConfig . name } -`
182+ const key = `${ restoreKey } ${ hash } `
183+ await restoreCacheImpl ( cacheConfig , key , [ restoreKey ] )
184+ }
185+
186+ async function restoreDiskCache ( cacheConfig ) {
187+ const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
188+
189+ // Since disk caches get updated on any change, each run has a unique key.
190+ // Therefore it can only be restored by prefix match, rather than exact key match.
191+ // When multiple prefix matches exist, the most recent is selected.
192+ const restoreKey = `${ config . baseCacheKey } -${ cacheConfig . name } -`
193+ const hashedRestoreKey = `${ restoreKey } ${ hash } -`
194+ await restoreCacheImpl ( cacheConfig , hashedRestoreKey , [ hashedRestoreKey , restoreKey ] )
195+ }
196+
182197run ( )
0 commit comments