Skip to content

Commit

Permalink
Merge branch 'main' into tsuf/limit-apis-amounts-in-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
monadabot authored Mar 19, 2024
2 parents 12507f2 + 8dfd1bc commit c7107b4
Show file tree
Hide file tree
Showing 77 changed files with 2,616 additions and 289 deletions.
1 change: 1 addition & 0 deletions apps/vscode-wing/.projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ vscodeIgnore.addPatterns(
);

const contributes: VSCodeExtensionContributions = {
breakpoints: [{ language: "wing" }],
languages: [
{
id: "wing",
Expand Down
5 changes: 5 additions & 0 deletions apps/vscode-wing/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions apps/wing/project-templates/wing/react-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"author": "Your Name",
"license": "MIT",
"scripts": {
"install:backend": "npm install --prefix backend",
"install:frontend": "npm install --prefix frontend",
"install:backend": "cd backend && npm install",
"install:frontend": "cd frontend && npm install",
"postinstall": "npm run install:backend && npm run install:frontend"
},
"dependencies": {
Expand Down
59 changes: 59 additions & 0 deletions docs/docs/02-concepts/01-preflight-and-inflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,65 @@ inflight () => {
};
```

### Lift qualification

Preflight objects referenced inflight are called "lifted" objects:

```js playground
let preflight_str = "hello from preflight";
inflight () => {
log(preflight_str); // `preflight_str` is "lifted" into inflight.
};
```

During the lifting process the compiler tries to figure out in what way the lifted objects are being used.
This is how Winglang generats least privilage permissions. Consider the case of lifting a [`cloud.Bucket`](../04-standard-library/cloud/bucket.md) object:

```js playground
bring cloud;
let bucket = new cloud.Bucket();
new cloud.Function(inflight () => {
bucket.put("key", "value"); // `bucket` is lifted and `put` is being used on it
});
```

In this example the compiler generates the correct _write_ access permissions for the [`cloud.Function`](../04-standard-library/cloud/function.md) on `bucket` based on the fact we're `put`ing into it. We say `bucket`'s lift is qualified with `put`.

#### Explicit lift qualification
In some cases the compiler can't figure out (yet) the lift qualifications, and therefore will report an error:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error: the compiler doesn't know the possible values for `b` and therefore can't qualify the lift.
});
```

To explicitly qualify lifts in an inflight closure or inflight method and supress the above compiler error use the `lift()` utility function:

```js playground
bring cloud;
let main_bucket = new cloud.Bucket() as "main";
let secondary_bucket = new cloud.Bucket() as "backup";
let use_main = true;
new cloud.Function(inflight () => {
lift(main_bucket, ["put"]); // Explicitly sate the "put" may be used on `main_bucket`
lift(secondary_bucket, ["put"]); // Explicitly sate the "put" may be used on `secondary_bucket`
let var b = main_bucket;
if !use_main {
b = secondary_bucket;
}
b.put("key", "value"); // Error is supressed and all possible values of `b` were explicitly qualified with "put"
});
```

## Phase-independent code

The global functions `log`, `assert`, and `throw` can all be used in both preflight and inflight code.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ log("UTC: {t1.utc.toIso())}"); // output: 2023-02-09T06:21:03.000Z
| `assert` | checks a condition and _throws_ if evaluated to false |
| `unsafeCast` | cast a value into a different type |
| `nodeof` | obtain the [tree node](./02-concepts/02-application-tree.md) of a preflight object |
| `lift` | explicitly qualify a [lift](./02-concepts/01-preflight-and-inflight.md#explicit-lift-qualification) of a preflight object |
> ```TS
> log("Hello {name}");
Expand Down
10 changes: 9 additions & 1 deletion docs/docs/04-standard-library/cloud/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,21 @@ Trigger events according to a cron schedule using the UNIX cron format.

Timezone is UTC.
[minute] [hour] [day of month] [month] [day of week]
'*' means all possible values.
'-' means a range of values.
',' means a list of values.
[minute] allows 0-59.
[hour] allows 0-23.
[day of month] allows 1-31.
[month] allows 1-12 or JAN-DEC.
[day of week] allows 0-6 or SUN-SAT.

---

*Example*

```wing
"0/1 * ? * *"
"* * * * *"
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,9 @@
"initial": {
"tf-aws": {
"implemented": true
},
"sim": {
"implemented": true
}
}
},
Expand Down
31 changes: 31 additions & 0 deletions docs/docs/06-tools/03-debugging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Debugging
id: Debugging
description: Learn how to debug your Wing application
keywords: [debugging, debug, test, vscode]
---

## Overview

Internally Wing uses JavaScript to execute preflight and inflight code, so standard JavaScript debugging tools can be used to debug your Wing application. The best-supported debugger is the built-in VS Code one so this guide will focus on that.

### Local/Simulator Debugging

To start, open your .w file in VS Code and set a breakpoint by clicking in the gutter to the left of the line number. Breakpoints can also be set in extern files. There are several ways to start the debugger, but let's use the "JavaScript Debug Terminal".
Open the command palette and type "Debug: Open JavaScript Debug Terminal". This works for any wing commands like `wing test` and `wing it`, although keep in mind that `wing compile` will only debug preflight code.

### Limitations

- ([Issue](https://github.com/winglang/wing/issues/5988)) When using the Wing Console (`wing it`) and attempting to debug inflight code in a `test` or Function, the first execution of the test will not hit a breakpoint and will need to be run again
- ([Issue](https://github.com/winglang/wing/issues/5986)) inflight code by default has a timeout that continues during debugging, so if execution is paused for too long the program is terminate
- Caught/Unhandled will often not stop at expected places

#### Non-VSCode Support

The Wing CLI itself is a Node.js application, so you can use the `--inspect` flag to debug it and expose a debug server.

```bash
node --inspect $(which wing)
```

Note that inflight code will be executed among multiple child processes, so it's recommended to use a debugger that supports automatically attaching to child processes.
40 changes: 40 additions & 0 deletions examples/tests/invalid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
bring cloud;

let bucket = new cloud.Bucket();

let prelight_string = "hi";

class Foo {
pub inflight mehtod1() {
let b = bucket;
lift(b, ["put"]); // Explicit qualification with inflight object, lift call as non first statement
// ^ Expected a preflight object as first argument to `lift` builtin, found inflight expression instead
//^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

lift(prelight_string, ["contains"]); // Explicit qualification on preflight non-class
// ^^^^^^^^^^^^^^^ Expected type to be "Resource", but got "str" instead
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inflight_qualifier = "delete";
lift(bucket, [inflight_qualifier]); // Explicit qualification with inflight qualifiers, lift call as non first statement
// ^^^^^^^^^^^^^^^^^^^^ Qualification list must not contain any inflight elements
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method

let inner_closure = () => {
lift(bucket, ["get"]); // lift() call in inner closure
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
};
class Bar {
pub inflight method() {
lift(bucket, ["get"]); // lift() call in inner class
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight
}
}
}

pub inflight method2() {
let b = bucket;
b.put("k", "v"); // With no explicit qualification this should be an error
//^ Expression of type "Bucket" references an unknown preflight object
}
}
8 changes: 0 additions & 8 deletions examples/tests/sdk_tests/schedule/init.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,4 @@ if (util.env("WING_TARGET") != "sim") {
error = e;
}
assert(error == "cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]");


try {
new cloud.Schedule( cron: "* * * * *" ) as "s5";
} catch e {
error = e;
}
assert(error == "cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other");
}
43 changes: 43 additions & 0 deletions examples/tests/valid/explicit_lift_qualification.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
bring cloud;

let bucket = new cloud.Bucket();
bucket.addObject("k", "value");

let put_and_list = ["put", "list"];

class Foo {
pub inflight mehtod() {
lift(bucket, put_and_list); // Qualify `bucket` with a preflight expression
lift(bucket, ["delete"]); // Qualify `bucket` with `delete` via literal
let b = bucket; // Assign `bucket` to an inflight variable

// `put` should work on `b` since we explicitly qualified `bucket` with `put`
// no error generated here because of use of `lift()` in this method
b.put("k2", "value2");

// validate `put` worked and that we can also `list`
assert(b.list() == ["k", "k2"]);

// Validate `delete` works
b.delete("k2");
assert(bucket.tryGet("k2") == nil);
}
}

let foo = new Foo();

test "explicit method lift qualification" {
foo.mehtod();
}

// Similar to the above test, but using a closure
let inflight_closure = inflight () => {
lift(bucket, ["put"]);
let b = bucket;
b.put("k3", "value3"); // Use inflight expression to access explicitly qualified `bucket`
assert(bucket.get("k3") == "value3");
};

test "explicit closure lift qualification" {
inflight_closure();
}
5 changes: 3 additions & 2 deletions examples/tests/valid/optionals.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class Super {
class Sub extends Super {
new() { this.name = "Sub"; }
}
class Sub1 extends Super {
new() { this.name = "Sub"; }
class SubSub extends Sub {
new() { this.name = "SubSub"; }
}

let optionalSup: Super? = new Super();
let s = optionalSup ?? new Sub();
assert(s.name == "Super");
let s2 = optionalSup ?? optionalSup ?? new SubSub();

struct Name {
first: str;
Expand Down
30 changes: 10 additions & 20 deletions libs/awscdk/src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { Construct } from "constructs";
import { App } from "./app";
import { cloud, core, std } from "@winglang/sdk";
import { convertBetweenHandlers } from "@winglang/sdk/lib/shared/convert";
import { convertUnixCronToAWSCron } from "@winglang/sdk/lib/shared-aws/schedule";
import { isAwsCdkFunction } from "./function";


/**
* AWS implementation of `cloud.Schedule`.
*
Expand All @@ -25,27 +27,15 @@ export class Schedule extends cloud.Schedule {

const { rate, cron } = props;

/*
* The schedule cron string is Unix cron format: [minute] [hour] [day of month] [month] [day of week]
* AWS EventBridge Schedule uses a 6 field format which includes year: [minute] [hour] [day of month] [month] [day of week] [year]
* https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#cron-based
*
* We append * to the cron string for year field.
*/
if (cron) {
const cronArr = cron.split(" ");
let cronOpt: { [k: string]: string } = {
minute: cronArr[0],
hour: cronArr[1],
month: cronArr[3],
year: "*",
};
if (cronArr[2] !== "?") {
cronOpt.day = cronArr[2];
}
if (cronArr[4] !== "?") {
cronOpt.weekDay = cronArr[4];
}
let cronOpt: { [k: string]: string } = {};
const awsCron = convertUnixCronToAWSCron(cron);
const cronArr = awsCron.split(" ");
if (cronArr[0] !== "*" && cronArr[0] !== "?") { cronOpt.minute = cronArr[0]; }
if (cronArr[1] !== "*" && cronArr[1] !== "?") { cronOpt.hour = cronArr[1]; }
if (cronArr[2] !== "*" && cronArr[2] !== "?") { cronOpt.day = cronArr[2]; }
if (cronArr[3] !== "*" && cronArr[3] !== "?") { cronOpt.month = cronArr[3]; }
if (cronArr[4] !== "*" && cronArr[4] !== "?") { cronOpt.weekDay = cronArr[4]; }

this.scheduleExpression = EventSchedule.cron(cronOpt);
} else {
Expand Down
Loading

0 comments on commit c7107b4

Please sign in to comment.