Skip to content

fix(toolkit): diff prints stack name not display name #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class DiffFormatter {
this.ioHelper = props.ioHelper;
this.oldTemplate = props.templateInfo.oldTemplate;
this.newTemplate = props.templateInfo.newTemplate;
this.stackName = props.templateInfo.newTemplate.stackName;
this.stackName = props.templateInfo.newTemplate.displayName ?? props.templateInfo.newTemplate.stackName;
Copy link
Contributor Author

@kaizencc kaizencc May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is kind of iffy, but i think its safe. diff formatter is using stack name to differentiate nested stacks, and i don't think nested stacks can have a display name clash.

stack.displayName is defined as this:

/**
 * A string that represents this stack. Should only be used in user
 * interfaces. If the stackName has not been set explicitly, or has been set
 * to artifactId, it will return the hierarchicalId of the stack. Otherwise,
 * it will return something like "<hierarchicalId> (<stackName>)"
 */

this.changeSet = props.templateInfo.changeSet;
this.nestedStacks = props.templateInfo.nestedStacks;
this.isImport = props.templateInfo.isImport ?? false;
Expand Down
90 changes: 90 additions & 0 deletions packages/aws-cdk/test/commands/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,3 +1150,93 @@ Resources
expect(exitCode).toBe(0);
});
});

describe('stack display names', () => {
beforeEach(() => {

cloudFormation = instanceMockFrom(Deployments);
cloudFormation.readCurrentTemplateWithNestedStacks.mockImplementation((_stackArtifact: CloudFormationStackArtifact) => {
return Promise.resolve({
deployedRootTemplate: {},
nestedStacks: {},
});
});
cloudExecutable = new MockCloudExecutable({
stacks: [
{
stackName: 'MyParent',
displayName: 'Parent/NestedStack',
template: { resource: 'ParentStack' },
},
{
stackName: 'MyChild',
displayName: 'Parent/NestedStack/MyChild',
template: { resource: 'ChildStack' },
},
],
}, undefined, ioHost);

toolkit = new CdkToolkit({
cloudExecutable,
deployments: cloudFormation,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
});
});

test('diff should display stack paths instead of logical IDs', async () => {
// WHEN
const exitCode = await toolkit.diff({
stackNames: ['Parent/NestedStack', 'Parent/NestedStack/MyChild'],
});

// THEN
const parentOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
const childOutput = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');

// Verify that the display name (path) is shown instead of the logical ID
expect(parentOutput).toContain('Stack Parent/NestedStack/MyChild');
expect(parentOutput).not.toContain('Stack MyChild');

expect(childOutput).toContain('Stack Parent/NestedStack');
expect(childOutput).not.toContain('Stack MyParent');

expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
message: expect.stringContaining('✨ Number of stacks with differences: 2'),
}));
expect(exitCode).toBe(0);
});

test('diff should fall back to logical ID if display name is not available', async () => {
// Create a new cloud executable with stacks that don't have display names
cloudExecutable = new MockCloudExecutable({
stacks: [
{
stackName: 'NoDisplayNameStack',
// No displayName provided
template: { resource: 'ParentStack' },
},
],
}, undefined, ioHost);

toolkit = new CdkToolkit({
cloudExecutable,
deployments: cloudFormation,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
});

// WHEN
const exitCode = await toolkit.diff({
stackNames: ['NoDisplayNameStack'],
});

// THEN
const output = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');

// Verify that the logical ID is shown when display name is not available
expect(output).toContain('Stack NoDisplayNameStack');

expect(exitCode).toBe(0);
});
});
Loading