Skip to content

Commit ac08c41

Browse files
rentziassCopilot
andauthored
Add 'Connect to Actions Job Debugger' command
This is the first step toward bringing the Actions job debugging experience into VS Code. The full vision is described in the ADR (c2c-actions/docs/adrs/9758-actions-job-debugger-mvp.md): users will be able to re-run a job with a debugger attached and connect their editor to inspect variables, evaluate expressions, and run commands in the job's runtime context — all through the standard Debug Adapter Protocol. This PR adds the extension-side connect flow: - A new command ('GitHub Actions: Connect to Actions Job Debugger...') that prompts for a Dev Tunnel wss:// URL and starts a debug session. - A WebSocket-based inline debug adapter (DebugAdapterInlineImplementation) that speaks DAP-over-WebSocket directly to the runner — no local TCP bridge needed. - Authentication using the existing VS Code GitHub session token, sent as a Bearer token on the WebSocket handshake. The tunnel URL is entered manually for now. Once the server-side endpoint that serves the debugger URL for a job is deployed, we can automate this (and eventually add 're-run with debugger' directly from the extension). Security hardening from code review: - Tunnel URLs are restricted to wss://*.devtunnels.ms (no cleartext, no arbitrary hosts receiving the GitHub token). - Auth tokens are kept in extension-private memory, never exposed through DebugConfiguration. - 30s connection timeout prevents indefinite hangs. - WebSocket send errors trigger clean session teardown. Workarounds for VS Code quirks (to be removed as the runner evolves): - Synthetic source references on stack frames so VS Code auto-focuses the top frame and loads variables on connect/step. - Buffering of early 'stopped' events that the runner sends before VS Code completes the DAP handshake. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 53f7bbe commit ac08c41

File tree

8 files changed

+650
-2
lines changed

8 files changed

+650
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# GitHub Actions for VS Code
22

33
> **🐛 Actions Job Debugger (Preview):** To try the latest debugger build, download the `.vsix` artifact from the most recent [Build Debugger Extension](https://github.com/github/vscode-github-actions/actions/workflows/debugger-build.yml) workflow run. On the workflow run page, scroll to **Artifacts** and download **vscode-github-actions-debugger**. Then install it in VS Code by running `code --install-extension <path-to-downloaded.vsix>` or via the Extensions view → `` menu → **Install from VSIX…**.
4+
>
5+
> Once installed, open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) and run **GitHub Actions: Connect to Actions Job Debugger…**. Paste the `wss://` tunnel URL from a debug-mode job and the extension will open a full debug session using your current GitHub credentials.
46
57
The GitHub Actions extension lets you manage your workflows, view the workflow run history, and helps with authoring workflows.
68

