Skip to content
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

chore(cli): use CliIoHost as logger everywhere #32708

Merged
merged 28 commits into from
Jan 14, 2025
Merged

Conversation

HBobertz
Copy link
Contributor

@HBobertz HBobertz commented Jan 1, 2025

Related to #32346

Reason for this change

Capture all logs across the CLI as IoMessages so that when we allow customers to use the CDK Core functionality programmatically, these logs are delivered to the customers as messages and now just swallowed across the CLI.

Description of changes

Prevented access to log() and formatLogMessage(). Now the only ways to log something properly are either through the exported log functions (which is how everyone was doing it anyway), or through CliIoHost.notify() directly.

CliIoHost is now exposed as a global singleton which is currently only directly exclusively used in logging.ts but is effectively used everywhere as all logging functions inevitably call CliIoHost.notify()

All logging functions now optionally support the following input types

error(`operation failed: ${e}`)                                 // infers default error code `TOOLKIT_0000`
error('operation failed: %s', e)                                // infers default error code `TOOLKIT_0000`
error({ message: 'operation failed', code: 'SDK_0001' })        // specifies error code `SDK_0001`
error({ message: 'operation failed: %s', code: 'SDK_0001' }, e) // specifies error code `SDK_0001`

and everything is now translated into an IoMessage and calls CliIoHost.notify() from these logging functions. Nothing currently specifies any message code so it's all the generic _0000, _1000, _2000

Description of how you validated changes

added and updated unit tests

Checklist


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 January 1, 2025 20:52
@github-actions github-actions bot added the p2 label Jan 1, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 1, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Jan 1, 2025
Copy link

codecov bot commented Jan 1, 2025

Codecov Report

Attention: Patch coverage is 79.73568% with 46 lines in your changes missing coverage. Please review.

Project coverage is 81.40%. Comparing base (173a204) to head (59504c4).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32708      +/-   ##
==========================================
- Coverage   81.41%   81.40%   -0.01%     
==========================================
  Files         223      223              
  Lines       13721    13727       +6     
  Branches     2416     2411       -5     
==========================================
+ Hits        11171    11175       +4     
- Misses       2271     2274       +3     
+ Partials      279      278       -1     
Flag Coverage Δ
suite.unit 81.40% <79.73%> (-0.01%) ⬇️

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

Components Coverage Δ
packages/aws-cdk 80.74% <79.73%> (-0.02%) ⬇️
packages/aws-cdk-lib/core 82.10% <ø> (ø)

@HBobertz HBobertz marked this pull request as ready for review January 5, 2025 19:11
@HBobertz HBobertz requested a review from a team as a code owner January 5, 2025 19:11
@@ -108,105 +46,239 @@ export async function withCorkedLogging<T>(block: () => Promise<T>): Promise<T>
} finally {
CORK_COUNTER--;
if (CORK_COUNTER === 0) {
logBuffer.forEach(([stream, str]) => stream.write(str + '\n'));
// Process each buffered message through notify
for (const ioMessage of logBuffer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Backlog item for Corking. Because Corking is global and we cannot have that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -23,7 +23,7 @@ export async function shell(command: string[]): Promise<string> {

// Both write to stdout and collect
child.stdout.on('data', chunk => {
process.stdout.write(chunk);
data(chunk);
Copy link
Contributor

Choose a reason for hiding this comment

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

this probably no good. This used to just print chunks and now we are formatting chunks.

Copy link
Contributor

Choose a reason for hiding this comment

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

For now revert and make backlog item to deal with this.

Only used in cdk init apparently to do some setting up of Dotnet apps

Copy link
Contributor

@mrgrain mrgrain Jan 13, 2025

Choose a reason for hiding this comment

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

Previously this line indiscriminately wrote data chunks from the child process into the output stream. These chunks are probably strings, although technically they can be a buffer.

The chunk then gets passed through formatMessageAndLog and log, to end up on ioHost.notify.
While this is probably safe, the logging helpers as written may or may not format chunks. Going forward, we should have a more explicit API for data chunks.

The problem is not with code calling data but with the eventually called functions no explicitly being aware of the possibility of not to be formatted chunks.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is just to be defensive about unintentional changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/**
* The current action being performed by the CLI. 'none' represents the absence of an action.
*/
export type IoAction = 'synth' | 'list' | 'deploy' | 'destroy' | 'none';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export type IoAction = 'synth' | 'list' | 'deploy' | 'destroy' | 'none';
export type ToolkitAction = 'synth' | 'list' | 'deploy' | 'destroy' | 'none';

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines 6 to 7
import { flatten } from '../../util';
import * as chalk from '../../util/cdk-chalk';
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems we've previously made the decision to import from ../../util (i.e. import from an index.ts file in the util folder). you are now doing something different for cdk-chalk. i am not opinionated on which way we do this, but i think it is important to be consistent, unless you have a compelling reason not to be.

maybe it means that cdk-chalk doesn't belong in util at all?

the resolution to this comment is either turning it into import { chalk, flatten} from '../../util', or moving chalk out of util, or providing a reason why it makes sense to import twice from ../../util in this file.

packages/aws-cdk/lib/api/deployments.ts Show resolved Hide resolved
Comment on lines 4 to 5
import { flatten } from '../../util/arrays';
import * as chalk from '../../util/cdk-chalk';
Copy link
Contributor

Choose a reason for hiding this comment

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

Ugh... it annoys me that we can import flatten multiple ways, from ../../utill/arrays and ../../util. Maybe the solution is to remove util/index.ts because that seems irrelevant. maybe i do this in a separate PR...

HBobertz and others added 3 commits January 13, 2025 14:42
### Reason for this change

Instead of running regexp at runtime, we validate message codes at
compile time

### Checklist
- [x] My code adheres to the [CONTRIBUTING
GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and
[DESIGN
GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*
@aws-cdk-automation
Copy link
Collaborator

➡️ PR build request submitted to test-main-pipeline ⬅️

A maintainer must now check the pipeline and add the pr-linter/cli-integ-tested label once the pipeline succeeds.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 59504c4
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@kaizencc kaizencc self-requested a review January 13, 2025 21:06
@HBobertz HBobertz added pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested labels Jan 14, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 14, 2025 01:47

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-cli-test-run This PR needs CLI tests run against it. label Jan 14, 2025
Copy link
Contributor

mergify bot commented Jan 14, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@HBobertz HBobertz merged commit ef135ef into main Jan 14, 2025
27 of 32 checks passed
@HBobertz HBobertz deleted the bobertzh/CliIoHost branch January 14, 2025 02:01
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution/core This is a PR that came from AWS. p2 pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants