Open tdenniston opened 6 months ago
Reproducible.
Similar code below in TypeScript works:
import * as cdk from 'aws-cdk-lib';
import * as awslogs from 'aws-cdk-lib/aws-logs';
import { TypescriptStack } from '../lib/typescript-stack';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
new awslogs.LogGroup(stack, 'log-group', {
logGroupName: cdk.Lazy.string({
produce() {
return 'value';
}})
});
.NET code:
using Amazon.CDK;
using Amazon.CDK.AWS.Logs;
namespace Dotnet
{
sealed class Program
{
public static void Main(string[] args)
{
var app = new App();
var stack = new Stack(app, "MyStack");
new LogGroup(stack, "log-group", new LogGroupProps()
{
LogGroupName = Lazy.String(new Producer())
});
app.Synth();
}
class Producer : IStableStringProducer
{
public string Produce()
{
return "value";
}
}
}
}
fails with below error:
Unhandled exception. System.ArgumentException: Could not convert argument 'Dotnet.Program+Producer' to Jsii (Parameter 'arguments')
at Amazon.JSII.Runtime.Deputy.DeputyBase.<>c__DisplayClass20_0.<ConvertArguments>b__0(Parameter parameter, Object frameworkArgument)
at System.Linq.Enumerable.ZipIterator[TFirst,TSecond,TResult](IEnumerable`1 first, IEnumerable`1 second, Func`3 resultSelector)+MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at Amazon.JSII.Runtime.Deputy.DeputyBase.ConvertArguments(Parameter[] parameters, Object[] arguments)
at Amazon.JSII.Runtime.Deputy.DeputyBase.<InvokeMethodCore>g__GetResult|18_0[T](<>c__DisplayClass18_0`1& )
at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeMethodCore[T](JsiiMethodAttribute methodAttribute, Object[] arguments, Func`3 beginFunc, Func`3 invokeFunc)
at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeStaticMethod[T](Type type, Type[] parameterTypes, Object[] arguments, String methodName)
at Amazon.CDK.Lazy.String(IStableStringProducer producer, ILazyStringValueOptions options)
at Dotnet.Program.Main(String[] args) in /Users/tempuser/dev/repros/cdk/issue29906_go/dotnet/src/Dotnet/Program.cs:line 14
Subprocess exited with error 134
There was an issue https://github.com/aws/jsii/issues/1781 for Python which was closed due to staleness.
I hit this bug with python and found a workaround. Taking the example from the CDK Guide:
class Producer:
def __init__(self, func):
self.produce = func
class FixedProducer:
__jsii_type__ = None
def __init__(self, func):
self.func = func
@jsii.member(jsii_name='produce')
def produce(self, context):
return self.func()
Maybe this can be of some use with Go as well?
Also an issue in Java:
package com.myorg;
import software.amazon.awscdk.IStableStringProducer;
import software.amazon.awscdk.Lazy;
import software.amazon.awscdk.services.sqs.Queue;
import software.constructs.Construct;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
public class CdkBugTestStack extends Stack {
public CdkBugTestStack(final Construct scope, final String id) {
this(scope, id, null);
}
public class LazyStableString implements IStableStringProducer {
@Override
public String produce() {
return "LazyStableCDKBugTestQueue";
}
}
public CdkBugTestStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Queue.Builder.create(this, "CdkBugTestQueue")
.queueName(Lazy.string(new LazyStableString()))
.build();
}
}
produces:
An exception occurred while executing the Java class. TypeError: Resolution error: Resolution error: Resolution error: Converting circular structure to JSON
--> starting at object with constructor 'CfnQueue'
| property 'node' -> object with constructor 'Node'
--- property 'host' closes the circle.
Switching to uncachedString works fine: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Lazy.html#static-uncachedwbrstringproducer-options
But this seems to be discouraged by the docs.
Describe the bug
This is probably specific to the Go CDK bindings, or it is also possible I am misunderstanding the use of the lazy string mechanism. When I try to use the
awscdk.Lazy_String
method for e.g. a log group name, synth fails due to a circular reference:See below for a very simple example that reproduces the issue.
Expected Behavior
Able to use the
awscdk.Lazy_String
function.Current Behavior
Synth fails when using the
awscdk.Lazy_String
function.Reproduction Steps
Complete example:
Then just run cdk synth, and it will fail.
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.137.0 (build bb90b4c)
Framework Version
No response
Node.js Version
18.18.2
OS
Linux
Language
Go
Language Version
go version go1.20.12 linux/amd64
Other information
No response