Skip to content

Commit 31aa6a3

Browse files
Add disableFallback option to prevent cache key fallback (#807)
* Initial plan * Add disableFallback option to skip cache key fallback Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> * Revert dist/ changes — CI handles dist updates Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> * Indicate disableFallback in cache keys log message Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> * Fix prettier formatting in restore.ts Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexarchambault <7063723+alexarchambault@users.noreply.github.com>
1 parent 34d69e0 commit 31aa6a3

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ inputs:
125125
Set 'true' to skip saving and restoring the Ammonite cache,
126126
regardless of whether the repository contains .sc scripts.
127127
default: 'false'
128+
disableFallback:
129+
required: false
130+
description: >
131+
Set 'true' to disable falling back to a less specific cache key when there is no exact
132+
cache hit. By default, if no exact cache match is found, the action will restore from a
133+
more general cache key (e.g. from a different job or build configuration). Disabling this
134+
prevents unintended cache sharing across jobs or matrix instances that have different
135+
dependency sets.
136+
default: 'false'
128137
outputs:
129138
cache-hit-coursier:
130139
description: 'A boolean value to indicate a match was found for the coursier cache'

src/restore.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ async function restoreCache(
8989
extraSharedKey: string,
9090
extraKey: string,
9191
matrixHashedContent: string,
92-
extraHashedContent: string
92+
extraHashedContent: string,
93+
disableFallback: boolean
9394
): Promise<void> {
9495
const upperId = id.toLocaleUpperCase('en-US')
9596
const cacheHitId = `cache-hit-${id}`
@@ -126,7 +127,7 @@ async function restoreCache(
126127

127128
restoreKeys.reverse()
128129

129-
core.info(`${id} cache keys:`)
130+
core.info(`${id} cache keys${disableFallback ? ' (fallback disabled)' : ''}:`)
130131
core.info(` ${key}`)
131132
for (const restoreKey of restoreKeys) {
132133
core.info(` ${restoreKey}`)
@@ -138,7 +139,11 @@ async function restoreCache(
138139
let restoreKey: string | undefined = undefined
139140

140141
try {
141-
restoreKey = await cache.restoreCache(paths, key, restoreKeys)
142+
restoreKey = await cache.restoreCache(
143+
paths,
144+
key,
145+
disableFallback ? [] : restoreKeys
146+
)
142147
} catch (err: unknown) {
143148
const msg = err instanceof Error ? err.message : String(err)
144149
core.info(`[warning] ${msg}`)
@@ -170,7 +175,8 @@ async function restoreCoursierCache(
170175
extraSharedKey: string,
171176
extraKey: string,
172177
matrixHashedContent: string,
173-
extraHashedContent: string
178+
extraHashedContent: string,
179+
disableFallback: boolean
174180
): Promise<void> {
175181
let paths: string[] = []
176182

@@ -190,7 +196,8 @@ async function restoreCoursierCache(
190196
extraSharedKey,
191197
extraKey,
192198
matrixHashedContent,
193-
extraHashedContent
199+
extraHashedContent,
200+
disableFallback
194201
)
195202
}
196203

@@ -200,7 +207,8 @@ async function restoreSbtCache(
200207
extraSharedKey: string,
201208
extraKey: string,
202209
matrixHashedContent: string,
203-
extraHashedContent: string
210+
extraHashedContent: string,
211+
disableFallback: boolean
204212
): Promise<void> {
205213
await restoreCache(
206214
'sbt-ivy2-cache',
@@ -210,7 +218,8 @@ async function restoreSbtCache(
210218
extraSharedKey,
211219
extraKey,
212220
matrixHashedContent,
213-
extraHashedContent
221+
extraHashedContent,
222+
disableFallback
214223
)
215224
}
216225

@@ -220,7 +229,8 @@ async function restoreMillCache(
220229
extraSharedKey: string,
221230
extraKey: string,
222231
matrixHashedContent: string,
223-
extraHashedContent: string
232+
extraHashedContent: string,
233+
disableFallback: boolean
224234
): Promise<void> {
225235
await restoreCache(
226236
'mill',
@@ -230,7 +240,8 @@ async function restoreMillCache(
230240
extraSharedKey,
231241
extraKey,
232242
matrixHashedContent,
233-
extraHashedContent
243+
extraHashedContent,
244+
disableFallback
234245
)
235246
}
236247

@@ -240,7 +251,8 @@ async function restoreAmmoniteCache(
240251
extraSharedKey: string,
241252
extraKey: string,
242253
matrixHashedContent: string,
243-
extraHashedContent: string
254+
extraHashedContent: string,
255+
disableFallback: boolean
244256
): Promise<void> {
245257
await restoreCache(
246258
'ammonite',
@@ -250,7 +262,8 @@ async function restoreAmmoniteCache(
250262
extraSharedKey,
251263
extraKey,
252264
matrixHashedContent,
253-
extraHashedContent
265+
extraHashedContent,
266+
disableFallback
254267
)
255268
}
256269

@@ -319,6 +332,7 @@ async function run(): Promise<void> {
319332
const ignoreJobAsPartCacheKey = readExtraBoolean('ignoreJob')
320333
const ignoreMatrixAsPartCacheKey = readExtraBoolean('ignoreMatrix')
321334
const ignoreAmmonite = readExtraBoolean('ignoreAmmonite')
335+
const disableFallback = readExtraBoolean('disableFallback')
322336

323337
const job = ignoreJobAsPartCacheKey ? '' : readExtraKeys('job')
324338
let matrix = readExtraKeys('matrix')
@@ -378,7 +392,8 @@ async function run(): Promise<void> {
378392
extraKey,
379393
extraCoursierKey,
380394
matrix,
381-
JSON.stringify(coursierHashedContent)
395+
JSON.stringify(coursierHashedContent),
396+
disableFallback
382397
)
383398

384399
if (hasSbtFiles) {
@@ -391,7 +406,8 @@ async function run(): Promise<void> {
391406
JSON.stringify({
392407
sbt: extraSbtHashedContent,
393408
other: extraHashedContent
394-
})
409+
}),
410+
disableFallback
395411
)
396412
}
397413

@@ -405,7 +421,8 @@ async function run(): Promise<void> {
405421
JSON.stringify({
406422
mill: extraMillHashedContent,
407423
other: extraHashedContent
408-
})
424+
}),
425+
disableFallback
409426
)
410427
}
411428

@@ -427,7 +444,8 @@ async function run(): Promise<void> {
427444
JSON.stringify({
428445
amm: extraAmmoniteHashedContent,
429446
other: extraHashedContent
430-
})
447+
}),
448+
disableFallback
431449
)
432450
}
433451
}

0 commit comments

Comments
 (0)