Skip to content

Commit

Permalink
fix(core): --debug doesn't record stack traces (#21931)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
rix0rrr authored Sep 6, 2022
1 parent b36bc11 commit 9f2ea45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/core/lib/cfn-element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct, Node } from 'constructs';
import { debugModeEnabled } from './debug';
import { Lazy } from './lazy';

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

if (!this.node.tryGetContext(cxapi.DISABLE_LOGICAL_ID_METADATA)) {
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, {
stackTrace: debugModeEnabled(),
traceFromFunction: this.constructor,
});
}
Expand Down Expand Up @@ -204,3 +206,4 @@ function notTooLong(x: string) {
import { CfnReference } from './private/cfn-reference';
import { Stack } from './stack';
import { Token } from './token';

20 changes: 20 additions & 0 deletions packages/@aws-cdk/core/test/cfn-resource.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { VALIDATE_SNAPSHOT_REMOVAL_POLICY } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import * as core from '../lib';
Expand Down Expand Up @@ -255,4 +256,23 @@ describe('cfn resource', () => {
});
}).toThrow(/should be created in the scope of a Stack, but no Stack found/);
});

test('CfnResource has logical ID metadata with stack trace attached', () => {
process.env.CDK_DEBUG = '1';
try {
const app = new core.App();
const stack = new core.Stack(app, 'Stack');
const res = new core.CfnResource(stack, 'SomeCfnResource', {
type: 'Some::Resource',
});

// THEN
const metadata = res.node.metadata.find(m => m.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID);
expect(metadata).toBeDefined();
expect(metadata?.trace).toBeDefined();
expect(metadata?.trace?.length).toBeGreaterThan(0);
} finally {
delete process.env.CDK_DEBUG;
}
});
});

0 comments on commit 9f2ea45

Please sign in to comment.