winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.98k stars 196 forks source link

Accessing preflight fields from jsii libraries doesn't work #2001

Closed Chriscbr closed 1 year ago

Chriscbr commented 1 year ago

Community Note

Please vote by adding a 👍 reaction to the issue to help us prioritize. If you are interested to work on this issue, please leave a comment.

I tried this:

bring cloud;
bring "@cdktf/provider-aws" as aws;

let bucket = new aws.s3Bucket.S3Bucket(
  bucket_prefix: "hello-"
);

new cloud.Function(inflight () => {
  log(bucket.bucket_prefix);
});

Then I ran wing compile -t tf-aws jsii_access.w

I expected this:

Successful compile

Instead, this happened:

preflight error: unable to serialize immutable data object of type S3Bucket

  note: intermediate javascript code (/Users/chrisr/dev/wing-test/target/jsii_access.tfaws.534790.tmp/.wing/preflight.js):
       super(scope, id);
       const bucket = this.node.root.new("@cdktf/provider-aws.s3Bucket.S3Bucket",aws.s3Bucket.S3Bucket,this,"aws.s3Bucket.S3Bucket",{ bucketPrefix: "hello-" });
>>     this.node.root.newAbstract("@winglang/sdk.cloud.Function",this,"cloud.Function",new $stdlib.core.Inflight(this, "$Inflight1", {
     code: $stdlib.core.NodeJsCode.fromFile(require.resolve("./proc.d3341071d1c049bbd3c165b06f8f2ff5f128cbeac444359dc4cb45d22f0c88a2/index.js".replace(/\\/g, "/"))),
     bindings: {
  note: run with `NODE_STACKTRACE=1` environment variable to display a stack trace

Is there a workaround?

One workaround I tried was to assign the value to a variable first:

bring cloud;
bring "@cdktf/provider-aws" as aws;

let bucket = new aws.s3Bucket.S3Bucket(
  bucket_prefix: "hello-"
);

let foo = bucket.bucket_prefix;

new cloud.Function(inflight () => {
  log(foo);
});

But right now it looks like this raises a different error:

preflight error: Cannot read properties of undefined (reading 'constructor')

  note: intermediate javascript code (/Users/chrisr/dev/wing-test/target/jsii_access.tfaws.712983.tmp/.wing/preflight.js):
       const bucket = this.node.root.new("@cdktf/provider-aws.s3Bucket.S3Bucket",aws.s3Bucket.S3Bucket,this,"aws.s3Bucket.S3Bucket",{ bucketPrefix: "hello-" });
       const foo = bucket.bucket_prefix;
>>     this.node.root.newAbstract("@winglang/sdk.cloud.Function",this,"cloud.Function",new $stdlib.core.Inflight(this, "$Inflight1", {
     code: $stdlib.core.NodeJsCode.fromFile(require.resolve("./proc.cfbaeab6a8bb32438d1e09ba99b319c68bf197d6432d61cf5fbd6f66c9a25930/index.js".replace(/\\/g, "/"))),
     bindings: {

which I think is a result of how bucket.bucket_prefix actually returns an object under the hood that behaves like a string instead of a plain JavaScript string

Component:

Compiler

Wing Version:

0.8.38

Node.js Version:

18.12.1

Platform(s):

MacOS

Anything else?

I think part of the issue could be that these imported resources do not have _toInflight methods so they cannot return an inflight client containing preflight properties.

staycoolcall911 commented 1 year ago

Duplicate of #1878

Chriscbr commented 1 year ago

Oops thanks. @staycoolcall911 Perhaps we need some kind of label or "tracking issue" for all inflight or capturing related bugs/enhancements to make them easier to find?

staycoolcall911 commented 1 year ago

Yeah, either suggestions will make it easier to find these issues, but I'm not sure about the overhead. Let's chat about it today and perhaps revisit our approach if we see it happening often.