From 15c7e8dea298bdf96328922a060c1163f446cd3a Mon Sep 17 00:00:00 2001
From: John Daly <john.daly2@gmail.com>
Date: Wed, 15 Mar 2023 21:57:34 -0700
Subject: [PATCH] fix: Prevent uninitialized cacheProxy from being accessed in
 cleanup (#20975)

---
 lib/util/cache/package/index.spec.ts | 3 +++
 lib/util/cache/package/index.ts      | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/util/cache/package/index.spec.ts b/lib/util/cache/package/index.spec.ts
index 24f1cb5e5f..65bbab5c49 100644
--- a/lib/util/cache/package/index.spec.ts
+++ b/lib/util/cache/package/index.spec.ts
@@ -7,6 +7,9 @@ describe('util/cache/package/index', () => {
   it('returns undefined if not initialized', async () => {
     expect(await get('test', 'missing-key')).toBeUndefined();
     expect(await set('test', 'some-key', 'some-value', 5)).toBeUndefined();
+    expect(async () => {
+      await cleanup({});
+    }).not.toThrow();
   });
 
   it('sets and gets file', async () => {
diff --git a/lib/util/cache/package/index.ts b/lib/util/cache/package/index.ts
index 9cb48a05b2..438095147c 100644
--- a/lib/util/cache/package/index.ts
+++ b/lib/util/cache/package/index.ts
@@ -4,7 +4,7 @@ import * as fileCache from './file';
 import * as redisCache from './redis';
 import type { PackageCache } from './types';
 
-let cacheProxy: PackageCache;
+let cacheProxy: PackageCache | undefined;
 
 function getGlobalKey(namespace: string, key: string): string {
   return `global%%${namespace}%%${key}`;
@@ -74,7 +74,7 @@ export async function cleanup(config: AllConfig): Promise<void> {
   if (config?.redisUrl) {
     await redisCache.end();
   }
-  if (cacheProxy.cleanup) {
+  if (cacheProxy?.cleanup) {
     await cacheProxy.cleanup();
   }
 }
-- 
GitLab