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

‼️ NOTICE: jsii libraries using v2 (aws-cdk-lib) require specific import format #17860

Closed
rix0rrr opened this issue Dec 6, 2021 · 14 comments · Fixed by aws/jsii#3233
Closed
Assignees
Labels
@aws-cdk/core Related to core CDK functionality aws-cdk-lib Related to the aws-cdk-lib package bug This issue is a bug. management/tracking Issues that track a subject or multiple issues needs-triage This issue or PR still needs to be triaged. p0

Comments

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 6, 2021

What is the problem?

Please add your +1 👍 to let us know you have encountered this


Status: IN PROGRESS

Overview

When writing plain TypeScript applications using CDK v2 (aws-cdk-lib), you can use one of two different import styles:

// Both of these are allowed for TypeScript apps/libs
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

When writing jsii libraries against CDK v2 (aws-cdk-lib), you can currently only use one of those import formats. It must be:

// Only this one is currently supported by jsii
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

If you use the other one, you will see an error like:

index.ts:4:12 - error JSII3002: Type "aws-cdk-lib.IVpc" cannot be used as a property type because it is not exported from aws-cdk-lib

Resolution

The issue has been resolved as of jsii 1.48.0. Make sure you are using that version or higher when writing construct libraries for v2, and you want to use the particular import statement style that doesn't work otherwise.

@rix0rrr rix0rrr added bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0 @aws-cdk/core Related to core CDK functionality needs-triage This issue or PR still needs to be triaged. labels Dec 6, 2021
@github-actions github-actions bot added the aws-cdk-lib Related to the aws-cdk-lib package label Dec 6, 2021
@rix0rrr rix0rrr pinned this issue Dec 6, 2021
@rix0rrr rix0rrr changed the title ‼️ NOTICE: not possible to write jsii libraries against v2 (aws-cdk-lib) ‼️ NOTICE: jsii libraries using v2 (aws-cdk-lib) require specific import format Dec 6, 2021
rix0rrr added a commit to aws/jsii that referenced this issue Dec 6, 2021
Types imported from dependency's submodules are only correctly
identified if they are imported via a top-level namespace import.

Otherwise, they are incorrectly identified as coming from the top-level
scope and compilation will fail.

Compare:

```ts
// Works
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

// Does not work
import * as ec2 from 'aws-cdk-lib/aws-ec2';
```

Fortunately we already have symbol ids to solve this exact issue
for Rosetta, so we can resort to looking up FQN by symbol id.

Fixes aws/aws-cdk#17860.
rix0rrr added a commit to aws/jsii that referenced this issue Dec 8, 2021
Types imported from dependency's submodules are only correctly
identified if they are imported via a top-level namespace import.

Otherwise, they are incorrectly identified as coming from the top-level
scope and compilation will fail.

Compare:

```ts
// Works
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

// Does not work
import * as ec2 from 'aws-cdk-lib/aws-ec2';
```

Fortunately we already have symbol ids to solve this exact issue
for Rosetta, so we can resort to looking up FQN by symbol id.

Fixes aws/aws-cdk#17860.
mergify bot pushed a commit to aws/jsii that referenced this issue Dec 8, 2021
Types imported from dependency's submodules are only correctly
identified if they are imported via a top-level namespace import.

Otherwise, they are incorrectly identified as coming from the top-level
scope and compilation will fail.

Compare:

```ts
// Works
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

// Does not work
import * as ec2 from 'aws-cdk-lib/aws-ec2';
```

Fortunately we already have symbol ids to solve this exact issue
for Rosetta, so we can resort to looking up FQN by symbol id.

Fixes aws/aws-cdk#17860.



---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
@github-actions
Copy link

github-actions bot commented Dec 8, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@rix0rrr rix0rrr reopened this Dec 8, 2021
@RichiCoder1
Copy link
Contributor

I'm getting this error no matter which style I use, so looking forward to a firmer resolution.

@ldecaro
Copy link

ldecaro commented Dec 15, 2021

The above proposed solution doesn't work on the use case below. Any ideas?

