Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vscode): improved wing debugging and docs (breakpoints) #5981

Merged
merged 9 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }],
MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved
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.

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

MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved
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.
MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved

### 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

- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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
- 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

I don't see a reason to quote inflight every time. It's a term we use, not a symbol.

Copy link
Contributor Author

@MarkMcCulloh MarkMcCulloh Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use inflight over "inflight" pretty often because it's a keyword. I would do the same with let or class. Should inflight specifically not use "`" or should other keywords also not get backticks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use backticks when we refer to the keyword and no backticks when we refer to the concept or the term.

For example:

  • "Let's declare a class that represents an employee."

  • "To declare a data shape use the struct keyword followed by the name of the struct"

  • "This value is a constant"

Makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, the keyword is the concept and vice-versa. It helps my brain establish the link between the two, so that when I see the word in code I'll think of the concept and when I read the word in docs I'll think about how it's used in code.

I don't get confused when I see alternating struct vs "struct" so always using backticks isn't crazy important to me, but I'm not sold on the reasoning to avoid them so far.

The only reason I do get is aesthetics, because keywords can be pretty ugly when overused. So if that's part of our "Style Guide" then ok.

MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved
- Caught/Unhandled will often not stop at expected places
- `inflight` code by default has a timeout that continues during debugging, so if execution is paused for too long the program is terminate
MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved

#### 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 than supports automatically attaching to child processes.
MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 2 additions & 5 deletions libs/wingsdk/src/shared/legacy-sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { mkdtemp, readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { readFile } from "node:fs/promises";
import * as util from "node:util";
import * as vm from "node:vm";
import { createBundle } from "./bundling";
Expand Down Expand Up @@ -90,8 +88,7 @@ export class LegacySandbox {

private async createBundle() {
// load bundle into context on first run
const workdir = await mkdtemp(path.join(tmpdir(), "wing-bundles-"));
const bundle = createBundle(this.entrypoint, [], workdir);
const bundle = createBundle(this.entrypoint);
this.entrypoint = bundle.entrypointPath;

this.code = await readFile(this.entrypoint, "utf-8");
Expand Down
8 changes: 2 additions & 6 deletions libs/wingsdk/src/shared/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as cp from "child_process";
import { writeFileSync } from "fs";
import { mkdtemp, readFile, stat } from "fs/promises";
import { tmpdir } from "os";
import path from "path";
import { readFile, stat } from "fs/promises";
import { Bundle, createBundle } from "./bundling";
import { processStream } from "./stream-processor";

Expand Down Expand Up @@ -39,8 +37,6 @@ export class Sandbox {
entrypoint: string,
log?: (message: string) => void
): Promise<Bundle> {
const workdir = await mkdtemp(path.join(tmpdir(), "wing-bundles-"));

let contents = (await readFile(entrypoint)).toString();

// log a warning if contents includes __dirname or __filename
Expand All @@ -67,7 +63,7 @@ process.on("message", async (message) => {
`;
const wrappedPath = entrypoint.replace(/\.js$/, ".sandbox.js");
writeFileSync(wrappedPath, contents); // async fsPromises.writeFile "flush" option is not available in Node 20
const bundle = createBundle(wrappedPath, [], workdir);
const bundle = createBundle(wrappedPath);

if (process.env.DEBUG) {
const fileStats = await stat(entrypoint);
Expand Down
12 changes: 9 additions & 3 deletions libs/wingsdk/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ export function directorySnapshot(initialRoot: string) {
if (f === "node_modules") {
continue;
}
// skip sandbox entrypoints since they are mostly a duplicate of the original
if (f.endsWith(".sandbox.js")) {
continue;
}
// skip esbuild output
if (f.endsWith(".js.bundle")) {
continue;
}

const relpath = join(subdir, f);
const abspath = join(root, relpath);
const key = prefix + relpath;
Expand All @@ -149,9 +158,6 @@ export function directorySnapshot(initialRoot: string) {
break;

case ".js":
if (f.endsWith(".sandbox.js")) {
continue;
}
const code = readFileSync(abspath, "utf-8");
snapshot[key] = sanitizeCode(code);
break;
Expand Down
Loading