Skip to content

Commit 94c604e

Browse files
fix(test): harden bundling-disable against ambient CDK_CONTEXT_JSON (#366)
Two robustness fixes to the global bundling-disable setup file, both from PR #371 review (finding 1 hardening nits): - Spread BUNDLING_DISABLED_CONTEXT LAST when merging a pre-set CDK_CONTEXT_JSON, so an ambient `aws:cdk:bundling-stacks` value can no longer silently win and re-enable bundling. This is safe precisely because the documented per-test opt-out is `postCliContext` (applied after the env var regardless), so the escape hatch still works. - Guard `JSON.parse` of the pre-set var with try/catch. A malformed value previously threw inside setupFiles and failed every test in every file with an opaque parse error; now the blame is localized to the offending value. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent ce523fa commit 94c604e

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

cdk/test/setup/disable-bundling.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,23 @@ const BUNDLING_DISABLED_CONTEXT = { 'aws:cdk:bundling-stacks': [] as string[] };
4949

5050
const existing = process.env.CDK_CONTEXT_JSON;
5151
if (existing) {
52-
// Preserve any context already provided; our key wins only if unset.
53-
const merged = { ...BUNDLING_DISABLED_CONTEXT, ...JSON.parse(existing) };
52+
// Preserve any ambient context, but let the bundling disable win
53+
// unconditionally: a pre-set CDK_CONTEXT_JSON that carries
54+
// `aws:cdk:bundling-stacks` must not silently re-enable bundling and defeat
55+
// the speedup. The documented per-test opt-out is `postCliContext` (applied
56+
// AFTER the env var regardless — see above), so spreading our key LAST keeps
57+
// that escape hatch working while making the global disable robust to
58+
// whatever the environment already set.
59+
let ambient: Record<string, unknown>;
60+
try {
61+
ambient = JSON.parse(existing);
62+
} catch {
63+
// Without this guard a malformed pre-set var throws here, inside
64+
// `setupFiles`, and fails EVERY test in EVERY file with an opaque parse
65+
// error. Re-throw with the offending value so the blame is localized.
66+
throw new Error(`malformed CDK_CONTEXT_JSON in environment (must be valid JSON): ${existing}`);
67+
}
68+
const merged = { ...ambient, ...BUNDLING_DISABLED_CONTEXT };
5469
process.env.CDK_CONTEXT_JSON = JSON.stringify(merged);
5570
} else {
5671
process.env.CDK_CONTEXT_JSON = JSON.stringify(BUNDLING_DISABLED_CONTEXT);

0 commit comments

Comments
 (0)