Skip to content

Commit 2680942

Browse files
committed
fix(cx-api): cannot detect CloudAssembly across different library versions
1 parent ebe9580 commit 2680942

File tree

8 files changed

+38
-11
lines changed

8 files changed

+38
-11
lines changed

packages/@aws-cdk/toolkit/lib/api/cloud-assembly/private/source-builder.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as cxapi from '@aws-cdk/cx-api';
12
import * as fs from 'fs-extra';
23
import type { ICloudAssemblySource } from '../';
34
import { ContextAwareCloudAssembly, ContextAwareCloudAssemblyProps } from './context-aware-source';
@@ -9,6 +10,12 @@ import { ToolkitError } from '../../errors';
910
import { debug } from '../../io/private';
1011
import { AssemblyBuilder, CdkAppSourceProps } from '../source-builder';
1112

13+
// bypass loading from disk if we already have a supported object
14+
const CLOUD_ASSEMBLY_SYMBOL = Symbol.for('@aws-cdk/cx-api.CloudAssembly');
15+
function isCloudAssembly(x: any): x is cxapi.CloudAssembly {
16+
return x !== null && typeof(x) === 'object' && CLOUD_ASSEMBLY_SYMBOL in x;
17+
}
18+
1219
export abstract class CloudAssemblySourceBuilder {
1320

1421
/**
@@ -40,13 +47,19 @@ export abstract class CloudAssemblySourceBuilder {
4047
produce: async () => {
4148
const outdir = determineOutputDirectory(props.outdir);
4249
const env = await prepareDefaultEnvironment(services, { outdir });
43-
return changeDir(async () =>
50+
const assembly = await changeDir(async () =>
4451
withContext(context.all, env, props.synthOptions ?? {}, async (envWithContext, ctx) =>
4552
withEnv(envWithContext, () => builder({
4653
outdir,
4754
context: ctx,
4855
})),
4956
), props.workingDirectory);
57+
58+
if (isCloudAssembly(assembly)) {
59+
return assembly;
60+
}
61+
62+
return new cxapi.CloudAssembly(assembly.directory);
5063
},
5164
},
5265
contextAssemblyProps,

packages/@aws-cdk/toolkit/lib/api/cloud-assembly/source-builder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type * as cxapi from '@aws-cdk/cx-api';
2-
31
export interface AppProps {
42
/**
53
* The output directory into which to the builder app will emit synthesized artifacts.
@@ -12,7 +10,14 @@ export interface AppProps {
1210
readonly context?: { [key: string]: any };
1311
}
1412

15-
export type AssemblyBuilder = (props: AppProps) => Promise<cxapi.CloudAssembly>;
13+
export type AssemblyBuilder = (props: AppProps) => Promise<ICloudAssembly>;
14+
15+
export interface ICloudAssembly {
16+
/**
17+
* The root directory of the cloud assembly.
18+
*/
19+
readonly directory: string;
20+
}
1621

1722
/**
1823
* Configuration for creating a CLI from an AWS CDK App directory

packages/@aws-cdk/toolkit/test/_fixtures/external-context/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export default async () => {
77
new s3.Bucket(stack, 'MyBucket', {
88
bucketName: app.node.tryGetContext('externally-provided-bucket-name'),
99
});
10-
return app.synth() as any;
10+
return app.synth();
1111
};

packages/@aws-cdk/toolkit/test/_fixtures/stack-with-bucket/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit/test/_fixtures/stack-with-bucket/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export default async () => {
55
const app = new core.App();
66
const stack = new core.Stack(app, 'Stack1');
77
new s3.Bucket(stack, 'MyBucket');
8-
return app.synth() as any;
8+
return app.synth();
99
};

packages/@aws-cdk/toolkit/test/_fixtures/two-empty-stacks/index.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit/test/_fixtures/two-empty-stacks/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ export default async () => {
55
new core.Stack(app, 'Stack1');
66
new core.Stack(app, 'Stack2');
77

8-
// @todo fix api
9-
return app.synth() as any;
8+
return app.synth();
109
};

packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { CloudArtifact } from './cloud-artifact';
88
import { topologicalSort } from './toposort';
99
import * as cxschema from '../../cloud-assembly-schema';
1010

11+
const CLOUD_ASSEMBLY_SYMBOL = Symbol.for('@aws-cdk/cx-api.CloudAssembly');
12+
1113
/**
1214
* The name of the root manifest file of the assembly.
1315
*/
@@ -17,6 +19,15 @@ const MANIFEST_FILE = 'manifest.json';
1719
* Represents a deployable cloud application.
1820
*/
1921
export class CloudAssembly {
22+
/**
23+
* Return whether the given object is a Stack.
24+
*
25+
* We do attribute detection since we can't reliably use 'instanceof'.
26+
*/
27+
public static isCloudAssembly(x: any): x is CloudAssembly {
28+
return x !== null && typeof(x) === 'object' && CLOUD_ASSEMBLY_SYMBOL in x;
29+
}
30+
2031
/**
2132
* The root directory of the cloud assembly.
2233
*/

0 commit comments

Comments
 (0)