Файли
openidea/src/features/run/targetDebug.ts
T
LU XIANGRUIтаwildcatDownstairs 9b578c2fad feat(run): debug Node.js on SSH, Docker and Compose run targets (#6919)
Debug on a Node.js configuration or a .js / .ts file run whose run target is SSH, Docker or Compose now debugs the program there, extending the Python flow. A js-debug configuration starts node with --inspect-brk on a free port (0.0.0.0 inside the container, published on this machine's loopback only; the host's loopback over the existing SSH forward). The run prints the project folder on the target as a system line, which the frontend reads to map sources before attaching js-debug. tsx is refused for now (it hangs once js-debug attaches); other programs get an error naming the fix.

Closes #6901

Co-authored-by: wildcatDownstairs <[email protected]>

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-30 11:52:59 +08:00

154 рядки
6.2 KiB
TypeScript

/**
* Debugging Python and Node.js on run targets: the run starts the program under debugpy (the
* bootstrap of `ide_remote::docker`), or Node.js with `--inspect-brk`, in a new container, or on
* the SSH host with an SSH forward of the port. Once debugpy listens the bootstrap prints the
* port; Node prints its own `Debugger listening` line, after the IDE's line naming the project
* folder there. The debugger then attaches like Python Remote Debug or Attach to Node.js, with
* the project folder mapped. Output stays in the Run tab; stopping the debugger stops the program.
*/
import { executeAction } from "@/platform/actions";
import type { RunConfig } from "@/platform/ipc";
import { tx } from "@/platform/i18n";
import { notifications } from "@/workbench/contributions";
import { inContainer } from "@/platform/remoteSettings";
import { targetOf } from "./remote";
import { isLive, startRun, stopSession, useRun } from "./store";
/** The bootstrap's first line, once the program has started. */
const STARTING = "debugpy is starting";
/** The bootstrap's line (`ide_remote::docker::DEBUG_READY`). */
const READY = /^debugpy is waiting for the debugger on port (\d+), project folder (.+)$/;
/** The IDE's line before a Node.js debug run starts (`ide_remote::docker::NODE_DEBUG_FOLDER`). */
const NODE_FOLDER = "Starting Node.js for the debugger, project folder ";
/** Node's line once the inspector listens (`Debugger listening on ws://0.0.0.0:9229/<id>`). */
const INSPECTOR = /^Debugger listening on ws:\/\/(?:\[[^\]]*\]|[^/:]+):(\d+)\//;
interface Ready {
port: number;
remoteRoot: string;
}
/** Where the debugger attaches, from an output line of a container debug run. */
export function debugpyReady(line: string): Ready | null {
const m = READY.exec(line.trim());
return m ? { port: Number(m[1]), remoteRoot: m[2]! } : null;
}
/** The project folder on the target, from the IDE's line before a Node.js debug run. */
export function nodeDebugFolder(line: string): string | null {
const text = line.trim();
return text.startsWith(NODE_FOLDER) ? text.slice(NODE_FOLDER.length) : null;
}
/** The inspector's port, from Node's line once it listens. */
export function inspectorPort(line: string): number | null {
const m = INSPECTOR.exec(line.trim());
return m ? Number(m[1]) : null;
}
/** `config` as the backend runs it for debugging: under debugpy or with Node's inspector, waiting for the debugger. */
export function targetDebugLaunch(config: RunConfig): RunConfig {
return { ...config, params: { ...config.params, targetDebug: "true" } };
}
/** Runs `config` on its run target under debugpy (Node.js: with its inspector) and attaches the debugger. */
export async function debugOnTarget(config: RunConfig): Promise<void> {
const target = targetOf(config);
const container = !target || inContainer(target);
const node = config.debugAdapter === "js-debug";
let sessionId: string | null = null;
let ready: Ready | null = null;
let nodeFolder: string | null = null;
let done = false;
const pending: { timer?: ReturnType<typeof setTimeout>; unsubscribe?: () => void } = {};
const finish = () => {
done = true;
clearTimeout(pending.timer);
pending.unsubscribe?.();
};
const fail = (body: string) => {
finish();
notifications().notify({
group: "run",
level: "error",
title: container ? tx("Cannot start container debugging") : tx("Cannot start debugging on the SSH host"),
body,
});
};
const attach = ({ port, remoteRoot }: Ready) => {
if (done) return;
finish();
const runId = useRun.getState().sessions.find((s) => s.id === sessionId)?.runId ?? null;
void executeAction("debug.attach", {
name: config.name,
adapter: node ? "js-debug" : "debugpy",
hostName: "127.0.0.1",
port,
remoteRoot,
process: runId,
runConfig: config,
});
};
const startTimer = () => {
if (done || pending.timer) return;
pending.timer = setTimeout(() => {
fail(
node
? tx("The Node.js inspector did not become ready within 120 seconds. Check the Run console.")
: container
? tx(
"debugpy did not become ready within 120 seconds. Check the Run console and the container's Python interpreter.",
)
: tx(
"debugpy did not become ready within 120 seconds. Check the Run console and the SSH host's Python interpreter.",
),
);
if (sessionId) void stopSession(sessionId);
}, 120_000);
};
const session = await startRun(config, undefined, undefined, {
launch: targetDebugLaunch(config),
onLine: (line) => {
if (done) return;
// Over SSH the project and debugpy may be uploaded first, for as long as the link needs:
// the wait starts with the program.
if (!node && line.trim() === STARTING) startTimer();
const port = node && nodeFolder !== null ? inspectorPort(line) : null;
const parsed = node ? (port === null ? null : { port, remoteRoot: nodeFolder! }) : debugpyReady(line);
if (!parsed) return;
ready = parsed;
if (sessionId) attach(ready);
},
onSystemLine: (line) => {
const folder = node && !done ? nodeDebugFolder(line) : null;
if (folder === null) return;
nodeFolder = folder;
startTimer();
},
});
if (!session) return;
sessionId = session.id;
const check = () => {
const current = useRun.getState().sessions.find((s) => s.id === sessionId);
if (!current || current.stopped || current.status === "failed") finish();
else if (!isLive(current))
fail(
node
? container
? tx("The container exited before the Node.js inspector was ready. Check the Run console for errors.")
: tx("The program exited before the Node.js inspector was ready. Check the Run console for errors.")
: container
? tx("The container exited before debugpy was ready. Check the Run console for errors.")
: tx("The program exited before debugpy was ready. Check the Run console for errors."),
);
};
pending.unsubscribe = useRun.subscribe(check);
check();
if (done) return;
if (ready) return attach(ready);
if (container) startTimer();
}