From 1df243a0130ed15034f53d95e6544935de911a88 Mon Sep 17 00:00:00 2001 From: Damien Hill Date: Mon, 24 Jul 2023 23:25:59 +1000 Subject: [PATCH] fix(lambda): bundling fails with pnpm >= 8.4.0 (#26478) (#26479) Fix issue with order of `-f` flag and file path in `rm` command for `pnpm` esbuild bundling step to remove `node_modules/.modules.yaml` from output dir. This is continuing to cause bundling step to fail for `pnpm` >= 8.4.0 with no external `node_modules` specified per issue #26478. Solved by moving the `-f` flag before file path in the `rm` command and updating relevant unit test. Please note that I haven't adjusted the `del` command for windows env as not sure if same issue occurs in that env. Exemption Request: No changes to integration test output of `aws-lambda-nodejs/test/integ.dependencies-pnpm.js` and don't feel this warrants a separate integration test. Closes #26478. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts | 7 ++++--- .../aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts index 0cb0acad11973..5fd9024d8120e 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts @@ -245,7 +245,7 @@ export class Bundling implements cdk.BundlingOptions { osCommand.copy(lockFilePath, pathJoin(options.outputDir, this.packageManager.lockFile)), osCommand.changeDirectory(options.outputDir), this.packageManager.installCommand.join(' '), - isPnpm ? osCommand.remove(pathJoin(options.outputDir, 'node_modules', '.modules.yaml')) + ' -f' : '', // Remove '.modules.yaml' file which changes on each deployment + isPnpm ? osCommand.remove(pathJoin(options.outputDir, 'node_modules', '.modules.yaml'), true) : '', // Remove '.modules.yaml' file which changes on each deployment ]); } @@ -349,12 +349,13 @@ class OsCommand { return `cd "${dir}"`; } - public remove(filePath: string): string { + public remove(filePath: string, force: boolean = false): string { if (this.osPlatform === 'win32') { return `del "${filePath}"`; } - return `rm "${filePath}"`; + const opts = force ? ['-f'] : []; + return `rm ${opts.join(' ')} "${filePath}"`; } } diff --git a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts index 2051d54993d11..59845e6d40671 100644 --- a/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts @@ -432,7 +432,7 @@ test('Detects pnpm-lock.yaml', () => { assetHashType: AssetHashType.OUTPUT, bundling: expect.objectContaining({ command: expect.arrayContaining([ - expect.stringMatching(/echo '' > "\/asset-output\/pnpm-workspace.yaml\".+pnpm-lock\.yaml.+pnpm install --config.node-linker=hoisted --config.package-import-method=clone-or-copy --no-prefer-frozen-lockfile && rm "\/asset-output\/node_modules\/.modules.yaml" -f/), + expect.stringMatching(/echo '' > "\/asset-output\/pnpm-workspace.yaml\".+pnpm-lock\.yaml.+pnpm install --config.node-linker=hoisted --config.package-import-method=clone-or-copy --no-prefer-frozen-lockfile && rm -f "\/asset-output\/node_modules\/.modules.yaml"/), ]), }), });