From 2193b7299aea9145ed8de6261a6cfe4cef621989 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 15 Oct 2020 12:34:10 +0200 Subject: [PATCH 1/2] fix(cli): failure if account cache is malformed Because of concurrent running of integration tests, the account cache (which is supposed to be a JSON file) can be read in a state where it's empty or incompletely written, which fails the JSON parse. If that happens, ignore the error and pretend the cache is empty. Fixes sporadic concurrency issues in the integration tests. --- packages/aws-cdk/bin/cdk.ts | 3 +++ packages/aws-cdk/lib/api/aws-auth/account-cache.ts | 3 +++ packages/aws-cdk/test/account-cache.test.ts | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/packages/aws-cdk/bin/cdk.ts b/packages/aws-cdk/bin/cdk.ts index fcae51e342956..edfbdab0246ec 100644 --- a/packages/aws-cdk/bin/cdk.ts +++ b/packages/aws-cdk/bin/cdk.ts @@ -229,6 +229,9 @@ async function initCommandLine() { return cli.list(args.STACKS, { long: args.long }); case 'diff': + console.log(configuration.context); + console.log(configuration.context.all); + console.log(configuration.context.get(cxapi.ENABLE_DIFF_NO_FAIL)); const enableDiffNoFail = isFeatureEnabled(configuration, cxapi.ENABLE_DIFF_NO_FAIL); return cli.diff({ stackNames: args.STACKS, diff --git a/packages/aws-cdk/lib/api/aws-auth/account-cache.ts b/packages/aws-cdk/lib/api/aws-auth/account-cache.ts index 75082b9953966..30ac1704ccc36 100644 --- a/packages/aws-cdk/lib/api/aws-auth/account-cache.ts +++ b/packages/aws-cdk/lib/api/aws-auth/account-cache.ts @@ -83,6 +83,9 @@ export class AccountAccessKeyCache { // File doesn't exist or is not readable. This is a cache, // pretend we successfully loaded an empty map. if (e.code === 'ENOENT' || e.code === 'EACCES') { return {}; } + // File is not JSON, could be corrupted because of concurrent writes. + // Again, an empty cache is fine. + if (e instanceof SyntaxError) { return {}; } throw e; } } diff --git a/packages/aws-cdk/test/account-cache.test.ts b/packages/aws-cdk/test/account-cache.test.ts index f8860046df584..89887cc627d71 100644 --- a/packages/aws-cdk/test/account-cache.test.ts +++ b/packages/aws-cdk/test/account-cache.test.ts @@ -111,3 +111,14 @@ test(`cache is nuked if it exceeds ${AccountAccessKeyCache.MAX_ENTRIES} entries` await nukeCache(cacheDir); } }); + +test('cache pretends to be empty if cache file does not contain JSON', async() => { + const { cacheDir, cacheFile, cache } = await makeCache(); + try { + await fs.writeFile(cacheFile, ''); + + await expect(cache.get('abc')).resolves.toEqual(undefined); + } finally { + await nukeCache(cacheDir); + } +}); From fb22261a3a154820e51e42943bdb4409d5da8777 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 16 Oct 2020 15:40:01 +0200 Subject: [PATCH 2/2] Update cdk.ts --- packages/aws-cdk/bin/cdk.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/aws-cdk/bin/cdk.ts b/packages/aws-cdk/bin/cdk.ts index edfbdab0246ec..fcae51e342956 100644 --- a/packages/aws-cdk/bin/cdk.ts +++ b/packages/aws-cdk/bin/cdk.ts @@ -229,9 +229,6 @@ async function initCommandLine() { return cli.list(args.STACKS, { long: args.long }); case 'diff': - console.log(configuration.context); - console.log(configuration.context.all); - console.log(configuration.context.get(cxapi.ENABLE_DIFF_NO_FAIL)); const enableDiffNoFail = isFeatureEnabled(configuration, cxapi.ENABLE_DIFF_NO_FAIL); return cli.diff({ stackNames: args.STACKS,