winglang / winglang/wing

Unable to pass methods (static or instance) as closures

Open
#3,250 8 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🐛 bug 🛠️ compiler
Dominant language
TypeScript
Stars
5.4k
Forks
215
Avg merge
2h 9m
Merged PRs (30d)
27

Description

I tried this:

Given this starter code:

bring cloud;

let api = new cloud.Api();

api.get("/", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
  return cloud.ApiResponse {
    status: 200,
    body: "Hello, world!"
  };
});

I moved the controller logic into a static method of a class:

inflight class RequestController {
  static inflight handleRequest(request: cloud.ApiRequest): cloud.ApiResponse {
    return cloud.ApiResponse {
      status: 200,
      body: "Hello, world!"
    };
  }
}

And then tried to use that static method to handle requests:

api.get("/", RequestController.handleRequest);
This happened:

The following error was thrown during compilation:

ERROR: Cannot read properties of undefined (reading 'node')

target/gdpr.wsim.233847.tmp/.wing/preflight.js:44
       }
       const api = this.node.root.newAbstract("@winglang/sdk.cloud.Api",this,"cloud.Api");
>>     (api.get("/",RequestController.handleRequest));
     }
   }
I expected this:

I expected the console to compile and requests to be handled successfully.

Is there a workaround?

Don't use class methods for handling API requests, and define route controllers using anonymous inflight functions (like in the original code sample).

Component

Compiler

Wing Version

0.23.17

Wing Console Version

No response

Node.js Version

v20.3.1

Platform(s)

MacOS

Anything else?
Community Notes
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground reproduction and the compiler component described in the issue. Trace how RequestController.handleRequest is compiled when passed to api.get, comparing it with the anonymous inflight function example. Done means static and instance methods can be passed as closures, the reproduction compiles, and requests are handled successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.