import {Annotations, CfnOutput, IAspect, Stack} from 'aws-cdk-lib';
import {aws_apigateway as api} from 'aws-cdk-lib';
import {aws_iam as iam} from 'aws-cdk-lib';
import {IConstruct} from 'constructs';
export class ApiGatewayHelpers {
public static endpointConfiguration(stack: Stack, group?: string): api.EndpointConfiguration {
const vpcInfo = VpcHelpers.lookupVpcInfo(Stack.of(stack), group)
return {types: [api.EndpointType.PRIVATE], vpcEndpoints: [vpcInfo.apigwEndpointId]}
}

error JSII3002: Type "aws-cdk-lib.EndpointConfiguration" cannot be used as a return type because it is not exported from aws-cdk-lib

9 public static endpointConfiguration(stack: Stack, group?: string): api.EndpointConfiguration {

node_modules/aws-cdk-lib/aws-apigateway/lib/restapi.d.ts:626:1
626 export interface EndpointConfiguration {

627 /**

...
640 readonly vpcEndpoints?: IVpcEndpoint[];

641 }
~
The referenced type is declared here
lib/api-gateway-helpers.ts:44:17 - error JSII3002: Type "aws-cdk-lib.PolicyDocument" cannot be used as a return type because it is not exported from aws-cdk-lib

@biffgaut
Copy link
Contributor

We pinned jsii at 1.47 in our package.json file for the time being, that kept us moving until there's a better solution.

@RichiCoder1
Copy link
Contributor

Have either of ya'll had a chance to see if https://github.com/aws/jsii/releases/tag/v1.49.0 resolves this issue or is it still present?

@RichiCoder1
Copy link
Contributor

Update: This appears to have been fixed in v1.49 for me

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Dec 16, 2021

@ldecaro are you still seeing this in 1.48 or later?

@biffgaut
Copy link
Contributor

We still see it in 1.49 when building for CDK V2

@aws-solutions-constructs/aws-kinesisstreams-gluejob: lib/index.ts:75:12 - error JSII3002: Type "aws-cdk-lib.CfnTable.ColumnProperty" cannot be used as a list element type because it is not exported from aws-cdk-lib

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Dec 17, 2021

@biffgaut I tried reproducing that but seems to work for me.

When I did the following:

  • Ran npx -p aws-cdk-migration rewrite-imports-v2 **/*.ts on @aws-solutions-constructs/core and @aws-solutions-constructs/aws-kinesisstreams-gluejob
  • Updated package.json to depend on aws-cdk-lib@2.1.0
  • Updated package.json to depend on jsii@1.49.0.
  • Got rid of the test/ directories because they needed some source changes I was not prepared to make

It seems to work for me:

huijbers ~/D/a/s/p/@/core(migrate-to-v2)> npx jsii
lib/lambda-event-source-mapping-defaults.ts:24:14 - warning JSII5019: The property name "eventSourceProps" conflicts with the declaring interface "EventSourceProps". This will result in renaming the interface to "_EventSourceProps" in C#. Consider renaming "eventSourceProps".

24     readonly eventSourceProps?: StreamEventSourceProps,
                ~~~~~~~~~~~~~~~~

  lib/lambda-event-source-mapping-defaults.ts:23:18
    23 export interface EventSourceProps {
                        ~~~~~~~~~~~~~~~~
    The declaring interface is introduced here
huijbers ~/D/a/s/p/@/core(migrate-to-v2)> cd ../aws-kinesisstreams-gluejob/
huijbers ~/D/a/s/p/@/aws-kinesisstreams-gluejob(migrate-to-v2)> npx jsii
huijbers ~/D/a/s/p/@/aws-kinesisstreams-gluejob(migrate-to-v2)> npx jsii --version
1.49.0 (build e322d87), typescript 3.9.10

Code here: rix0rrr/aws-solutions-constructs#1

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Dec 20, 2021

If anyone still has issues with this, can you please make me a repro?

@ldecaro
Copy link

ldecaro commented Dec 20, 2021

@rix0rrr I’m not seeing the error described on 1.48.

Thanks.

@biffgaut
Copy link
Contributor

It cleared up for us when we moved up to building on CDK 2.2.0 (we were several RCs back, I imagine it would have cleared up with 2.1.0 as well)

@am29d
Copy link

am29d commented Dec 21, 2021

I had the same issue with cdk version 2.1.0 jsii 1.47.0:

error JSII9002: Unable to resolve type "aws-cdk-lib.LayerVersion". It may be @internal or not exported from the module's entry point (as configured in "package.json" as "main").

After an update to CDK 2.2.0 the issue was resolved, jsii is still at 1.47.0.

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@madeline-k madeline-k unpinned this issue Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality aws-cdk-lib Related to the aws-cdk-lib package bug This issue is a bug. management/tracking Issues that track a subject or multiple issues needs-triage This issue or PR still needs to be triaged. p0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants