[core] Multiple projects breaks CDK synth (using instanceof cross-project)
See original GitHub issueβ General Issue
Creating a Construct, shared by multiple stacks, in a separate project, fails to deploy in CDK version >= 1.57.0
In our setup we have multiple stacks sharing misc. Constructs. These Constructs are all in separate Typescript/NodeJS projects. We use Lerna to maintain/link dependencies between projects.
A simplified example of the structure could be:
β tree
.
βββ README.md
βββ lerna.json
βββ package-lock.json
βββ package.json
βββ packages
βββ myapp
βΒ Β βββ README.md
βΒ Β βββ bin
βΒ Β βΒ Β βββ myapp.ts
βΒ Β βββ cdk.json
βΒ Β βββ jest.config.js
βΒ Β βββ lambda
βΒ Β βΒ Β βββ index.ts
βΒ Β βββ lib
βΒ Β βΒ Β βββ myapp-stack.ts
βΒ Β βββ package-lock.json
βΒ Β βββ package.json
βΒ Β βββ test
βΒ Β βΒ Β βββ myapp.test.ts
βΒ Β βββ tsconfig.json
βββ myconstruct
βββ README.md
βββ jest.config.js
βββ lambda
βΒ Β βββ index.ts
βββ lib
βΒ Β βββ index.ts
βββ package-lock.json
βββ package.json
βββ test
βΒ Β βββ myconstruct.test.ts
βββ tsconfig.json
(an example can be found here: https://github.com/gwriss/upgrade_bug)
Running
β npx lerna bootstrap
will npm-install all dependencies and link projects like this (snipped for better overview):
β tree -L 4
.
βββ ...
βββ packages
βββ myapp
βΒ Β βββ ...
βΒ Β βββ node_modules
βΒ Β βΒ Β βββ ...
βΒ Β βΒ Β βββ @aws-cdk
βΒ Β βΒ Β βββ ...
βΒ Β βΒ Β βββ myconstruct -> ../../myconstruct
βΒ Β βΒ Β βββ ...
βΒ Β βββ ...
βββ myconstruct
βββ ...
βββ node_modules
βΒ Β βββ ...
βΒ Β βββ @aws-cdk
βΒ Β βββ ...
βββ ...
Now, when the Construct in myconstruct
contains a Lambda Function and the Myapp in myapp
instantiate an instance of Myconstruct, the synth of the app fails:
β npx lerna run build
lerna notice cli v3.22.1
...
...
lerna success - myapp
lerna success - myconstruct
β cd packages/myapp
β npx cdk synth
unable to determine cloud assembly output directory. Assets must be defined indirectly within a "Stage" or an "App" scope
Subprocess exited with error 1
(an example of the setup can be found and tested here: https://github.com/gwriss/upgrade_bug)
Diving into the problem, it seems like itβs another issue with instanceof
comparing two instances of the same class, but from different node_modules
.
I think @tneely was onto something in https://github.com/aws/aws-cdk/issues/9546#issuecomment-671064704, but decided to close the issue without a resolution.
Another issued related to this is the βDuck Typingβ fix of Stack in https://github.com/aws/aws-cdk/issues/10671.
β οΈ Notice Please donβt pollute the discussion with suggestions about βdeleting node_modules & reinstallβ and βchecking modules are all of the same versionβ. That is another issue but has nothing to do with this issue!
The Question
Did I miss som crucial point in our preferred project setup? I would really like a discussion about the recommended way of organising bigger CDK apps/projects. This could hopefully lead to a new topic in developer guide.
@rix0rrr suggest a peerDependency solution for a similar problem here: https://github.com/aws/aws-cdk/commit/5e10b94aaaa87e6479e0bfcbe844d9fa68d0c1f5 but I fail to see how this would solve the problem. (please enlighten me if I missed something π )
At the moment this is a show-stopper for our long-overdue upgrade of CDK
Reference
- https://github.com/aws/aws-cdk/issues/9546
- https://github.com/aws/aws-cdk/issues/10671
- https://github.com/gwriss/upgrade_bug
Environment
- CDK CLI Version: 1.68.0 (build a6a3f46)
- Module Version:
- Node.js Version: v12.19.0
- OS:
- Language (Version): TypeScript 3.9.7
Other information
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
I understand that CDK, as a project, does not want to fix the dependency problems with npm/lerna. Totally understandable.
Still, this will most likely become a problem lots of people will face in the future, when their CDK projects gets bigger and they need to rearrange their source code into separate projects.
@rix0rrr would it be interesting to have a section in the developer guide for organising bigger CDK projects? What is the best way to proceed with this?
https://github.com/aws/aws-cdk/pull/11113 will most likely fix this issue (still needs to be confirmed when CDK 1.72.0 is released)