Skip to content

Conversation

@kaizencc
Copy link
Contributor

@kaizencc kaizencc commented Mar 19, 2025

part of https://github.com/orgs/aws/projects/257

this refactor removes direct prints to stderr or stdout for the diff cli command. the diff result will instead be printed through the global CliIoHost.

the diff format is determined by the Formatter class, which takes in a stream that previously was stdout (or stderr). now, we are sending our own stream into Formatter, capturing what was previously immediately printed and instead returning it as a string. we then print the resulting string through CliIoHost. it should have no functional change to how the diff cli works today.

this effort will help support diff in the toolkit lib because it allows the IoHost governance of the diff print.

as this is a pure refactor, successful existing tests is enough to ensure that this works.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team March 19, 2025 22:28
@kaizencc kaizencc closed this Mar 20, 2025
@kaizencc kaizencc reopened this Mar 20, 2025
@kaizencc kaizencc marked this pull request as ready for review March 20, 2025 20:54
Comment on lines 200 to 202
diffs = numberFromBool(printSecurityDiff(template, stacks.firstStack, RequireApproval.Broadening, quiet));
} else {
const { stackDiffCount, printableStackDiff } = printStackDiff(template, stacks.firstStack, strict, contextLines, quiet, undefined, undefined, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd strongly advise you rename these functions since they don't print anything anymore.

Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

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

This doesn't touch printSecurityDiff yet, right?

@kaizencc kaizencc changed the title refactor(cli): diff no longer prints directly to a stream refactor(cli): stack diff no longer prints directly to a stream Mar 21, 2025
@kaizencc kaizencc changed the title refactor(cli): stack diff no longer prints directly to a stream refactor(cli): stack/security diff no longer prints directly to a stream Mar 21, 2025
@kaizencc kaizencc requested a review from mrgrain March 21, 2025 19:01
import type { Deployments } from './deployments';
import { ToolkitError } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api';
import { IO, type IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
import { info } from '../../cli/messages';
Copy link
Contributor

Choose a reason for hiding this comment

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

Nope!

@codecov-commenter
Copy link

codecov-commenter commented Mar 21, 2025

Codecov Report

Attention: Patch coverage is 73.48066% with 48 lines in your changes missing coverage. Please review.

Project coverage is 85.24%. Comparing base (ee0d87d) to head (c9463d0).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cli/cdk-toolkit.ts 48.97% 25 Missing ⚠️
packages/aws-cdk/lib/commands/diff.ts 85.15% 19 Missing ⚠️
packages/aws-cdk/lib/api/deployments/cfn-api.ts 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #264      +/-   ##
==========================================
+ Coverage   85.18%   85.24%   +0.05%     
==========================================
  Files         221      221              
  Lines       36512    36606      +94     
  Branches     4445     4450       +5     
==========================================
+ Hits        31104    31205     +101     
+ Misses       5307     5306       -1     
+ Partials      101       95       -6     
Flag Coverage Δ
suite.unit 85.24% <73.48%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kaizencc kaizencc requested a review from mrgrain March 21, 2025 19:09
@aws-cdk-automation aws-cdk-automation added this pull request to the merge queue Mar 21, 2025
Merged via the queue into main with commit 762e57f Mar 21, 2025
20 checks passed
@aws-cdk-automation aws-cdk-automation deleted the conroy/diff-refactor branch March 21, 2025 22:03
github-merge-queue bot pushed a commit that referenced this pull request Apr 2, 2025
Fixes #302 

In #264, we capture the output of
the Formatter into a stream, and that is then converted to a string.
This diff only gets printed if the diff isn't empty, but by some quirk
`formatStackDiff` used to print the stack name _outside_ of the stream,
thus creating the (correct) behavior. #264 made sure that `format`
doesn't print, so we need to make sure that the consumer of
`formatStackDiff` prints the relevant info.

This PR makes sure that the stack name is returned as the formatted diff
even if the actual diff is empty. We will print the right information
when we print the formatted diff now.

I also made similar modifications to `formatSecurityDiff`, as we should
move away from `format` methods printing anything at all.

BEFORE (cdk 2.1006.0):

<img width="326" alt="Screenshot 2025-04-02 at 9 04 04 AM"
src="https://github.com/user-attachments/assets/03b92517-e475-4c27-a4c1-52217be824c8"
/>


AFTER (my local cdk):

<img width="322" alt="Screenshot 2025-04-02 at 9 03 11 AM"
src="https://github.com/user-attachments/assets/53a7d3e0-2e62-45ca-898b-93c923028346"
/>


---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license

---------

Signed-off-by: github-actions <github-actions@github.com>
Co-authored-by: github-actions <github-actions@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants