aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.62k stars 3.91k forks source link

aws_cdk: Annotations do not render if they reference tokens #31786

Open rittneje opened 1 week ago

rittneje commented 1 week ago

Describe the bug

If we add an annotation via aws_cdk.Annotations.of(x).add_error(...) where the error message is an f-string contain a token, then it does not print out the error message properly.

Regression Issue

Last Known Working CDK Version

No response

Expected Behavior

It should at least try to print the actual message.

Current Behavior

It prints [object Object].

Reproduction Steps

    app = aws_cdk.App()

    env = aws_cdk.Environment(region="us-east-1")

    stack = aws_cdk.Stack(app, "test-stack", env=env)

    param = aws_ssm.StringParameter(stack, 'string-parameter', string_value='foobar')

    aws_cdk.Annotations.of(param).add_error(f"an error occurred: {param.env.account}")

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.161.0

Framework Version

No response

Node.js Version

v22.9.0

OS

Alpine 3.20.3

Language

Python

Language Version

3.12.7

Other information

No response

khushail commented 2 days ago

Hey @rittneje , thanks for reaching out. So I tried different variations for printing the error message, here is what I observed

Screenshot 2024-10-22 at 3 07 33 PM

-but if I try to print the account value, its not printing anything (may be because the account is not resolved to anything as yet and would be given default value from config) -

Screenshot 2024-10-22 at 3 10 54 PM

This is how the method is implemented in CDK -

https://github.com/aws/aws-cdk/blob/eef4b6601246f0c81869cfe192f359cf16903e2c/packages/aws-cdk-lib/core/lib/annotations.ts#L107

  /**
   * Adds an { "error": <message> } metadata entry to this construct.
   * The toolkit will fail deployment of any stack that has errors reported against it.
   * @param message The error message.
   */
  public addError(message: string) {
    this.addMessage(cxschema.ArtifactMetadataEntryType.ERROR, message);
  }

I did not find any workaround for this so marking this as P2 as passing normal string works.

Marking as P2 means team won't immediately look into it but it would be on their radar and this issue would also be open for community contribution. Hope that helps!