Skip to content

Commit 9f2ea45

Browse files
authored
fix(core): --debug doesn't record stack traces (#21931)
In v1 we used to record construct stack traces; those have disappeared in v2 because the defaults in the underlying `constructs` library changed. Re-add them when `--debug` is passed. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b36bc11 commit 9f2ea45

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/@aws-cdk/core/lib/cfn-element.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
22
import * as cxapi from '@aws-cdk/cx-api';
33
import { Construct, Node } from 'constructs';
4+
import { debugModeEnabled } from './debug';
45
import { Lazy } from './lazy';
56

67
const CFN_ELEMENT_SYMBOL = Symbol.for('@aws-cdk/core.CfnElement');
@@ -71,6 +72,7 @@ export abstract class CfnElement extends Construct {
7172

7273
if (!this.node.tryGetContext(cxapi.DISABLE_LOGICAL_ID_METADATA)) {
7374
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, {
75+
stackTrace: debugModeEnabled(),
7476
traceFromFunction: this.constructor,
7577
});
7678
}
@@ -204,3 +206,4 @@ function notTooLong(x: string) {
204206
import { CfnReference } from './private/cfn-reference';
205207
import { Stack } from './stack';
206208
import { Token } from './token';
209+

packages/@aws-cdk/core/test/cfn-resource.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
12
import { VALIDATE_SNAPSHOT_REMOVAL_POLICY } from '@aws-cdk/cx-api';
23
import { Construct } from 'constructs';
34
import * as core from '../lib';
@@ -255,4 +256,23 @@ describe('cfn resource', () => {
255256
});
256257
}).toThrow(/should be created in the scope of a Stack, but no Stack found/);
257258
});
259+
260+
test('CfnResource has logical ID metadata with stack trace attached', () => {
261+
process.env.CDK_DEBUG = '1';
262+
try {
263+
const app = new core.App();
264+
const stack = new core.Stack(app, 'Stack');
265+
const res = new core.CfnResource(stack, 'SomeCfnResource', {
266+
type: 'Some::Resource',
267+
});
268+
269+
// THEN
270+
const metadata = res.node.metadata.find(m => m.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID);
271+
expect(metadata).toBeDefined();
272+
expect(metadata?.trace).toBeDefined();
273+
expect(metadata?.trace?.length).toBeGreaterThan(0);
274+
} finally {
275+
delete process.env.CDK_DEBUG;
276+
}
277+
});
258278
});

0 commit comments

Comments
 (0)