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

fix(aspects): "localAspects is not iterable" error #32647

Merged
merged 8 commits into from
Dec 23, 2024
22 changes: 18 additions & 4 deletions packages/aws-cdk-lib/core/lib/private/synthesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CloudAssembly } from '../../../cx-api';
import * as cxapi from '../../../cx-api';
import { Annotations } from '../annotations';
import { App } from '../app';
import { AspectApplication, Aspects } from '../aspect';
import { AspectApplication, AspectPriority, Aspects } from '../aspect';
import { FileSystem } from '../fs';
import { Stack } from '../stack';
import { ISynthesisSession } from '../stack-synthesizers/types';
Expand Down Expand Up @@ -228,7 +228,7 @@ function invokeAspects(root: IConstruct) {
const node = construct.node;
const aspects = Aspects.of(construct);

let localAspects = aspects.applied;
let localAspects = getAspectApplications(construct);
const allAspectsHere = sortAspectsByPriority(inheritedAspects, localAspects);

const nodeAspectsCount = aspects.all.length;
Expand Down Expand Up @@ -290,11 +290,10 @@ function invokeAspectsV2(root: IConstruct) {

function recurse(construct: IConstruct, inheritedAspects: AspectApplication[]): boolean {
const node = construct.node;
const aspects = Aspects.of(construct);

let didSomething = false;

let localAspects = aspects.applied;
let localAspects = getAspectApplications(construct);
const allAspectsHere = sortAspectsByPriority(inheritedAspects, localAspects);

for (const aspectApplication of allAspectsHere) {
Expand Down Expand Up @@ -354,6 +353,21 @@ function sortAspectsByPriority(inheritedAspects: AspectApplication[], localAspec
return allAspects;
}

/**
* Helper function to get aspect applications.
* If `Aspects.applied` is available, it is used; otherwise, create AspectApplications from `Aspects.all`.
*/
function getAspectApplications(node: IConstruct): AspectApplication[] {
const aspects = Aspects.of(node);
if (aspects.applied !== undefined) {
return aspects.applied;
}

// Fallback: Create AspectApplications from `aspects.all`
const typedAspects = aspects as Aspects;
return typedAspects.all.map(aspect => new AspectApplication(node, aspect, AspectPriority.DEFAULT));
}

/**
* Find all stacks and add Metadata Resources to all of them
*
Expand Down
20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/core/test/aspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,24 @@ describe('aspect', () => {
}
}
}

test.each([
{ stabilization: true },
{ stabilization: false },
])('Error is not thrown if Aspects.applied does not exist (stabilization: $stabilization)', ({ stabilization }) => {
const app = new App({ context: { '@aws-cdk/core:aspectStabilization': stabilization } });
const root = new Stack(app, 'My-Stack');

Aspects.of(root).add(new Tag('AspectA', 'Visited'));

// "Monkey patching" - Override `applied` to simulate its absence
Object.defineProperty(Aspects.prototype, 'applied', {
value: undefined,
configurable: true,
});

expect(() => {
app.synth();
}).not.toThrow();
});
});
2 changes: 2 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './yargs-gen';
export * from './yargs-types';
19 changes: 19 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/index.js

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

9 changes: 9 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/yargs-gen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Expression, ExternalModule } from '@cdklabs/typewriter';
import { CliConfig } from './yargs-types';
export declare class CliHelpers extends ExternalModule {
readonly browserForPlatform: import("@cdklabs/typewriter").ExpressionProxy<Expression>;
readonly cliVersion: import("@cdklabs/typewriter").ExpressionProxy<Expression>;
readonly isCI: import("@cdklabs/typewriter").ExpressionProxy<Expression>;
readonly yargsNegativeAlias: import("@cdklabs/typewriter").ExpressionProxy<Expression>;
}
export declare function renderYargs(config: CliConfig, helpers: CliHelpers): Promise<string>;
165 changes: 165 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/yargs-gen.js

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/yargs-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
interface YargsCommand {
description: string;
options?: {
[optionName: string]: YargsOption;
};
aliases?: string[];
arg?: YargsArg;
}
interface CliAction extends YargsCommand {
options?: {
[optionName: string]: CliOption;
};
}
interface YargsArg {
name: string;
variadic: boolean;
}
export interface YargsOption {
type: 'string' | 'array' | 'number' | 'boolean' | 'count';
desc?: string;
default?: any;
deprecated?: boolean | string;
choices?: ReadonlyArray<string | number | true | undefined>;
alias?: string | string[];
conflicts?: string | readonly string[] | {
[key: string]: string | readonly string[];
};
nargs?: number;
requiresArg?: boolean;
hidden?: boolean;
count?: boolean;
}
export interface CliOption extends Omit<YargsOption, 'nargs' | 'hidden'> {
negativeAlias?: string;
}
export interface Middleware {
callback: string;
args: string[];
applyBeforeValidation?: boolean;
}
export interface CliConfig {
globalOptions: {
[optionName: string]: CliOption;
};
commands: {
[commandName: string]: CliAction;
};
}
/**
* The result of a DynamicValue call
*/
export interface DynamicResult {
dynamicType: 'parameter' | 'function';
dynamicValue: string;
}
export {};
3 changes: 3 additions & 0 deletions tools/@aws-cdk/yargs-gen/lib/yargs-types.js

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

1 change: 1 addition & 0 deletions tools/@aws-cdk/yargs-gen/test/cli.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading
Loading