From 49b1c0c8191c997e0f45d4a75ad7ba1a736db283 Mon Sep 17 00:00:00 2001 From: Rico Hermans Date: Wed, 29 Apr 2026 09:47:19 +0000 Subject: [PATCH 1/2] fix(aws-cdk-lib): cannot be used as a `bundledDependency` NPM has a bug where if a package is used as a bundled dependency at multiple levels in a dependency tree, the containing package cannot be itself used as a bundled dependency. For us, this manifests because ``` aws-cdk-lib -[bundles]-> jsonschema -[bundles]-> semver -[bundles]-> @aws-cdk/cloud-assembly-api -[bundles]-> jsonschema -[bundles]-> semver ``` If then `aws-cdk-lib` is itself declared as a bundled depencency, NPM errors out during `npm install`. Testing shows that this only happens if the same dependency is bundled multiple times; if the bundled dependencies are different it seems to work out. We fix this by making sure that dependencies that are bundled by `aws-cdk-lib` itself are not bundled by any of its dependencies. We `rm -rf` them from the nested dependency tree. --- packages/aws-cdk-lib/package.json | 1 + .../scripts/strip-nested-bundled-deps.ts | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts diff --git a/packages/aws-cdk-lib/package.json b/packages/aws-cdk-lib/package.json index 296f1cae8dd46..1661c92b1e3c6 100644 --- a/packages/aws-cdk-lib/package.json +++ b/packages/aws-cdk-lib/package.json @@ -31,6 +31,7 @@ "stripDeprecated": true, "compressAssembly": true, "pre": [ + "ts-node ./scripts/strip-nested-bundled-deps.ts", "ts-node region-info/build-tools/generate-static-data.ts", "(rm -rf core/test/fs/fixtures && cd core/test/fs && tar -xzf fixtures.tar.gz)", "(rm -rf assets/test/fs/fixtures && cd assets/test/fs && tar -xzf fixtures.tar.gz)", diff --git a/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts b/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts new file mode 100644 index 0000000000000..6ca8b16b39864 --- /dev/null +++ b/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts @@ -0,0 +1,43 @@ +#!/usr/bin/env node +/** + * Node.js fully doesn't survive if the same dependency is bundled at multiple + * levels. Since we bundle `@aws-cdk/cloud-assembly-api` and that package + * bundles some dependencies that `aws-cdk-lib` also bundles, we tickle that + * bug. + * + * Get rid of that problem by deleting all bundled dependencies from deeper + * `node_modules` directories that we already bundle ourselves as well. + * + * We can run this command either before compilation or before packing. We choose + * to run it as early as possible to improve the chances of us detecting any + * problems with it. + */ +import * as fs from 'fs'; + +function main() { + const pj = JSON.parse(fs.readFileSync('package.json', 'utf-8')); + const bundledDeps = pj.bundleDependencies as string[]; + + // A bundled dependency cannot be bundled under any other bundled depencency. + for (const outerDep of bundledDeps) { + for (const unwantedDep of bundledDeps) { + const conflictPath = `node_modules/${outerDep}/node_modules/${unwantedDep}`; + // { force: true } will make this not error if the path doesn't exist. + fs.rmSync(conflictPath, { recursive: true, force: true }); + } + } +} + +function pathExistsSync(p: string) { + try { + fs.statSync(p); + return true; + } catch (e: any) { + if (e.code === 'ENOENT') { + return false; + } + throw e; + } +} + +main(); From 2ce0c5d2947f50eb86b58f523647e05055948ecb Mon Sep 17 00:00:00 2001 From: Rico Hermans Date: Wed, 29 Apr 2026 10:10:53 +0000 Subject: [PATCH 2/2] Unused var --- .../aws-cdk-lib/scripts/strip-nested-bundled-deps.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts b/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts index 6ca8b16b39864..2e7532a4431bd 100644 --- a/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts +++ b/packages/aws-cdk-lib/scripts/strip-nested-bundled-deps.ts @@ -28,16 +28,4 @@ function main() { } } -function pathExistsSync(p: string) { - try { - fs.statSync(p); - return true; - } catch (e: any) { - if (e.code === 'ENOENT') { - return false; - } - throw e; - } -} - main();