Skip to content

Conversation

@rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 10, 2025

Move the message definitions to the shared package and make them public. This also required moving a bunch of type definitions to the shared package because they are used as payloads for the message types.

In the CLI it's now possible to access those numbers and access their associated payload types in a type-checked way.

A type-safe switch statement now looks like this:

image

This change also makes the data field of an IoMessage required. The type of a message without payload is now IoMessage<void>: data: void is only inhabited by data: undefined which sort of tracks because m.data === undefined even if there is no payload (don't look too closely at the different behavior for 'data' in m 😎 ). The type can no longer be never because a type like { data: never } can not be inhabited by any values. We could also use data: undefined, also only admitting undefined, but void reads more clearly for its intent.


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

@rix0rrr rix0rrr requested a review from a team March 10, 2025 18:54
@aws-cdk-automation aws-cdk-automation requested a review from a team March 10, 2025 18:54
@github-actions github-actions bot added the p2 label Mar 10, 2025
The `IIoHost` interface has the following method:

```ts
interface IIoHost {
  notify<T>(msg: IoMessage<T>): Promise<void>;
}
```

The generic parameter `T` is only used once, without bounds, for a
singleton argument in input position. This means it is equivalent to the
following:

```ts
interface IIoHost {
  notify(msg: IoMessage<unknown>): Promise<void>;
}
```

(Preferring `unknown` over `any` so that implementors are forced to
test for the type of the `data` field, just like they would need
to do with an argument of type `T`)

In the words of the [Java generics
tutorial](https://docs.oracle.com/javase/tutorial/extra/generics/methods.html):

> Generic methods allow type parameters to be used to express
> dependencies among the types of one or more arguments to a method and/or
> its return type. If there isn't such a dependency, a generic method
> should not be used.

Or [similar advice for
TypeScript](https://effectivetypescript.com/2020/08/12/generics-golden-rule/):

> Type Parameters Should Appear Twice
>
> Type parameters are for relating the types of multiple values. If a
> type parameter is only used once in the function signature, it's not
> relating anything.
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from aa9fcc9 to 91cbd22 Compare March 10, 2025 18:56
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from 91cbd22 to 807f549 Compare March 10, 2025 18:57
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from 807f549 to 074c32c Compare March 10, 2025 18:58
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from 074c32c to 293973f Compare March 10, 2025 19:03
@rix0rrr rix0rrr added the pr/exempt-integ-test Skips the integ test steps if set. label Mar 10, 2025
@rix0rrr rix0rrr closed this Mar 10, 2025
auto-merge was automatically disabled March 10, 2025 19:04

Pull request was closed

@rix0rrr rix0rrr reopened this Mar 10, 2025
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from 293973f to b5cbf62 Compare March 10, 2025 19:14
Move message registry to shared package, so that it can be
shared between the CLI and the toolkit.
@rix0rrr rix0rrr force-pushed the huijbers/publicize-codes branch from b5cbf62 to a0d2444 Compare March 10, 2025 19:21
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 71.42857% with 6 lines in your changes missing coverage. Please review.

Project coverage is 84.89%. Comparing base (e20f1a8) to head (a0d2444).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cli/activity-printer/base.ts 25.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #220      +/-   ##
==========================================
- Coverage   85.16%   84.89%   -0.27%     
==========================================
  Files         208      208              
  Lines       35657    35564      -93     
  Branches     4630     4626       -4     
==========================================
- Hits        30367    30192     -175     
- Misses       5139     5215      +76     
- Partials      151      157       +6     
Flag Coverage Δ
suite.unit 84.89% <71.42%> (-0.27%) ⬇️

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.

@mrgrain mrgrain changed the title chore: move message definitions to shared package refactor(toolkit-lib): move message definitions to shared package Mar 10, 2025
@aws-cdk-automation aws-cdk-automation added this pull request to the merge queue Mar 10, 2025
Merged via the queue into main with commit 0c21495 Mar 10, 2025
14 checks passed
@aws-cdk-automation aws-cdk-automation deleted the huijbers/publicize-codes branch March 10, 2025 21:22
github-merge-queue bot pushed a commit that referenced this pull request Mar 18, 2025
(Replaces #188).

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.

Also in this PR, because this grew.

* Introduce `IoDefaultMessages` in the CLI package, which helps migrate
"legacy" logging code to just emit default warning/info/etc messages to
the IoHost.
* Removed the ability to log with a `{ message: 'asdf' }` object to the
global logger functions. This wasn't being used anywhere other than
tests, and it's sort of pointless: if you know the code you should be
using the `MessageMaker` to make a message object; if you don't know the
code you can emit a string. There is no need to look up the right code
given a level and a message object.
* Make it possible for result types to be any type, not just object
types. This is necessary to cover the "result" from legacy logging,
where the result is just a string.
* Updated many tests in a test file (`cli-io-host.test.ts`) that failed
type checking, but succeeded running, and therefore didn't fail the
build of #220.
* Centralized `TestIoHost` into the helper package, and renamed it to
`MockIoHost`.
* Introducing a `FakeIoHost` in the CLI package to assert on messages
emitted to an `IoHost`.

---
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 <[email protected]>
Co-authored-by: github-actions <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2 pr/exempt-integ-test Skips the integ test steps if set.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants