Skip to content

Commit ea0a2a0

Browse files
committed
fix: harden CDK test cleanup against race conditions and version mismatch
1 parent 65c1102 commit ea0a2a0

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

packages/amplify-e2e-core/src/utils/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,22 @@ export function deleteProjectDir(root: string) {
3333
console.warn(`🌋 Did not delete project dir: ${root}`);
3434
return;
3535
}
36-
rimraf.sync(root);
36+
// Retry up to 3 times with 1s delay to handle Node 22 + rimraf v3
37+
// ENOTEMPTY race conditions during parallel test cleanup
38+
for (let attempt = 0; attempt < 3; attempt++) {
39+
try {
40+
rimraf.sync(root);
41+
return;
42+
} catch (err: any) {
43+
if (attempt === 2) {
44+
console.error(`Failed to delete project dir after 3 attempts: ${root}`, err.message);
45+
return; // Don't throw — cleanup failures shouldn't fail tests
46+
}
47+
// Wait 1s before retry
48+
const { execSync } = require('child_process');
49+
execSync('sleep 1');
50+
}
51+
}
3752
}
3853

3954
export function deleteAmplifyDir(root: string) {

packages/amplify-graphql-api-construct-tests/src/__tests__/ddb-iam-access.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,16 @@ describe('CDK DDB Iam Access', () => {
153153
} catch (err) {
154154
console.log(`Error invoking 'cdk destroy': ${err}`);
155155
}
156-
157-
deleteProjectDir(projRoot);
158-
deleteProjectDir(projRootWithIam);
156+
try {
157+
deleteProjectDir(projRoot);
158+
} catch (e) {
159+
console.error(`Cleanup failed: ${e}`);
160+
}
161+
try {
162+
deleteProjectDir(projRootWithIam);
163+
} catch (e) {
164+
console.error(`Cleanup failed: ${e}`);
165+
}
159166
});
160167

161168
it('can access TodoWithPrivateIam', async () => {

packages/amplify-graphql-api-construct-tests/src/__tests__/log-config.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,16 @@ describe('Log Config Tests', () => {
123123

124124
afterAll(async () => {
125125
const destroyCDKProjectAndDeleteProjectDir = async (projRoot: string): Promise<void> => {
126-
await cdkDestroy(projRoot, '--all');
127-
deleteProjectDir(projRoot);
126+
try {
127+
await cdkDestroy(projRoot, '--all');
128+
} catch (err) {
129+
console.error(`Failed to destroy CDK project ${projRoot}:`, err);
130+
}
131+
try {
132+
deleteProjectDir(projRoot);
133+
} catch (err) {
134+
console.error(`Failed to delete project dir ${projRoot}:`, err);
135+
}
128136
};
129137
const cleanupTasks = projRoots.map((projRoot) => destroyCDKProjectAndDeleteProjectDir(projRoot));
130138
await Promise.all(cleanupTasks);

packages/amplify-graphql-api-construct-tests/src/commands.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
3-
import { copySync, moveSync, readFileSync, writeFileSync } from 'fs-extra';
3+
import { copySync, moveSync, readFileSync, removeSync, writeFileSync } from 'fs-extra';
44
import {
55
addApiWithoutSchema,
66
amplifyPush,
@@ -99,6 +99,14 @@ export const initCDKProject = async (cwd: string, templatePath: string, props?:
9999

100100
copyTemplateDirectory(cwd, templatePath);
101101

102+
// Remove the auto-generated lib/ directory from `cdk init` — it contains
103+
// dead code that references potentially incompatible aws-cdk-lib imports
104+
// and causes TypeScript compilation failures during `cdk destroy`
105+
const libDir = path.join(cwd, 'lib');
106+
if (fs.existsSync(libDir)) {
107+
removeSync(libDir);
108+
}
109+
102110
const deps = [getPackagedConstructPath(props?.construct ?? 'GraphqlApi'), `aws-cdk-lib@${cdkVersion}`, ...additionalDependencies];
103111
await spawn('npm', ['install', ...deps], { cwd, stripColors: true }).runAsync();
104112

0 commit comments

Comments
 (0)