Skip to content

Commit 1e4dfe6

Browse files
authored
fix: move reference interfaces to their own submodules (#35971)
The introduction of reference interfaces (`IBucketRef`, `IRoleRef`, etc) led to an increased potential for cyclic references between service modules. Cyclic references are a problem in both Python and Go, and prevents us from taking this idea as far as we want to go. In this PR, we introduce a new submodule `aws-cdk-lib.interfaces` where all reference interfaces will live. Because some services have resources with the same name, there are submodules inside the `interfaces` submodule, so actual interface references will look like `aws-cdk-lib.interfaces.aws_s3.IBucketRef`. Because moving the canonical location for these interfaces will be breaking, we leave behind aliases for them in the old location, so TypeScript code can still do `import { IBucketRef } from 'aws-cdk-lib/aws-s3';`. Unfortunately we cannot do the same for other languages because (a) jsii doesn't understand type aliases yet and (b) even if it did, type aliases can not be rendered appropriately in either Java or C#. This means that this change might be backwards breaking for some consumers that implement L2 interfaces in jsii client languages, but we are assuming that most people doing this would be using TypeScript which will remain unaffected. It is unfortunate that we have to do this, but we have painted ourselves into a corner that we otherwise cannot get out of. ## Implementation notes This PR contains heavy refactoring in `spec2cdk` around determining file paths and import paths. We used to have to pass import paths into every service module that we code generated; but if we determine the file names ahead of time, we can also just use `path.posix.relative()` to determine the right import location, which is much easier to use. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 76e0fa0 commit 1e4dfe6

37 files changed

+3988
-2719
lines changed

allowed-breaking-changes.txt

Lines changed: 3032 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"fs-extra": "^9.1.0",
2727
"graceful-fs": "^4.2.11",
2828
"jest-junit": "^13.2.0",
29-
"jsii-diff": "1.118.0",
30-
"jsii-pacmak": "1.118.0",
31-
"jsii-reflect": "1.118.0",
29+
"jsii-diff": "1.119.0",
30+
"jsii-pacmak": "1.119.0",
31+
"jsii-reflect": "1.119.0",
3232
"lerna": "^8.2.4",
3333
"nx": "^20",
3434
"semver": "^7.7.2",

packages/aws-cdk-lib/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
*.d.ts
2-
*.generated.ts
2+
*.generated.*
33
*.js
44
*.js.map
55
*.snk

packages/aws-cdk-lib/aws-iam/lib/grant.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Dependable, IConstruct, IDependable } from 'constructs';
22
import { PolicyStatement } from './policy-statement';
33
import { IGrantable, IPrincipal } from './principals';
44
import * as cdk from '../../core';
5+
import { IEnvironmentAware } from '../../interfaces/environment-aware';
56

