From db6212021f619f36c5ab06ebbd6e7842024e0cf6 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Jun 2021 15:12:11 +0200 Subject: [PATCH 1/4] fix: add cache error recovery for environments without cache --- build/index.js | 17 ++++++++++++++--- src/cache.ts | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/build/index.js b/build/index.js index 0d68957e..46d392d2 100644 --- a/build/index.js +++ b/build/index.js @@ -98989,9 +98989,20 @@ function fromRemoteCache(version, packager, customCacheKey) { // see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401 const target = path_1.default.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os_1.default.arch()); const cacheKey = customCacheKey || getRemoteKey(version, packager); - const hit = yield cache_1.restoreCache([target], cacheKey); - if (hit) { - return target; + try { + // When running with nektos/act, or other custom environments, the cache might not be set up. + const hit = yield cache_1.restoreCache([target], cacheKey); + if (hit) { + return target; + } + } + catch (error) { + if (error.message.toLowerCase().includes('cache service url not found')) { + core.info('Skipping remote cache storage, service URL not found.'); + } + else { + throw error; + } } }); } diff --git a/src/cache.ts b/src/cache.ts index 17e8a351..c35f7d8a 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -32,10 +32,19 @@ export async function fromRemoteCache(version: string, packager: string, customC // see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401 const target = path.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os.arch()); const cacheKey = customCacheKey || getRemoteKey(version, packager); - const hit = await restoreCache([target], cacheKey); - if (hit) { - return target; + try { + // When running with nektos/act, or other custom environments, the cache might not be set up. + const hit = await restoreCache([target], cacheKey); + if (hit) { + return target; + } + } catch (error) { + if (error.message.toLowerCase().includes('cache service url not found')) { + core.info('Skipping remote cache storage, service URL not found.'); + } else { + throw error; + } } } From ba7f08b0d84b7e2a49aa286842e487425d578757 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Jun 2021 15:19:55 +0200 Subject: [PATCH 2/4] refactor: change error handling and add it to cache restore --- build/index.js | 28 +++++++++++++++++++--------- src/cache.ts | 31 ++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/build/index.js b/build/index.js index 46d392d2..536ba4df 100644 --- a/build/index.js +++ b/build/index.js @@ -98997,10 +98997,7 @@ function fromRemoteCache(version, packager, customCacheKey) { } } catch (error) { - if (error.message.toLowerCase().includes('cache service url not found')) { - core.info('Skipping remote cache storage, service URL not found.'); - } - else { + if (!handleRemoteCacheError(error)) { throw error; } } @@ -99018,11 +99015,7 @@ function toRemoteCache(source, version, packager, customCacheKey) { yield cache_1.saveCache([source], cacheKey); } catch (error) { - if (error instanceof cache_1.ReserveCacheError) { - core.info('Skipping remote cache storage, encountered error:'); - core.info(error.message); - } - else { + if (!handleRemoteCacheError(error)) { throw error; } } @@ -99035,6 +99028,23 @@ exports.toRemoteCache = toRemoteCache; function getRemoteKey(version, packager) { return `expo-cli-${process.platform}-${os_1.default.arch()}-${packager}-${version}`; } +/** + * Handle any incoming errors from cache methods. + * This can include actual errors like `ReserveCacheErrors` or unavailability errors. + * When the error is handled, it MUST provide feedback for the developer. + * + * @returns If the error was handled properly. + */ +function handleRemoteCacheError(error) { + const isReserveCacheError = error instanceof cache_1.ReserveCacheError; + const isCacheUnavailable = error.message.toLowerCase().includes('cache service url not found'); + if (isReserveCacheError || isCacheUnavailable) { + core.info('Skipping remote cache storage, encountered error:'); + core.info(error.message); + return true; + } + return false; +} /***/ }), diff --git a/src/cache.ts b/src/cache.ts index c35f7d8a..55bb6060 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -40,9 +40,7 @@ export async function fromRemoteCache(version: string, packager: string, customC return target; } } catch (error) { - if (error.message.toLowerCase().includes('cache service url not found')) { - core.info('Skipping remote cache storage, service URL not found.'); - } else { + if (!handleRemoteCacheError(error)) { throw error; } } @@ -58,10 +56,7 @@ export async function toRemoteCache(source: string, version: string, packager: s try { await saveCache([source], cacheKey); } catch (error) { - if (error instanceof ReserveCacheError) { - core.info('Skipping remote cache storage, encountered error:'); - core.info(error.message); - } else { + if (!handleRemoteCacheError(error)) { throw error; } } @@ -73,3 +68,25 @@ export async function toRemoteCache(source: string, version: string, packager: s function getRemoteKey(version: string, packager: string): string { return `expo-cli-${process.platform}-${os.arch()}-${packager}-${version}`; } + +/** + * Handle any incoming errors from cache methods. + * This can include actual errors like `ReserveCacheErrors` or unavailability errors. + * When the error is handled, it MUST provide feedback for the developer. + * + * @returns If the error was handled properly. + */ +function handleRemoteCacheError(error: Error): boolean { + const isReserveCacheError = error instanceof ReserveCacheError; + const isCacheUnavailable = error.message.toLowerCase().includes( + 'cache service url not found', + ); + + if (isReserveCacheError || isCacheUnavailable) { + core.info('Skipping remote cache storage, encountered error:'); + core.info(error.message); + return true; + } + + return false; +} From 7de0531c058fa3cb61ce5a1494026d8917fbe39d Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Jun 2021 15:26:41 +0200 Subject: [PATCH 3/4] chore: add cache tests for unavailability errors --- tests/cache.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/cache.test.ts b/tests/cache.test.ts index f8a00634..d168b419 100644 --- a/tests/cache.test.ts +++ b/tests/cache.test.ts @@ -74,6 +74,13 @@ describe('fromRemoteCache', () => { spy.restore.mockRejectedValueOnce(error); await expect(cache.fromRemoteCache('3.20.1', 'yarn')).rejects.toBe(error); }); + + it('skips remote cache when unavailable', async () => { + // see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41 + const error = new Error('Cache Service Url not found, unable to restore cache.'); + spy.restore.mockRejectedValueOnce(error); + await expect(cache.fromRemoteCache('3.20.1', 'yarn')).resolves.toBeUndefined(); + }); }); describe('toRemoteCache', () => { @@ -107,4 +114,11 @@ describe('toRemoteCache', () => { spy.save.mockRejectedValueOnce(error); await expect(cache.toRemoteCache(join('local', 'path'), '3.20.1', 'yarn')).rejects.toBe(error); }); + + it('skips remote cache when unavailable', async () => { + // see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41 + const error = new Error('Cache Service Url not found, unable to restore cache.'); + spy.save.mockRejectedValueOnce(error); + await expect(cache.toRemoteCache(join('local', 'path'), '3.20.1', 'yarn')).resolves.toBeUndefined(); + }); }); From 16e05529427c439ee86ea1b781761782d83c0175 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Jun 2021 15:32:02 +0200 Subject: [PATCH 4/4] refactor: mark cache failures as warnings --- build/index.js | 4 ++-- src/cache.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build/index.js b/build/index.js index 536ba4df..f6f1c3ff 100644 --- a/build/index.js +++ b/build/index.js @@ -99039,8 +99039,8 @@ function handleRemoteCacheError(error) { const isReserveCacheError = error instanceof cache_1.ReserveCacheError; const isCacheUnavailable = error.message.toLowerCase().includes('cache service url not found'); if (isReserveCacheError || isCacheUnavailable) { - core.info('Skipping remote cache storage, encountered error:'); - core.info(error.message); + core.warning('Skipping remote cache storage, encountered error:'); + core.warning(error.message); return true; } return false; diff --git a/src/cache.ts b/src/cache.ts index 55bb6060..25c67984 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -83,8 +83,8 @@ function handleRemoteCacheError(error: Error): boolean { ); if (isReserveCacheError || isCacheUnavailable) { - core.info('Skipping remote cache storage, encountered error:'); - core.info(error.message); + core.warning('Skipping remote cache storage, encountered error:'); + core.warning(error.message); return true; }