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.75k stars 186 forks source link

Wing Platform overrides do not work on extended classes #6632

Closed Chriscbr closed 3 weeks ago

Chriscbr commented 3 weeks ago

I tried this:

A Wing Platform provided by my team has special presets for the Terraform "S3Bucket" class. When I'm writing in Wing, the presets don't take effect if I extend the class instead of instantiating it directly, which is unexpected.

// main.w
bring "@cdktf/provider-aws" as aws;

pub class MyBucket extends aws.s3Bucket.S3Bucket {}

new MyBucket();
// my-platform.js
const aws = require("@cdktf/provider-aws");

exports.Platform = class MyPlatform {
  target = "tf-aws"
  newInstance(type, scope, id, props) {
    if (type === "@cdktf/provider-aws.s3Bucket.S3Bucket") {
      return new aws.s3Bucket.S3Bucket(scope, id, {
        ...props,
        bucketPrefix: "foo",
      })
    }
  }
}

This happened:

I ran wing compile -t tf-aws --platform ./my-platform.js main.w and I saw inside target/main.tfaws/main.tf.json there is an S3 bucket in the configuration, but it doesn't include the custom bucket prefix.

If I write "new aws.s3Bucket.S3Bucket();" then the Terraform output includes bucket_prefix: "foo" as expected.

I expected this:

See above

Is there a workaround?

No response

Anything else?

A possible solution could be to refactor the API of Wing Platforms so instead of directly constructing instances for you, a platform provides the types directly:

exports.Platform = class MyPlatform {
  target = "tf-aws";
  typeForFqn(fqn) {
    if (fqn === "@cdktf/provider-aws.s3Bucket.S3Bucket") {
      return class extends aws.s3Bucket.S3Bucket {
        constructor(scope, id, props) {
          super(scope, id, {
            ...props,
            bucketPrefix: "bar",
          })
        }
      }
    }
  }
}

Wing Version

0.74.12

Node.js Version

No response

Platform(s)

No response

Community Notes

Chriscbr commented 3 weeks ago

@hasanaburayyan I think we might have chatted about this bug before but I'm not sure if we had an issue to track it -- feel free to close if it's covered elsewhere.

Chriscbr commented 3 weeks ago

Ah I think this is a duplicate of https://github.com/winglang/wing/issues/6093