67
/**
78
* Basic options for a grant operation
@@ -421,7 +422,7 @@ interface GrantProps {
421422
/**
422423
* A resource with a resource policy that can be added to
423424
*/
424-
export interface IResourceWithPolicyV2 extends cdk.IEnvironmentAware {
425+
export interface IResourceWithPolicyV2 extends IEnvironmentAware {
425426
/**
426427
* Add a statement to the resource's resource policy
427428
*/

packages/aws-cdk-lib/aws-stepfunctions/lib/states/task.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { FieldUtils } from '../fields';
88
import { noEmptyObject } from '../private/util';
99
import { StateGraph } from '../state-graph';
1010
import { IStepFunctionsTask, StepFunctionsTaskConfig } from '../step-functions-task';
11-
import { CatchProps, IChainable, INextable, RetryProps } from '../types';
11+
import { CatchProps, IChainable, INextable, QueryLanguage, RetryProps } from '../types';
1212

1313
/**
1414
* Props that are common to all tasks
@@ -162,7 +162,7 @@ export class Task extends State implements INextable {
162162
/**
163163
* Return the Amazon States Language object for this state
164164
*/
165-
public toStateJson(): object {
165+
public toStateJson(_?: QueryLanguage): object {
166166
return {
167167
...this.renderNextEnd(),
168168
...this.renderRetryCatch(),

packages/aws-cdk-lib/core/lib/environment.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,5 @@ export interface Environment {
3131
readonly region?: string;
3232
}
3333

34-
/**
35-
* Represents the environment a given resource lives in.
36-
*
37-
* Used as the return value for the `IEnvironmentAware.env` property.
38-
*/
39-
export interface ResourceEnvironment {
40-
/**
41-
* The AWS Account ID that this resource belongs to.
42-
*
43-
* Since this can be a Token (for example, when the account is
44-
* CloudFormation's `AWS::AccountId` intrinsic), make sure to use
45-
* `Token.compareStrings()` instead of comparing the values with direct
46-
* string equality.
47-
*/
48-
readonly account: string;
49-
50-
/**
51-
* The AWS Region that this resource belongs to.
52-
*
53-
* Since this can be a Token (for example, when the region is CloudFormation's
54-
* `AWS::Region` intrinsic), make sure to use `Token.compareStrings()` instead
55-
* of comparing the values with direct string equality.
56-
*/
57-
readonly region: string;
58-
}
59-
60-
/**
61-
* Used to indicate that a particular construct has an resource environment
62-
*/
63-
export interface IEnvironmentAware {
64-
/**
65-
* The environment this resource belongs to.
66-
*
67-
* For resources that are created and managed in a Stack (those created by
68-
* creating new class instances like `new Role()`, `new Bucket()`, etc.), this
69-
* is always the same as the environment of the stack they belong to.
70-
*
71-
* For referenced resources (those obtained from referencing methods like
72-
* `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
73-
* different than the stack they were imported into.
74-
*/
75-
readonly env: ResourceEnvironment;
76-
}
34+
// For backwards compatibility with TypeScript users
35+
export type { IEnvironmentAware, ResourceEnvironment } from '../../interfaces/environment-aware';

packages/aws-cdk-lib/core/lib/resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ArnComponents, ArnFormat } from './arn';
22
import { CfnResource } from './cfn-resource';
33
import { RESOURCE_SYMBOL } from './constants';
4-
import { IEnvironmentAware, ResourceEnvironment } from './environment';
54
import { ValidationError } from './errors';
65
import { IStringProducer, Lazy } from './lazy';
76
import { generatePhysicalName, isGeneratedWhenNeededMarker } from './private/physical-name-generator';
@@ -10,6 +9,7 @@ import { RemovalPolicy } from './removal-policy';
109
import { IResolveContext } from './resolvable';
1110
import { Stack } from './stack';
1211
import { Token, Tokenization } from './token';
12+
import { IEnvironmentAware, ResourceEnvironment } from '../../interfaces/environment-aware';
1313

1414
// v2 - leave this as a separate section so it reduces merge conflicts when compat is removed
1515
// eslint-disable-next-line import/order

packages/aws-cdk-lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export * as cloud_assembly_schema from './cloud-assembly-schema';
298298
export * as cloudformation_include from './cloudformation-include';
299299
export * as custom_resources from './custom-resources';
300300
export * as cx_api from './cx-api';
301+
export * as interfaces from './interfaces';
301302
export * as lambda_layer_awscli from './lambda-layer-awscli';
302303
export * as lambda_layer_node_proxy_agent from './lambda-layer-node-proxy-agent';
303304
export * as pipelines from './pipelines';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"targets": {
3+
"java": {
4+
"package": "software.amazon.awscdk.interfaces"
5+
},
6+
"dotnet": {
7+
"namespace": "Amazon.CDK.Interfaces"
8+
},
9+
"python": {
10+
"module": "aws_cdk.interfaces"
11+
}
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CDK Resource Interfaces
2+
=======================
3+
4+
This module contains resource interfaces for all AWS service resources.
5+
6+
These are interfaces that look like this:
7+
8+
```
9+
/**
10+
* Indicates that this resource can be referenced as a Bucket.
11+
*/
12+
interface IBucketRef {
13+
/**
14+
* A reference to a Bucket resource.
15+
*/
16+
readonly bucketRef: BucketReference;
17+
}
18+
19+
interface BucketReference {
20+
/**
21+
* The BucketName of the Bucket resource.
22+
*/
23+
readonly bucketName: string;
24+
25+
/**
26+
* The ARN of the Bucket resource.
27+
*/
28+
readonly bucketArn: string;
29+
}
30+
```
31+
32+
These are in a separate submodule so that they can be referenced from all other
33+
service submodules without introducing cyclic dependencies between them.

0 commit comments

Comments
 (0)