cdklabs / cdklabs/cdk-import

Include or add option for 'moduleKey' to allow for multiple imports in a single CDK Application

Open
#261 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
57
Forks
12
Avg merge
11m
Merged PRs (30d)
8

Description

Hey team, I'm not a TypeScript developer, so apologies for any syntax errors or missed troubleshooting steps that are in the below.

When calling `cdk-import cfn -l `, the generated `jsii_type` key appears to be using the same hash for every module. Which means that when using multiple Level 1 CDK Constructs generated by `cdk-import`, only one of the resources are recognised. **Note:** This is only impacts languages other than `typescript`.

Looking through the `compile.ts` in the `jsii-srcmak` library: https://github.com/cdklabs/jsii-srcmak/blob/main/src/compile.ts
This appears to be because the `jsii_type` key is generated using the value provided to the `moduleKey` argument, else it defaults to a SHA256 hash of the `basepath` variable. However, when logging the value of the `basepath` variable, it appears as though this always defaults to `index.ts`, meaning the created hash is always the same.
https://github.com/cdklabs/jsii-srcmak/blob/5c32a498d369edfb401d5e923d6255bfc21fdd91/src/compile.ts#L29

Generated hash:
`1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6`
Resulting in the following assembly code (for python, but similar outputs exist for other languages):
```
__jsii_assembly__ = jsii.JSIIAssembly.load(
"1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6",
"0.0.0",
__name__[0:-6],
"1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6@0.0.0.jsii.tgz",
)
```

A workaround that I've used for this in local testing is to pass a modified version of the typeName as the 'moduleKey'.
e.g.
```
async function renderCode(options) {
const srcmakopts = {
deps: cdkDeps.map(x => path.dirname(require.resolve(`${x}/package.json`))),
moduleKey: options.typeName.replace(/:/g, ''),
};
```
The resulting `jsii_type`, and generated prefix of the *.tgz file, is prefixed with the name of the resource type, and thus allows for multiple imported type modules to be used within the same CDK application.

Can we please have something like this implemented? Or alternatively have an option provided in the CLI in which we can define an override for the `moduleKey`?

Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.