package-lock.json

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"activationEvents": [
2626
"onView:workflows",
2727
"onView:settings",
28+
"onDebugResolve:github-actions-job",
2829
"workspaceContains:**/.github/workflows/**",
2930
"workspaceContains:**/action.yml",
3031
"workspaceContains:**/action.yaml"
@@ -97,7 +98,19 @@
9798
}
9899
}
99100
},
101+
"debuggers": [
102+
{
103+
"type": "github-actions-job",
104+
"label": "GitHub Actions Job Debugger",
105+
"languages": []
106+
}
107+
],
100108
"commands": [
109+
{
110+
"command": "github-actions.debugger.connect",
111+
"category": "GitHub Actions",
112+
"title": "Connect to Actions Job Debugger..."
113+
},
101114
{
102115
"command": "github-actions.explorer.refresh",
103116
"category": "GitHub Actions",
@@ -544,6 +557,7 @@
544557
"@types/libsodium-wrappers": "^0.7.10",
545558
"@types/uuid": "^3.4.6",
546559
"@types/vscode": "^1.72.0",
560+
"@types/ws": "^8.18.1",
547561
"@typescript-eslint/eslint-plugin": "^5.40.0",
548562
"@typescript-eslint/parser": "^5.40.0",
549563
"@vscode/test-web": "^0.0.69",
@@ -579,7 +593,8 @@
579593
"tunnel": "0.0.6",
580594
"util": "^0.12.1",
581595
"uuid": "^3.3.3",
582-
"vscode-languageclient": "^8.0.2"
596+
"vscode-languageclient": "^8.0.2",
597+
"ws": "^8.20.0"
583598
},
584599
"overrides": {
585600
"browserify-sign": {

src/debugger/debugger.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import * as vscode from "vscode";
2+
import {getSession} from "../auth/auth";
3+
import {log, logDebug, logError} from "../log";
4+
import {validateTunnelUrl} from "./tunnelUrl";
5+
import {WebSocketDapAdapter} from "./webSocketDapAdapter";
6+
7+
/** The custom debug type registered in package.json contributes.debuggers. */
8+
export const DEBUG_TYPE = "github-actions-job";
9+
10+
/**
11+
* Extension-private store for auth tokens, keyed by a one-time session
12+
* nonce. Tokens are never placed in DebugConfiguration (which is readable
13+
* by other extensions via vscode.debug.activeDebugSession.configuration).
14+
*/
15+
const pendingTokens = new Map<string, string>();
16+
let nextNonce = 0;
17+
18+
/**
19+
* Registers the Actions Job Debugger command and debug adapter factory.
20+
*
21+
* Contributes:
22+
* - A command-palette command that prompts for a tunnel URL and starts a debug session.
23+
* - A DebugAdapterDescriptorFactory that returns an inline DAP-over-WS adapter.
24+
*/
25+
export function registerDebugger(context: vscode.ExtensionContext): void {
26+
// Register the inline adapter factory for our debug type.
27+
context.subscriptions.push(
28+
vscode.debug.registerDebugAdapterDescriptorFactory(DEBUG_TYPE, new ActionsDebugAdapterFactory())
29+
);
30+
31+
// Register a tracker to log all DAP traffic for diagnostics.
32+
context.subscriptions.push(
33+
vscode.debug.registerDebugAdapterTrackerFactory(DEBUG_TYPE, new ActionsDebugTrackerFactory())
34+
);
35+
36+
// Register the connect command.
37+
context.subscriptions.push(
38+
vscode.commands.registerCommand("github-actions.debugger.connect", () => connectToDebugger())
39+
);
40+
}
41+
42+
// ── Connect command ──────────────────────────────────────────────────
43+
44+
async function connectToDebugger(): Promise<void> {
45+
// 1. Prompt for the tunnel URL.
46+
const rawUrl = await vscode.window.showInputBox({
47+
title: "Connect to Actions Job Debugger",
48+
prompt: "Enter the debugger tunnel URL (wss://…)",
49+
placeHolder: "wss://xxxx-4711.region.devtunnels.ms/",
50+
ignoreFocusOut: true,
51+
validateInput: input => {
52+
if (!input) {
53+
return "A tunnel URL is required";
54+
}
55+
const result = validateTunnelUrl(input);
56+
return result.valid ? null : result.reason;
57+
}
58+
});
59+
60+
if (!rawUrl) {
61+
return; // user cancelled
62+
}
63+
64+
const validation = validateTunnelUrl(rawUrl);
65+
if (!validation.valid) {
66+
void vscode.window.showErrorMessage(`Invalid tunnel URL: ${validation.reason}`);
67+
return;
68+
}
69+
70+
// 2. Acquire a GitHub auth session. The token is used as a Bearer token
71+
// against the Dev Tunnel relay, which accepts VS Code's GitHub app tokens.
72+
// getSession() silently reuses the existing session; it only prompts if
73+
// the user has never signed in.
74+
const session = await getSession();
75+
if (!session) {
76+
void vscode.window.showErrorMessage(
77+
"GitHub authentication is required to connect to the Actions job debugger. Please sign in and try again."
78+
);
79+
return;
80+
}
81+
82+
// 3. Launch the debug session. The token is stored in extension-private
83+
// memory (not in the configuration) to avoid exposing it to other extensions.
84+
const nonce = String(++nextNonce);
85+
pendingTokens.set(nonce, session.accessToken);
86+
87+
const config: vscode.DebugConfiguration = {
88+
type: DEBUG_TYPE,
89+
name: "Actions Job Debugger",
90+
request: "attach",
91+
tunnelUrl: validation.url,
92+
__tokenNonce: nonce
93+
};
94+
95+
log(`Starting debug session for ${validation.url}`);
96+
97+
const started = await vscode.debug.startDebugging(undefined, config);
98+
if (!started) {
99+
void vscode.window.showErrorMessage("Failed to start the debug session. Check the GitHub Actions output for details.");
100+
}
101+
}
102+
103+
// ── Debug adapter factory ────────────────────────────────────────────
104+
105+
class ActionsDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
106+
async createDebugAdapterDescriptor(
107+
session: vscode.DebugSession
108+
): Promise<vscode.DebugAdapterDescriptor> {
109+
const tunnelUrl = session.configuration.tunnelUrl as string | undefined;
110+
const nonce = session.configuration.__tokenNonce as string | undefined;
111+
const token = nonce ? pendingTokens.get(nonce) : undefined;
112+
113+
// Consume the token immediately so it cannot be replayed.
114+
if (nonce) {
115+
pendingTokens.delete(nonce);
116+
}
117+
118+
if (!tunnelUrl || !token) {
119+
throw new Error(
120+
"Missing tunnel URL or authentication token. Use the 'Connect to Actions Job Debugger' command to start a session."
121+
);
122+
}
123+
124+
const adapter = new WebSocketDapAdapter(tunnelUrl, token);
125+
126+
try {
127+
await adapter.connect();
128+
} catch (e) {
129+
adapter.dispose();
130+
const msg = (e as Error).message;
131+
logError(e as Error, "Failed to connect debugger adapter");
132+
throw new Error(`Could not connect to the debugger tunnel: ${msg}`);
133+
}
134+
135+
return new vscode.DebugAdapterInlineImplementation(adapter);
136+
}
137+
}
138+
139+
// ── Debug adapter tracker (diagnostics) ──────────────────────────────
140+
141+
class ActionsDebugTrackerFactory implements vscode.DebugAdapterTrackerFactory {
142+
createDebugAdapterTracker(): vscode.DebugAdapterTracker {
143+
return {
144+
onWillReceiveMessage(message: unknown) {
145+
const m = message as Record<string, unknown>;
146+
logDebug(`[tracker] VS Code → DA: ${String(m.type)}${m.command ? `:${String(m.command)}` : ""} (seq ${String(m.seq)})`);
147+
},
148+
onDidSendMessage(message: unknown) {
149+
const m = message as Record<string, unknown>;
150+
const body = m.body as Record<string, unknown> | undefined;
151+
let detail = String(m.type);
152+
if (m.command) {
153+
detail += `:${String(m.command)}`;
154+
}
155+
if (m.event) {
156+
detail += `:${String(m.event)}`;
157+
}
158+
if (m.event === "stopped" && body) {
159+
detail += ` threadId=${String(body.threadId)} allThreadsStopped=${String(body.allThreadsStopped)}`;
160+
}
161+
logDebug(`[tracker] DA → VS Code: ${detail} (seq ${String(m.seq)})`);
162+
},
163+
onError(error: Error) {
164+
logError(error, "[tracker] DAP error");
165+
},
166+
onExit(code: number | undefined, signal: string | undefined) {
167+
log(`[tracker] DAP session exited: code=${String(code)} signal=${String(signal)}`);
168+
}
169+
};
170+
}
171+
}

src/debugger/tunnelUrl.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {validateTunnelUrl} from "./tunnelUrl";
2+
3+
describe("validateTunnelUrl", () => {
4+
it("accepts a valid wss:// devtunnels URL", () => {
5+
const result = validateTunnelUrl("wss://abcdef-4711.uks1.devtunnels.ms/");
6+
expect(result).toEqual({valid: true, url: "wss://abcdef-4711.uks1.devtunnels.ms/"});
7+
});
8+
9+
it("accepts a devtunnels URL without trailing slash", () => {
10+
const result = validateTunnelUrl("wss://abcdef-4711.uks1.devtunnels.ms");
11+
expect(result.valid).toBe(true);
12+
});
13+
14+
it("accepts a devtunnels URL with a path", () => {
15+
const result = validateTunnelUrl("wss://abcdef-4711.uks1.devtunnels.ms/connect");
16+
expect(result.valid).toBe(true);
17+
});
18+
19+
it("rejects ws:// (cleartext)", () => {
20+
const result = validateTunnelUrl("ws://abcdef-4711.uks1.devtunnels.ms/");
21+
expect(result.valid).toBe(false);
22+
if (!result.valid) {
23+
expect(result.reason).toContain("wss://");
24+
}
25+
});
26+
27+
it("rejects http:// scheme", () => {
28+
const result = validateTunnelUrl("http://abcdef-4711.uks1.devtunnels.ms/");
29+
expect(result.valid).toBe(false);
30+
if (!result.valid) {
31+
expect(result.reason).toContain("wss://");
32+
}
33+
});
34+
35+
it("rejects https:// scheme", () => {
36+
const result = validateTunnelUrl("https://abcdef-4711.uks1.devtunnels.ms/");
37+
expect(result.valid).toBe(false);
38+
if (!result.valid) {
39+
expect(result.reason).toContain("wss://");
40+
}
41+
});
42+
43+
it("rejects non-devtunnels host", () => {
44+
const result = validateTunnelUrl("wss://evil.example.com/");
45+
expect(result.valid).toBe(false);
46+
if (!result.valid) {
47+
expect(result.reason).toContain("not an allowed tunnel domain");
48+
}
49+
});
50+
51+
it("rejects empty string", () => {
52+
const result = validateTunnelUrl("");
53+
expect(result.valid).toBe(false);
54+
});
55+
56+
it("rejects invalid URL format", () => {
57+
const result = validateTunnelUrl("not a url at all");
58+
expect(result.valid).toBe(false);
59+
if (!result.valid) {
60+
expect(result.reason).toContain("Invalid URL");
61+
}
62+
});
63+
64+
it("rejects URL with just a scheme", () => {
65+
const result = validateTunnelUrl("wss://");
66+
expect(result.valid).toBe(false);
67+
});
68+
});

0 commit comments

Comments
 (0)