Skip to content

Commit

Permalink
fix(cx-api): cannot detect CloudAssembly across different library ver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
mrgrain committed Jan 17, 2025
1 parent ebe9580 commit ed53139
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs-extra';
import type { ICloudAssemblySource } from '../';
import { ContextAwareCloudAssembly, ContextAwareCloudAssemblyProps } from './context-aware-source';
Expand All @@ -9,6 +10,12 @@ import { ToolkitError } from '../../errors';
import { debug } from '../../io/private';
import { AssemblyBuilder, CdkAppSourceProps } from '../source-builder';

// bypass loading from disk if we already have a supported object
const CLOUD_ASSEMBLY_SYMBOL = Symbol.for('@aws-cdk/cx-api.CloudAssembly');
function isCloudAssembly(x: any): x is cxapi.CloudAssembly {
return x !== null && typeof(x) === 'object' && CLOUD_ASSEMBLY_SYMBOL in x;
}

export abstract class CloudAssemblySourceBuilder {

/**
Expand Down Expand Up @@ -40,13 +47,19 @@ export abstract class CloudAssemblySourceBuilder {
produce: async () => {
const outdir = determineOutputDirectory(props.outdir);
const env = await prepareDefaultEnvironment(services, { outdir });
return changeDir(async () =>
const assembly = await changeDir(async () =>
withContext(context.all, env, props.synthOptions ?? {}, async (envWithContext, ctx) =>
withEnv(envWithContext, () => builder({
outdir,
context: ctx,
})),
), props.workingDirectory);

if (isCloudAssembly(assembly)) {
return assembly;
}

return new cxapi.CloudAssembly(assembly.directory);
},
},
contextAssemblyProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type * as cxapi from '@aws-cdk/cx-api';

export interface AppProps {
/**
* The output directory into which to the builder app will emit synthesized artifacts.
Expand All @@ -12,7 +10,14 @@ export interface AppProps {
readonly context?: { [key: string]: any };
}

export type AssemblyBuilder = (props: AppProps) => Promise<cxapi.CloudAssembly>;
export type AssemblyBuilder = (props: AppProps) => Promise<ICloudAssembly>;

export interface ICloudAssembly {
/**
* The root directory of the cloud assembly.
*/
readonly directory: string;
}

/**
* Configuration for creating a CLI from an AWS CDK App directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export default async () => {
new s3.Bucket(stack, 'MyBucket', {
bucketName: app.node.tryGetContext('externally-provided-bucket-name'),
});
return app.synth() as any;
return app.synth();
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export default async () => {
const app = new core.App();
const stack = new core.Stack(app, 'Stack1');
new s3.Bucket(stack, 'MyBucket');
return app.synth() as any;
return app.synth();
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export default async () => {
new core.Stack(app, 'Stack1');
new core.Stack(app, 'Stack2');

// @todo fix api
return app.synth() as any;
return app.synth();
};
13 changes: 13 additions & 0 deletions packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { CloudArtifact } from './cloud-artifact';
import { topologicalSort } from './toposort';
import * as cxschema from '../../cloud-assembly-schema';

const CLOUD_ASSEMBLY_SYMBOL = Symbol.for('@aws-cdk/cx-api.CloudAssembly');

/**
* The name of the root manifest file of the assembly.
*/
Expand All @@ -17,6 +19,15 @@ const MANIFEST_FILE = 'manifest.json';
* Represents a deployable cloud application.
*/
export class CloudAssembly {
/**
* Return whether the given object is a Stack.
*
* We do attribute detection since we can't reliably use 'instanceof'.
*/
public static isCloudAssembly(x: any): x is CloudAssembly {
return x !== null && typeof(x) === 'object' && CLOUD_ASSEMBLY_SYMBOL in x;
}

/**
* The root directory of the cloud assembly.
*/
Expand Down Expand Up @@ -54,6 +65,8 @@ export class CloudAssembly {
this.artifacts = this.renderArtifacts(loadOptions?.topoSort ?? true);
this.runtime = this.manifest.runtime || { libraries: { } };

Object.defineProperty(this, CLOUD_ASSEMBLY_SYMBOL, { value: true });

// force validation of deps by accessing 'depends' on all artifacts
this.validateDeps();
}
Expand Down

0 comments on commit ed53139

Please sign in to comment.