From 7a1500537758584892056b92322dbe65628f17db Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Mon, 6 Jun 2022 14:57:03 -0600 Subject: [PATCH] Avoid printing "reason unknown" for extract entries This was happening when the main Gradle User Home entry was not saved due to having an unchanged cache key. Fixes #309 --- src/cache-base.ts | 13 ++++++++++--- src/cache-extract-entries.ts | 2 +- src/cache-reporting.ts | 15 ++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/cache-base.ts b/src/cache-base.ts index b79fb0f..015f018 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -94,11 +94,18 @@ export class GradleStateCache { async save(listener: CacheListener): Promise { const cacheKey = generateCacheKey(this.cacheName).key const restoredCacheKey = core.getState(RESTORED_CACHE_KEY_KEY) - const entryListener = listener.entry(this.cacheDescription) + const gradleHomeEntryListener = listener.entry(this.cacheDescription) if (restoredCacheKey && cacheKey === restoredCacheKey) { core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`) - entryListener.markUnchanged('cache key not changed') + + for (const entryListener of listener.cacheEntries) { + if (entryListener === gradleHomeEntryListener) { + entryListener.markUnsaved('cache key not changed') + } else { + entryListener.markUnsaved(`referencing '${this.cacheDescription}' cache entry not saved`) + } + } return } @@ -111,7 +118,7 @@ export class GradleStateCache { core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`) const cachePath = this.getCachePath() - await saveCache(cachePath, cacheKey, entryListener) + await saveCache(cachePath, cacheKey, gradleHomeEntryListener) return } diff --git a/src/cache-extract-entries.ts b/src/cache-extract-entries.ts index e9d5c58..36411f9 100644 --- a/src/cache-extract-entries.ts +++ b/src/cache-extract-entries.ts @@ -213,7 +213,7 @@ abstract class AbstractEntryExtractor { if (previouslyRestoredKey === cacheKey) { cacheDebug(`No change to previously restored ${artifactType}. Not saving.`) - entryListener.markUnchanged('contents unchanged') + entryListener.markUnsaved('contents unchanged') } else { core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`) await saveCache([pattern], cacheKey, entryListener) diff --git a/src/cache-reporting.ts b/src/cache-reporting.ts index 8f7f89a..9cd0cbf 100644 --- a/src/cache-reporting.ts +++ b/src/cache-reporting.ts @@ -64,7 +64,7 @@ export class CacheEntryListener { savedKey: string | undefined savedSize: number | undefined - unchanged: string | undefined + unsaved: string | undefined constructor(entryName: string) { this.entryName = entryName @@ -98,8 +98,8 @@ export class CacheEntryListener { return this } - markUnchanged(message: string): CacheEntryListener { - this.unchanged = message + markUnsaved(message: string): CacheEntryListener { + this.unsaved = message return this } } @@ -159,8 +159,8 @@ function getRestoredMessage(entry: CacheEntryListener, isCacheWriteOnly: boolean } function getSavedMessage(entry: CacheEntryListener, isCacheReadOnly: boolean): string { - if (entry.unchanged) { - return `(Entry not saved: ${entry.unchanged})` + if (entry.unsaved) { + return `(Entry not saved: ${entry.unsaved})` } if (entry.savedKey === undefined) { if (isCacheReadOnly) { @@ -190,11 +190,8 @@ function getSize( } function formatSize(bytes: number | undefined): string { - if (bytes === undefined) { + if (bytes === undefined || bytes === 0) { return '' } - if (bytes === 0) { - return '0 (Entry already exists)' - } return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)` }