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

fix(cli): write notices to stderr or don't write them at all #188

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 4, 2025

On CI systems, the CDK CLI tries to avoid writing to stderr because there are a couple of CI systems that are commonly configured to fail if any output is written to stderr. That means all output, like notices, must go to stdout.

Some commands (like cdk synth or cdk bootstrap --show-template) produce usable output on stdout, and these are commonly scripted, like piping their output to a file.

However, because notices must go to stdout, these now interfere with the output of these commands.

This needs a more thorough reworking of the CLI output streams, but there is a risk of affecting users who are currently relying on the fact that all output goes to stdout.

In this PR, we are doing the first steps to solving this situation:

  • Notices will always go to stderr, so that they will never interfere with stdout anymore.
  • We try to detect what CI system we are running on, and we will completely suppress notices unless we determine that we are running on a CI system where it is "safe" to write to sterr (fail closed).

"Safe" in this case means that the CI system doesn't come with an easy to toggle checkbox that makes commands fail based on what they print, instead of their exit codes. The only systems I'm aware of that have this checkbox are "Azure DevOps", and "TeamCity running PowerShell scripts".

Even though we know the systems that are "unsafe", we will only show notices on systems known to be "safe".

To make the existing Notices code play nice with the IoHost, I needed to do a couple of things:

  • Make a helper class, IoHostEmitter, to format messages that are sent to the IoHost. This converts strings or structured messages into an IoMessage.
  • Thread instances of this helper class to everywhere we emit messages. This required making the static NoticesFilter class a proper class with an ioHostEmitter member.
  • Add a FakeIoHost class for tests, to check for the messages we're emitting (as opposed to spying on stdout or spying on the logging members)

Fixes aws/aws-cdk#33589.


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

On CI systems, the CDK CLI tries to avoid writing to `stderr` because
there are a couple of CI systems that are commonly configured to fail if
any output is written to `stderr`. That means all output, like notices,
must go to `stdout`.

Some commands (like `cdk synth` or `cdk bootstrap --show-template`)
produce usable output on `stdout`, and these are commonly scripted, like
piping their output to a file.

However, because notices must go to `stdout`, these now interfere with
the output of these commands.

This needs a more thorough reworking of the CLI output streams, but
there is a risk of affecting users who are currently relying on the fact
that all output goes to `stdout`.

In this PR, we are doing the first steps to solving this situation:

- Notices will always go to `stderr`, so that they will never interfere
  with `stdout` anymore.
- We try to detect what CI system we are running on, and we will
  completely suppress notices *unless* we determine that we are running
  on a CI system where it is "safe" to write to `sterr` (fail closed).

"Safe" in this case means that the CI system doesn't come with an easy
to toggle checkbox that makes commands fail based on what they print,
instead of their exit codes. The only systems I'm aware of that have
this checkbox are "Azure DevOps", and "TeamCity running PowerShell
scripts".

Even though we know the systems that are "unsafe", we will only
show notices on systems known to be "safe".

Fixes aws/aws-cdk#33589.
@rix0rrr rix0rrr requested a review from a team March 4, 2025 10:28
@github-actions github-actions bot added the p2 label Mar 4, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team March 4, 2025 10:29
*
* @default true
*/
readonly shouldDisplay?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Messages are now always emitted -- whether they are displayed or not is up to the IoHost.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 90.68100% with 26 lines in your changes missing coverage. Please review.

Project coverage is 85.02%. Comparing base (faa2de0) to head (d25fd89).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cli/cli.ts 54.54% 10 Missing ⚠️
packages/aws-cdk/lib/logging.ts 92.64% 5 Missing ⚠️
packages/aws-cdk/lib/cli/ci-systems.ts 95.18% 4 Missing ⚠️
packages/aws-cdk/lib/toolkit/cli-io-host.ts 91.83% 3 Missing and 1 partial ⚠️
packages/aws-cdk/lib/notices.ts 94.73% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #188    +/-   ##
========================================
  Coverage   85.02%   85.02%            
========================================
  Files         207      208     +1     
  Lines       35726    35919   +193     
  Branches     4605     4626    +21     
========================================
+ Hits        30375    30540   +165     
- Misses       5197     5232    +35     
+ Partials      154      147     -7     
Flag Coverage Δ
suite.unit 85.02% <90.68%> (+<0.01%) ⬆️

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.

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.

aws-cdk-notices: CDK Notice being parsed incorrectly causing YAMLSemanticError
2 participants