Skip to content

Commit 9237774

Browse files
authored
chore(toolkit-lib): consider developer builds as supporting context overflow (#721)
Updates the context overflow support detection logic to consider developer builds (with version `0.0.0`) as supported. This has no customer facing impact but prevents an incorrect debug message from being emitted when using a dev build of the framework, e.g. when running integ tests in the `aws/aws-cdk` repo. Also extracts the context overflow detection logic into a separate function to improve testability and maintainability. Added unit tests to ensure the function works correctly with different CDK versions. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 91f3b72 commit 9237774

File tree

3 files changed

+217
-8
lines changed

3 files changed

+217
-8
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IO } from '../../io/private';
1515
import type { IReadLock, IWriteLock } from '../../rwlock';
1616
import { RWLock } from '../../rwlock';
1717
import { Settings } from '../../settings';
18+
import type { ConstructTreeNode } from '../../tree';
1819
import { loadTree, some } from '../../tree';
1920
import type { Context, Env } from '../environment';
2021
import { prepareDefaultEnvironment, spaceAvailableForContext, guessExecutable, synthParametersFromSettings } from '../environment';
@@ -262,20 +263,30 @@ export function writeContextToEnv(env: Env, context: Context, completeness: 'add
262263
async function checkContextOverflowSupport(assembly: cxapi.CloudAssembly, ioHelper: IoHelper): Promise<void> {
263264
const traceFn = (msg: string) => ioHelper.defaults.trace(msg);
264265
const tree = await loadTree(assembly, traceFn);
265-
const frameworkDoesNotSupportContextOverflow = some(tree, node => {
266-
const fqn = node.constructInfo?.fqn;
267-
const version = node.constructInfo?.version;
268-
return (fqn === 'aws-cdk-lib.App' && version != null && lte(version, '2.38.0')) // v2
269-
|| fqn === '@aws-cdk/core.App'; // v1
270-
});
271266

272267
// We're dealing with an old version of the framework here. It is unaware of the temporary
273268
// file, which means that it will ignore the context overflow.
274-
if (frameworkDoesNotSupportContextOverflow) {
269+
if (!frameworkSupportsContextOverflow(tree)) {
275270
await ioHelper.notify(IO.CDK_ASSEMBLY_W0010.msg('Part of the context could not be sent to the application. Please update the AWS CDK library to the latest version.'));
276271
}
277272
}
278273

274+
/**
275+
* Checks if the framework supports context overflow
276+
*/
277+
export function frameworkSupportsContextOverflow(tree: ConstructTreeNode | undefined): boolean {
278+
return !some(tree, node => {
279+
const fqn = node.constructInfo?.fqn;
280+
const version = node.constructInfo?.version;
281+
return (
282+
fqn === 'aws-cdk-lib.App' // v2 app
283+
&& version !== '0.0.0' // ignore developer builds
284+
&& version != null && lte(version, '2.38.0') // last version not supporting large context
285+
) // v2
286+
|| fqn === '@aws-cdk/core.App'; // v1 app => not supported
287+
});
288+
}
289+
279290
/**
280291
* Safely create an assembly from a cloud assembly directory
281292
*/

packages/@aws-cdk/toolkit-lib/lib/api/tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function some(node: ConstructTreeNode | undefined, predicate: (n: Constru
3636
}
3737
}
3838

39-
export async function loadTree(assembly: CloudAssembly, trace: (msg: string) => Promise<void>): Promise<ConstructTreeNode | undefined > {
39+
export async function loadTree(assembly: CloudAssembly, trace: (msg: string) => Promise<void>): Promise<ConstructTreeNode | undefined> {
4040
try {
4141
const outdir = assembly.directory;
4242
const fileName = assembly.tree()?.file;
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import { frameworkSupportsContextOverflow } from '../../../lib/api/cloud-assembly/private/prepare-source';
2+
import type { ConstructTreeNode } from '../../../lib/api/tree';
3+
4+
describe('frameworkSupportsContextOverflow', () => {
5+
test('returns true for undefined tree', () => {
6+
expect(frameworkSupportsContextOverflow(undefined)).toBe(true);
7+
});
8+
9+
test('returns true for empty tree', () => {
10+
expect(frameworkSupportsContextOverflow({} as any)).toBe(true);
11+
});
12+
13+
test('returns true for tree with non-App constructs', () => {
14+
const tree: ConstructTreeNode = {
15+
id: 'root',
16+
path: '',
17+
children: {
18+
stack1: {
19+
id: 'stack1',
20+
path: 'stack1',
21+
constructInfo: {
22+
fqn: 'aws-cdk-lib.Stack',
23+
version: '2.50.0',
24+
},
25+
},
26+
},
27+
};
28+
expect(frameworkSupportsContextOverflow(tree)).toBe(true);
29+
});
30+
31+
test('returns false for v1 App', () => {
32+
const tree: ConstructTreeNode = {
33+
id: 'root',
34+
path: '',
35+
children: {
36+
app: {
37+
id: 'app',
38+
path: 'app',
39+
constructInfo: {
40+
fqn: '@aws-cdk/core.App',
41+
version: '1.180.0',
42+
},
43+
},
44+
},
45+
};
46+
expect(frameworkSupportsContextOverflow(tree)).toBe(false);
47+
});
48+
49+
test('returns false for v2 App with version <= 2.38.0', () => {
50+
const tree: ConstructTreeNode = {
51+
id: 'root',
52+
path: '',
53+
children: {
54+
app: {
55+
id: 'app',
56+
path: 'app',
57+
constructInfo: {
58+
fqn: 'aws-cdk-lib.App',
59+
version: '2.38.0',
60+
},
61+
},
62+
},
63+
};
64+
expect(frameworkSupportsContextOverflow(tree)).toBe(false);
65+
});
66+
67+
test('returns true for v2 App with version > 2.38.0', () => {
68+
const tree: ConstructTreeNode = {
69+
id: 'root',
70+
path: '',
71+
children: {
72+
app: {
73+
id: 'app',
74+
path: 'app',
75+
constructInfo: {
76+
fqn: 'aws-cdk-lib.App',
77+
version: '2.38.1',
78+
},
79+
},
80+
},
81+
};
82+
expect(frameworkSupportsContextOverflow(tree)).toBe(true);
83+
});
84+
85+
test('returns true for v2 App with developer version (0.0.0)', () => {
86+
const tree: ConstructTreeNode = {
87+
id: 'root',
88+
path: '',
89+
children: {
90+
app: {
91+
id: 'app',
92+
path: 'app',
93+
constructInfo: {
94+
fqn: 'aws-cdk-lib.App',
95+
version: '0.0.0',
96+
},
97+
},
98+
},
99+
};
100+
expect(frameworkSupportsContextOverflow(tree)).toBe(true);
101+
});
102+
103+
test('returns false if any node in the tree is a v1 App', () => {
104+
const tree: ConstructTreeNode = {
105+
id: 'root',
106+
path: '',
107+
children: {
108+
stack1: {
109+
id: 'stack1',
110+
path: 'stack1',
111+
constructInfo: {
112+
fqn: 'aws-cdk-lib.Stack',
113+
version: '2.50.0',
114+
},
115+
},
116+
nested: {
117+
id: 'nested',
118+
path: 'nested',
119+
children: {
120+
app: {
121+
id: 'app',
122+
path: 'nested/app',
123+
constructInfo: {
124+
fqn: '@aws-cdk/core.App',
125+
version: '1.180.0',
126+
},
127+
},
128+
},
129+
},
130+
},
131+
};
132+
expect(frameworkSupportsContextOverflow(tree)).toBe(false);
133+
});
134+
135+
test('returns false if any node in the tree is a v2 App with version <= 2.38.0', () => {
136+
const tree: ConstructTreeNode = {
137+
id: 'root',
138+
path: '',
139+
children: {
140+
stack1: {
141+
id: 'stack1',
142+
path: 'stack1',
143+
constructInfo: {
144+
fqn: 'aws-cdk-lib.Stack',
145+
version: '2.50.0',
146+
},
147+
},
148+
nested: {
149+
id: 'nested',
150+
path: 'nested',
151+
children: {
152+
app: {
153+
id: 'app',
154+
path: 'nested/app',
155+
constructInfo: {
156+
fqn: 'aws-cdk-lib.App',
157+
version: '2.38.0',
158+
},
159+
},
160+
},
161+
},
162+
},
163+
};
164+
expect(frameworkSupportsContextOverflow(tree)).toBe(false);
165+
});
166+
167+
test('returns true if all v2 Apps in the tree have version > 2.38.0', () => {
168+
const tree: ConstructTreeNode = {
169+
id: 'root',
170+
path: '',
171+
children: {
172+
app1: {
173+
id: 'app1',
174+
path: 'app1',
175+
constructInfo: {
176+
fqn: 'aws-cdk-lib.App',
177+
version: '2.38.1',
178+
},
179+
},
180+
nested: {
181+
id: 'nested',
182+
path: 'nested',
183+
children: {
184+
app2: {
185+
id: 'app2',
186+
path: 'nested/app2',
187+
constructInfo: {
188+
fqn: 'aws-cdk-lib.App',
189+
version: '2.50.0',
190+
},
191+
},
192+
},
193+
},
194+
},
195+
};
196+
expect(frameworkSupportsContextOverflow(tree)).toBe(true);
197+
});
198+
});

0 commit comments

Comments
 (0)