-
Notifications
You must be signed in to change notification settings - Fork 1k
Initial support for VSCode Javascript Debug Terminals #9535
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ab1b79b
Initial support for VSCode Javascript Debug Terminals
penalosa f094afc
turbo env
penalosa 5565065
type tests
penalosa be20890
Create perfect-plants-compete.md
penalosa 407d951
reset fixtures
penalosa 28d929e
Add comments
penalosa 09fcbba
Merge remote-tracking branch 'origin/main' into penalosa/js-debug-ter…
penalosa 851c71e
fix format
penalosa c655618
fixups
penalosa bac2fc5
Merge remote-tracking branch 'origin/main' into penalosa/js-debug-ter…
penalosa 21403c3
bump timeout
penalosa 30ad19b
loosen stack trace matching
penalosa f86dd6e
Support Vite
penalosa ba17331
fix sentry test
penalosa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"miniflare": patch | ||
"wrangler": patch | ||
--- | ||
|
||
In 2023 we announced [breakpoint debugging support](https://blog.cloudflare.com/debugging-cloudflare-workers/) for Workers, which meant that you could easily debug your Worker code in Wrangler's built-in devtools (accessible via the `[d]` hotkey) as well as multiple other devtools clients, [including VSCode](https://developers.cloudflare.com/workers/observability/dev-tools/breakpoints/). For most developers, breakpoint debugging via VSCode is the most natural flow, but until now it's required [manually configuring a `launch.json` file](https://developers.cloudflare.com/workers/observability/dev-tools/breakpoints/#setup-vs-code-to-use-breakpoints), running `wrangler dev`, and connecting via VSCode's built-in debugger. | ||
|
||
Now, using VSCode's built-in [JavaScript Debug Terminals](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_javascript-debug-terminal), there are just two steps: open a JS debug terminal and run `wrangler dev` (or `vite dev`). VSCode will automatically connect to your running Worker (even if you're running multiple Workers at once!) and start a debugging session. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import assert from "assert"; | ||
import childProcess from "child_process"; | ||
import childProcess, { spawn } from "child_process"; | ||
import { randomBytes } from "crypto"; | ||
import { Abortable, once } from "events"; | ||
import path from "path"; | ||
import rl from "readline"; | ||
import { Readable } from "stream"; | ||
import { $ as $colors, red } from "kleur/colors"; | ||
|
@@ -119,13 +121,40 @@ function getRuntimeArgs(options: RuntimeOptions) { | |
return args; | ||
} | ||
|
||
/** | ||
* Copied from https://github.com/microsoft/vscode-js-debug/blob/0b5e0dade997b3c702a98e1f58989afcb30612d6/src/targets/node/bootloader/environment.ts#L129 | ||
* | ||
* This function returns the segment of process.env.VSCODE_INSPECTOR_OPTIONS that corresponds to the current process (rather than a parent process) | ||
*/ | ||
function getInspectorOptions() { | ||
const value = process.env.VSCODE_INSPECTOR_OPTIONS; | ||
if (!value) { | ||
return undefined; | ||
} | ||
|
||
const ownOptions = value | ||
.split(":::") | ||
.reverse() | ||
.find((v) => !!v); | ||
if (!ownOptions) { | ||
return; | ||
} | ||
|
||
try { | ||
return JSON.parse(ownOptions); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
export class Runtime { | ||
#process?: childProcess.ChildProcess; | ||
#processExitPromise?: Promise<void>; | ||
|
||
async updateConfig( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add JSDoc? |
||
configBuffer: Buffer, | ||
options: Abortable & RuntimeOptions | ||
options: Abortable & RuntimeOptions, | ||
workerNames: string[] | ||
): Promise<SocketPorts | undefined> { | ||
// 1. Stop existing process (if any) and wait for exit | ||
await this.dispose(); | ||
|
@@ -156,7 +185,46 @@ export class Runtime { | |
await once(runtimeProcess.stdin, "finish"); | ||
|
||
// 4. Wait for sockets to start listening | ||
return waitForPorts(controlPipe, options); | ||
const ports = await waitForPorts(controlPipe, options); | ||
if (ports?.has(kInspectorSocket) && process.env.VSCODE_INSPECTOR_OPTIONS) { | ||
// We have an inspector socket and we're in a VSCode Debug Terminal. | ||
// Let's startup a watchdog service to register ourselves as a debuggable target | ||
|
||
// First, we need to _find_ the watchdog script. It's located next to bootloader.js, which should be injected as a require hook | ||
const bootloaderPath = | ||
process.env.NODE_OPTIONS?.match(/--require "(.*?)"/)?.[1]; | ||
|
||
if (!bootloaderPath) { | ||
return ports; | ||
} | ||
const watchdogPath = path.resolve(bootloaderPath, "../watchdog.js"); | ||
|
||
const info = getInspectorOptions(); | ||
|
||
for (const name of workerNames) { | ||
// This is copied from https://github.com/microsoft/vscode-js-debug/blob/0b5e0dade997b3c702a98e1f58989afcb30612d6/src/targets/node/bootloader.ts#L284 | ||
// It spawns a detached "watchdog" process for each corresponding (user) Worker in workerd which will maintain the VSCode debug connection | ||
penalosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const p = spawn(process.execPath, [watchdogPath], { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment on what the code is doing here? |
||
env: { | ||
NODE_INSPECTOR_INFO: JSON.stringify({ | ||
ipcAddress: info.inspectorIpc || "", | ||
pid: String(this.#process.pid), | ||
scriptName: name, | ||
inspectorURL: `ws://127.0.0.1:${ports?.get(kInspectorSocket)}/core:user:${name}`, | ||
waitForDebugger: true, | ||
ownId: randomBytes(12).toString("hex"), | ||
openerId: info.openerId, | ||
penalosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
NODE_SKIP_PLATFORM_CHECK: process.env.NODE_SKIP_PLATFORM_CHECK, | ||
ELECTRON_RUN_AS_NODE: "1", | ||
}, | ||
stdio: "ignore", | ||
detached: true, | ||
}); | ||
p.unref(); | ||
} | ||
} | ||
return ports; | ||
} | ||
|
||
dispose(): Awaitable<void> { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add JSDoc?