Skip to content

Commit b0e0155

Browse files
authored
fix: construct paths are not printed for nested stacks in CLI output (#18725)
This fixes issue #18724 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c190367 commit b0e0155

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

packages/@aws-cdk/cx-api/lib/artifacts/cloudformation-artifact.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ export class CloudFormationStackArtifact extends CloudArtifact {
3737
public readonly stackName: string;
3838

3939
/**
40-
* A string that represents this stack. Should only be used in user interfaces.
41-
* If the stackName and artifactId are the same, it will just return that. Otherwise,
42-
* it will return something like "<artifactId> (<stackName>)"
40+
* A string that represents this stack. Should only be used in user
41+
* interfaces. If the stackName has not been set explicitly, or has been set
42+
* to artifactId, it will return the hierarchicalId of the stack. Otherwise,
43+
* it will return something like "<hierarchicalId> (<stackName>)"
4344
*/
4445
public readonly displayName: string;
4546

@@ -148,8 +149,8 @@ export class CloudFormationStackArtifact extends CloudArtifact {
148149
this.assets = this.findMetadataByType(cxschema.ArtifactMetadataEntryType.ASSET).map(e => e.data as cxschema.AssetMetadataEntry);
149150

150151
this.displayName = this.stackName === artifactId
151-
? this.stackName
152-
: `${artifactId} (${this.stackName})`;
152+
? this.hierarchicalId
153+
: `${this.hierarchicalId} (${this.stackName})`;
153154

154155
this.name = this.stackName; // backwards compat
155156
this.originalName = this.stackName;

packages/@aws-cdk/cx-api/test/cloud-assembly.test.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,22 @@ test('getStackArtifact retrieves a stack by artifact id', () => {
132132
expect(assembly.getStackArtifact('stack1').id).toEqual('stack1');
133133
});
134134

135-
test('displayName shows both artifact ID and stack name if needed', () => {
135+
test('displayName shows hierarchical ID for nested stack without explicit stackName', () => {
136+
const assembly = new CloudAssembly(path.join(FIXTURES, 'nested-stacks'));
137+
const stackArtifact = assembly.getStackArtifact('topLevelStackNestedStackDAC87084');
138+
expect(stackArtifact.hierarchicalId).toStrictEqual('topLevelStack/nestedStack');
139+
expect(stackArtifact.displayName).toStrictEqual('topLevelStack/nestedStack');
140+
});
141+
142+
test('displayName shows hierarchical ID and stackName for nested stack with explicit stackName', () => {
143+
const assembly = new CloudAssembly(path.join(FIXTURES, 'nested-stacks'));
144+
const nestedStack = assembly.getStackArtifact('topLevelStackNestedStackWithStackName6D28EAEF');
145+
expect(nestedStack.hierarchicalId).toStrictEqual('topLevelStack/nestedStackWithStackName');
146+
expect(nestedStack.stackName).toStrictEqual('explicitStackName');
147+
expect(nestedStack.displayName).toStrictEqual('topLevelStack/nestedStackWithStackName (explicitStackName)');
148+
});
149+
150+
test('displayName shows both hierarchical ID and stack name if needed', () => {
136151
const a1 = new CloudAssembly(path.join(FIXTURES, 'multiple-stacks-same-name'));
137152
expect(a1.getStackArtifact('stack1').displayName).toStrictEqual('stack1 (the-physical-name-of-the-stack)');
138153
expect(a1.getStackArtifact('stack2').displayName).toStrictEqual('stack2 (the-physical-name-of-the-stack)');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": "0.0.0",
3+
"artifacts": {
4+
"topLevelStack": {
5+
"type": "aws:cloudformation:stack",
6+
"environment": "aws://111111111111/us-east-1",
7+
"properties": {
8+
"templateFile": "topLevelStack.template.json"
9+
},
10+
"displayName": "topLevelStack"
11+
},
12+
"topLevelStackNestedStackDAC87084": {
13+
"type": "aws:cloudformation:stack",
14+
"environment": "aws://111111111111/us-east-1",
15+
"properties": {
16+
"templateFile": "nestedStack.template.json"
17+
},
18+
"displayName": "topLevelStack/nestedStack"
19+
},
20+
"topLevelStackNestedStackWithStackName6D28EAEF": {
21+
"type": "aws:cloudformation:stack",
22+
"environment": "aws://111111111111/us-east-1",
23+
"properties": {
24+
"templateFile": "nestedStackWithStackName.template.json",
25+
"stackName": "explicitStackName"
26+
},
27+
"displayName": "topLevelStack/nestedStackWithStackName"
28+
}
29+
}
30+
}

packages/@aws-cdk/cx-api/test/fixtures/nested-stacks/nestedStack.template.json

Whitespace-only changes.

packages/@aws-cdk/cx-api/test/fixtures/nested-stacks/nestedStackWithStackName.template.json

Whitespace-only changes.

packages/@aws-cdk/cx-api/test/fixtures/nested-stacks/topLevelStack.template.json

Whitespace-only changes.

0 commit comments

Comments
 (0)