Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pettles opened this issue Jun 8, 2022 · 1 comment

Comments

@Pettles
Copy link

Pettles commented Jun 8, 2022

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 <language> <resource>, 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.

@gchristidis
Copy link

Have come across this issue as well. We have a CDK project that uses 3 CFN resources that are imported with cdk-import and the synth fails with an error similar to Error: Type '1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6.CfnApplication' not found where CfnApplication is a resource in a second imported module.

I have worked around this limitation by running a script post import to rebuild the 1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6 tgz file with a new name replacing all references of 1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6 with the new name. If i give each resource a different name it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants