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

(annotations): annotations that include tokens are output as [object Object] #116

Open
1 task
swachter opened this issue Feb 20, 2025 · 2 comments · May be fixed by #101
Open
1 task

(annotations): annotations that include tokens are output as [object Object] #116

swachter opened this issue Feb 20, 2025 · 2 comments · May be fixed by #101
Labels

Comments

@swachter
Copy link

Describe the bug

If a stack with name 'some-stack' includes an info annotation

Annotations.of(this).addInfo(`stackId: ${this.stackId}`);

then the following output results:

[Info at /some-stack] [object Object]

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

The string representation of the token should be output.

Current Behavior

The message is output as "[object Object]"

Reproduction Steps

class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id)
    Annotations.of(this).addInfo(`stackId: ${this.stackId}`)
  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.1000.2 (build bc82193)

Framework Version

No response

Node.js Version

v22.12.0

OS

Ubuntu

Language

TypeScript

Language Version

No response

Other information

No response

@pahud pahud self-assigned this Feb 20, 2025
@pahud
Copy link

pahud commented Feb 20, 2025

https://github.com/aws/aws-cdk/blob/241bda3c28d0d140ce4097c862ff5aa407a53b2f/packages/aws-cdk-lib/core/lib/annotations.ts#L146

I think the fix would be to use Token.isUnresolved() to detect if the string contains any Tokens, and if it does, use Token.asString() to ensure proper resolution at synthesis time.

e.g.

private addMessage(level: string, message: string | IResolvable) {
  const isNew = !this.scope.node.metadata.find((x) => x.data === message);
  if (isNew) {
    // If message already implements IResolvable (like a Token), or is a string containing tokens,
    // we want to preserve its resolvable nature rather than prematurely converting to string
    const normalizedMessage = Token.isUnresolved(message) ? Token.asString(message) : message;
    this.scope.node.addMetadata(level, normalizedMessage, { stackTrace: this.stackTraces });
  }
}

Making it a p1 and requesting inputs from the team.

@go-to-k
Copy link

go-to-k commented Feb 22, 2025

@pahud

I think the fix would be to use Token.isUnresolved() to detect if the string contains any Tokens, and if it does, use Token.asString() to ensure proper resolution at synthesis time.

Even with Token.asString(), all tokens are resolved before the timing of message output by annotation, so I think it is necessary to avoid resolving only annotation tokens in the first place.

However, in that case, the tokens are also output to manifest.json of the cloud assembly. The tokens are for CDK apps, but the cloud assembly is for CFn or CDK CLI. Therefore, the cloud assembly should be output with the token resolved. (It already contains strings in other CFn formats such as ${AWS::AccountId}.)

So I submitted a PR to the CDK CLI repository without modifying the aws-cdk-lib. Because I thought that was the best way to fix just the message output part without changing the manifest format.

Please see the details: #101

@godwingrs22 godwingrs22 transferred this issue from aws/aws-cdk Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants