дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
PHP configurations that run in a Docker or Compose run target can be debugged. The debugger listens first on a free port with the container's mounts mapped (run_container_xdebug), then the run starts with XDEBUG_MODE=debug, XDEBUG_SESSION=1 and XDEBUG_CONFIG pointing at host.docker.internal (docker run gets --add-host host-gateway); the debug session is linked to the run and stops with it. Install and Debug after installing php-debug repeats the whole flow. PHP on SSH targets stays refused. Refs #4582 Co-authored-by: samehelhawary <[email protected]>
230 рядки
10 KiB
TypeScript
230 рядки
10 KiB
TypeScript
/**
|
||
* Run targets (Settings › Run Targets): configurations with a `target` run over
|
||
* SSH, in a Docker / Compose container or in a WSL distribution. Labels, the debug and executor guards,
|
||
* and the connection remedies of `runStart`.
|
||
*/
|
||
import { withRunTarget } from "@/platform/runTargetSelection";
|
||
import { useRunTargets, runTargets as allRunTargets, type RuntimeRunTarget } from "@/platform/pluginRunTargets";
|
||
import { executeAction } from "@/platform/actions";
|
||
import { tx, txTemplate } from "@/platform/i18n";
|
||
import { call, commands, IpcError, type RunConfig } from "@/platform/ipc";
|
||
import { inContainer, type SshConfigValid } from "@/platform/remoteSettings";
|
||
import { validateLanguages } from "@/platform/languageSettings";
|
||
import { useProject } from "@/platform/project";
|
||
import { workflowSettings } from "@/platform/workflowSettings";
|
||
import { notifications } from "@/workbench/contributions";
|
||
import { confirm } from "@/workbench/messages";
|
||
import { promptSecret } from "./dialogs";
|
||
|
||
/** The run target `config` runs on; undefined when it runs locally or the target is gone. */
|
||
export function targetOf(
|
||
config: Pick<RunConfig, "target">,
|
||
targets: readonly RuntimeRunTarget[] = allRunTargets(),
|
||
): RuntimeRunTarget | undefined {
|
||
return config.target ? targets.find((t) => t.id === config.target) : undefined;
|
||
}
|
||
|
||
/** Display name of the target `config` runs on (its id when missing); null when local. */
|
||
export function targetName(
|
||
config: Pick<RunConfig, "target">,
|
||
targets: readonly RuntimeRunTarget[] = allRunTargets(),
|
||
): string | null {
|
||
if ("command" in config) config = withRunTarget(config as RunConfig);
|
||
if (!config.target) return null;
|
||
const t = targetOf(config, targets);
|
||
return t ? t.name.trim() || t.id : config.target;
|
||
}
|
||
|
||
/** `name @ target` for configurations on a run target, else the name. */
|
||
export function remoteLabel(
|
||
config: Pick<RunConfig, "name" | "target">,
|
||
targets: readonly RuntimeRunTarget[] = allRunTargets(),
|
||
): string {
|
||
const t = targetName(config, targets);
|
||
return t ? `${config.name} @ ${t}` : config.name;
|
||
}
|
||
|
||
/** `remoteLabel` that re-renders when the run targets change. */
|
||
export function useRemoteLabel(): (config: Pick<RunConfig, "name" | "target">) => string {
|
||
const targets = useRunTargets();
|
||
return (config) => remoteLabel(config, targets);
|
||
}
|
||
|
||
/** `config` running on `target` (empty: this machine, the key is dropped). */
|
||
export function withTarget(config: RunConfig, target: string): RunConfig {
|
||
const { target: _previous, ...rest } = config;
|
||
return target || workflowSettings().run.defaultTarget ? { ...rest, target } : rest;
|
||
}
|
||
|
||
const PYTHON_KINDS = new Set(["python", "pytest", "unittest", "doctest", "django-test", "celery"]);
|
||
|
||
/**
|
||
* The id of the Docker / Compose / WSL run target `config` runs in: its own target, else, for a
|
||
* Python configuration, the project interpreter's (`target:<id>`, as ide-sdk's `run_target` picks
|
||
* it). The paths such a run prints are looked up through `runContainerPath`.
|
||
*/
|
||
export function containerTarget(
|
||
config: RunConfig | null | undefined,
|
||
targets: readonly RuntimeRunTarget[] = allRunTargets(),
|
||
): string | null {
|
||
if (!config) return null;
|
||
const interpreter = validateLanguages(useProject.getState().settings?.languages).python.interpreter.trim();
|
||
const program = config.command.split(/[\\/]/).at(-1) ?? "";
|
||
const python = PYTHON_KINDS.has(config.kind) || /^python[\d.]*(\.exe)?$/.test(program);
|
||
const id =
|
||
config.target ??
|
||
(python && interpreter.startsWith("target:") ? interpreter.slice("target:".length).trim() : null) ??
|
||
withRunTarget(config).target;
|
||
const target = id ? targetOf({ target: id }, targets) : undefined;
|
||
return target && (inContainer(target) || target.kind === "wsl") ? target.id : null;
|
||
}
|
||
|
||
/**
|
||
* A Python configuration, or a Node.js one (a Node.js configuration or a `.js` / `.ts` file run; not
|
||
* npm scripts, whose child processes would need js-debug's auto attach there), on an SSH, Docker
|
||
* or Compose target, or a PHP one in a Docker or Compose target: debugged there (`debugOnTarget`).
|
||
*/
|
||
export function targetDebuggable(config: RunConfig, targets: readonly RuntimeRunTarget[] = allRunTargets()): boolean {
|
||
config = withRunTarget(config);
|
||
const target = targetOf(config, targets);
|
||
if (!target) return false;
|
||
// Xdebug connects from the container to the debugger here; over SSH nothing forwards that way.
|
||
if (config.debugAdapter === "php-debug") return inContainer(target);
|
||
const supported = config.debugAdapter === "debugpy" || (config.debugAdapter === "js-debug" && config.kind === "node");
|
||
return (target.kind === "ssh" || inContainer(target)) && supported;
|
||
}
|
||
|
||
/**
|
||
* Debugging on run targets is supported for Python and Node.js on SSH and container targets: tells
|
||
* so (offering Run) and returns true when `config` can't be debugged where it runs.
|
||
*/
|
||
export function remoteDebugBlocked(config: RunConfig, targets: readonly RuntimeRunTarget[] = allRunTargets()): boolean {
|
||
config = withRunTarget(config);
|
||
if (!config.target || targetDebuggable(config, targets)) return false;
|
||
const target = targetName(config, targets) ?? config.target;
|
||
const where = targetOf(config, targets);
|
||
notifications().notify({
|
||
group: "run",
|
||
level: "warning",
|
||
key: "run.remoteDebug",
|
||
title:
|
||
where && inContainer(where)
|
||
? "Only Python, Node.js and PHP can be debugged in containers"
|
||
: where?.kind === "ssh"
|
||
? "Only Python and Node.js can be debugged on SSH run targets"
|
||
: "Debugging on run targets is not supported",
|
||
body: txTemplate(
|
||
`'${config.name}' runs on ${target}. Run it, or choose “Local machine” under “Run on” to debug it.`,
|
||
),
|
||
actions: [
|
||
{ label: "Run", run: () => void executeAction("run.runConfig", config.id) },
|
||
{ label: "Edit Configuration…", run: () => void executeAction("run.editConfigs", config.id) },
|
||
],
|
||
});
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* Coverage and profiling read their results on this machine: tells so (offering
|
||
* Run) and returns true when `config` runs in a container or a WSL distribution.
|
||
*/
|
||
export function containerExecutorBlocked(
|
||
config: RunConfig,
|
||
targets: readonly RuntimeRunTarget[] = allRunTargets(),
|
||
): boolean {
|
||
const target = targetOf(config, targets);
|
||
if (!target || !(inContainer(target) || target.kind === "wsl")) return false;
|
||
const where = target.name.trim() || target.id;
|
||
const wsl = target.kind === "wsl";
|
||
notifications().notify({
|
||
group: "run",
|
||
level: "warning",
|
||
key: "run.containerExecutor",
|
||
title: wsl
|
||
? "Coverage and profiling are not supported on WSL"
|
||
: "Coverage and profiling are not supported in containers",
|
||
body: wsl
|
||
? txTemplate(`'${config.name}' runs in WSL on ${where}. Run it, or choose “Local machine” under “Run on”.`)
|
||
: txTemplate(
|
||
`'${config.name}' runs in a container on ${where}. Run it, or choose “Local machine” under “Run on”.`,
|
||
),
|
||
actions: [
|
||
{ label: "Run", run: () => void executeAction("run.runConfig", config.id) },
|
||
{ label: "Edit Configuration…", run: () => void executeAction("run.editConfigs", config.id) },
|
||
],
|
||
});
|
||
return true;
|
||
}
|
||
|
||
export type StartRemedy = "trustHost" | "sshSecret" | null;
|
||
|
||
/** What a failed remote `runStart` needs from the user. */
|
||
export function startRemedy(message: string): StartRemedy {
|
||
if (/^Unknown SSH host key/.test(message)) return "trustHost";
|
||
if (/^SSH (password|key passphrase) required|passphrase required|^cannot decrypt SSH key/.test(message))
|
||
return "sshSecret";
|
||
return null;
|
||
}
|
||
|
||
export interface RemedyDeps {
|
||
confirmTrust: (message: string) => Promise<boolean>;
|
||
askSecret: (message: string, save: boolean) => Promise<{ secret: string; save: boolean } | null>;
|
||
trustHost: (sshConfig: string) => Promise<unknown>;
|
||
setSecret: (sshConfig: string, secret: string, save: boolean) => Promise<unknown>;
|
||
targets: () => readonly RuntimeRunTarget[];
|
||
sshConfigs: () => readonly SshConfigValid[];
|
||
}
|
||
|
||
const defaultDeps: RemedyDeps = {
|
||
confirmTrust: (message) =>
|
||
confirm({
|
||
title: "Unknown SSH Host",
|
||
icon: "warning",
|
||
message: `${message}. ${tx("The authenticity of the host cannot be established. Trust it and add the key to ~/.ssh/known_hosts?")}`,
|
||
ok: "Trust and Connect",
|
||
}),
|
||
askSecret: (message, save) => promptSecret(message, save),
|
||
trustHost: (id) => call(commands.sshTrustHost(id)),
|
||
setSecret: (id, secret, save) => commands.sshSetSecret(id, secret, save),
|
||
targets: () => allRunTargets(),
|
||
sshConfigs: () => workflowSettings().ssh.configs,
|
||
};
|
||
|
||
/**
|
||
* Starts a configuration through `start`; when it runs on a target and the SSH
|
||
* connection needs a host key trusted or a secret, asks for it and retries
|
||
* (each remedy at most once). Other errors, and declined remedies, rethrow.
|
||
*/
|
||
export async function startWithRemedies<T>(
|
||
config: Pick<RunConfig, "target">,
|
||
start: () => Promise<T>,
|
||
deps: RemedyDeps = defaultDeps,
|
||
): Promise<T> {
|
||
const used = new Set<StartRemedy>();
|
||
for (;;) {
|
||
try {
|
||
// oxlint-disable-next-line no-await-in-loop -- retry loop: an attempt must finish before its failure can be classified and one remedy applied; `used` tracks tried remedies across iterations
|
||
return await start();
|
||
} catch (e) {
|
||
if (!config.target || !(e instanceof IpcError)) throw e;
|
||
const remedy = startRemedy(e.message);
|
||
const sshConfig = targetOf(config, deps.targets())?.sshConfig;
|
||
if (!remedy || used.has(remedy) || !sshConfig) throw e;
|
||
used.add(remedy);
|
||
if (remedy === "trustHost") {
|
||
// oxlint-disable-next-line no-await-in-loop -- do not trust the host until the user confirms; a declined prompt rethrows the original start error
|
||
if (!(await deps.confirmTrust(e.message))) throw e;
|
||
// oxlint-disable-next-line no-await-in-loop -- the host must be trusted before the next `start` attempt
|
||
await deps.trustHost(sshConfig);
|
||
} else {
|
||
const saved = deps.sshConfigs().find((c) => c.id === sshConfig)?.saveSecret ?? false;
|
||
// oxlint-disable-next-line no-await-in-loop -- the saved flag is read before prompting, and the answer is needed before a secret can be stored
|
||
const answer = await deps.askSecret(e.message, saved);
|
||
if (!answer) throw e;
|
||
// oxlint-disable-next-line no-await-in-loop -- the secret must be stored before the next `start` attempt
|
||
await deps.setSecret(sshConfig, answer.secret, answer.save);
|
||
}
|
||
}
|
||
}
|
||
}
|