Файли
openidea/src/features/debugger/__tests__/launch.test.ts
T
LU XIANGRUIтаwildcatDownstairs e30ae3f6b8 feat(run): add Flask and FastAPI run configurations (#6647)
Flask and FastAPI apps could only run as ordinary scripts. Adds Flask server and FastAPI run configuration types: Flask runs python -m flask run with FLASK_APP / FLASK_DEBUG (read by every Flask version), host, port and extra options; FastAPI runs python -m uvicorn <module:app> with reload, host, port and options. Both use the Python interpreter choice and debug with debugpy; under the debugger Flask runs with --no-debugger, and without subprocess attach the reloader is turned off so breakpoints still stop.

A top-level (or src/) app.py, wsgi.py, main.py or app/main.py assigning Flask(...) or FastAPI(...) to a module-level name gets a detected configuration. The list of Python run kinds is now one function (ide_run::is_python_kind) instead of copies in the host, Docker, process and SDK code.

Closes #6532

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 13:25:10 +08:00

749 рядки
31 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { RunConfig } from "@/platform/ipc";
import { usePlatform } from "@/platform/platformInfo";
import { useProject } from "@/platform/project";
import { useFlutterDevices } from "@/workbench/flutterDevices";
import { isJinjaTemplate } from "../breakpoints/languages";
import { debugUnsupportedReason, launchConfigFor, withEnvFiles } from "../launch";
const ipc = vi.hoisted(() => {
const ok = <T>(data: T) => Promise.resolve({ status: "ok" as const, data });
return { ok, runEnvFiles: vi.fn((_files: string[]) => ok({} as Record<string, string>)) };
});
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
const { ok: _ok, ...cmds } = ipc;
return { ...real, commands: { ...real.commands, ...cmds } };
});
const rc = (p: Partial<RunConfig>): RunConfig => ({
id: "x",
name: "cfg",
kind: "shell",
command: "true",
args: [],
cwd: null,
env: {},
detected: true,
isTest: false,
debugAdapter: null,
...p,
});
const extra = (l: { extraJson: string | null } | null) => JSON.parse(l?.extraJson ?? "{}");
const originalPlatform = usePlatform.getState();
beforeEach(() => {
usePlatform.setState({ os: "linux" });
vi.clearAllMocks();
});
afterEach(() => usePlatform.setState(originalPlatform));
describe("Flutter device", () => {
const device = { platformType: null, category: "desktop", emulator: false, unavailable: null };
const flutter = (p: Partial<RunConfig> = {}) =>
rc({ kind: "flutter", command: "flutter", args: ["run", "-t", "lib/main.dart"], debugAdapter: "dart", ...p });
beforeEach(() =>
useFlutterDevices.setState({
preferred: "macos",
list: {
status: { kind: "ready" },
devices: [
{ ...device, id: "chrome", name: "Chrome" },
{ ...device, id: "macos", name: "macOS" },
],
},
}),
);
afterEach(() => useFlutterDevices.setState({ preferred: null, list: null }));
it("debugs on the toolbar's device, through the backend's Flutter check", () => {
const launch = launchConfigFor(flutter({ args: ["run", "-t", "lib/app.dart", "--flavor", "dev"] }), "/p");
expect(launch?.program).toBe("/p/lib/app.dart");
expect(extra(launch)).toEqual({ toolArgs: ["--flavor", "dev"], __flutterDevice: "macos" });
});
it("takes the entry point from the target option, not from any argument ending in .dart", () => {
const debug = (...args: string[]) => launchConfigFor(flutter({ args: ["run", ...args] }), "/p");
expect(debug("--target=lib/app.dart")?.program).toBe("/p/lib/app.dart");
expect(debug("--target", "lib/app.dart")?.program).toBe("/p/lib/app.dart");
const define = debug("-t", "lib/main.dart", "--dart-define=CFG=dev.dart");
expect(define?.program).toBe("/p/lib/main.dart");
expect(extra(define).toolArgs).toEqual(["--dart-define=CFG=dev.dart"]);
expect(debug("--dart-define=CFG=dev.dart")?.program).toBe("/p/lib/main.dart");
});
it("keeps a file-scoped Dart configuration's last .dart argument as its entry point", () => {
const file = rc({ kind: "dart", command: "dart", args: ["run", "bin/tool.dart"], debugAdapter: "dart" });
expect(launchConfigFor(file, "/p")?.program).toBe("/p/bin/tool.dart");
});
it("reads the pinned device from every spelling of the option", () => {
const pinned = flutter({ args: ["run", "--device-id=chrome"] });
expect(extra(launchConfigFor(pinned, "/p"))).toEqual({ __flutterDevice: "chrome" });
});
it("prefers the device the configuration pins", () => {
const pinned = flutter({ args: ["run", "-t", "lib/main.dart", "-d", "chrome"] });
expect(extra(launchConfigFor(pinned, "/p"))).toEqual({ __flutterDevice: "chrome" });
});
it("falls back to a connected device when the chosen one is gone", () => {
useFlutterDevices.setState({ preferred: "R5C" });
expect(extra(launchConfigFor(flutter(), "/p")).__flutterDevice).toBe("chrome");
});
it("attaches on the pinned device, else the toolbar's", () => {
const attach = (params: Record<string, string>) =>
extra(launchConfigFor(rc({ debugAdapter: "dart", params: { request: "attach", ...params } }), "/p"));
expect(attach({}).toolArgs).toEqual(["-d", "macos"]);
expect(attach({ device: "chrome" }).toolArgs).toEqual(["-d", "chrome"]);
});
it("debugs the build mode and flavor of the command line", () => {
const launch = launchConfigFor(
flutter({ args: ["run", "-t", "lib/main.dart", "--profile", "--flavor=dev"] }),
"/p",
);
expect(extra(launch).toolArgs).toEqual(["--profile", "--flavor=dev"]);
});
it("attaches a Flutter configuration with its entry point and attach args", () => {
const config = flutter({
args: ["run", "-t", "lib/dev.dart", "--flavor=dev"],
params: { entry: "lib/dev.dart", flavor: "dev", attachArgs: "--debug-port 9000", request: "attach" },
});
const launch = launchConfigFor(config, "/p");
expect(launch).toMatchObject({ request: "attach", program: "/p/lib/dev.dart" });
expect(extra(launch)).toEqual({ __flutter: true, toolArgs: ["-d", "macos", "--debug-port", "9000"] });
});
it("leaves the choice to flutter without a device list", () => {
useFlutterDevices.setState({ list: null });
expect(launchConfigFor(flutter(), "/p")?.extraJson).toBeNull();
});
});
describe("run config → launch config", () => {
it("returns null without an adapter", () => {
expect(launchConfigFor(rc({}), "/p")).toBeNull();
});
it("php-debug: the script runs on the PHP from Settings with Xdebug's debug mode, on a free port", () => {
const xdebug = ["-dxdebug.mode=debug", "-dxdebug.start_with_request=yes", "-dxdebug.client_port=${port}"];
// `artisan serve` with PHP at Default: the script is a file of the project.
const serve = launchConfigFor(
rc({ kind: "php", command: "php", args: ["artisan", "serve"], debugAdapter: "php-debug" }),
"/app",
"/opt/[email protected]/bin/php",
)!;
expect(serve).toMatchObject({ adapter: "php-debug", program: "/app/artisan", args: ["serve"], cwd: "/app" });
expect(extra(serve)).toEqual({ port: 0, runtimeArgs: xdebug, runtimeExecutable: "/opt/[email protected]/bin/php" });
// The PHP the command names wins, and its options stay with it.
const file = launchConfigFor(
rc({
kind: "php",
command: "/usr/bin/php8.4",
args: ["-dmemory_limit=1G", "src/a.php", "x"],
debugAdapter: "php-debug",
}),
"/app",
"/opt/[email protected]/bin/php",
)!;
expect(file).toMatchObject({ program: "/app/src/a.php", args: ["x"] });
expect(extra(file)).toEqual({
port: 0,
runtimeArgs: [...xdebug, "-dmemory_limit=1G"],
runtimeExecutable: "/usr/bin/php8.4",
});
// PHPUnit runs its script directly; without a PHP in Settings, php-debug uses the `php` on PATH.
const unit = launchConfigFor(
rc({ kind: "phpunit", command: "vendor/bin/phpunit", args: ["--filter", "testA"], debugAdapter: "php-debug" }),
"/app",
)!;
expect(unit).toMatchObject({ program: "/app/vendor/bin/phpunit", args: ["--filter", "testA"] });
expect(extra(unit)).toEqual({ port: 0, runtimeArgs: xdebug });
});
it("cargo run builds the binary through codelldb's cargo integration", () => {
const l = launchConfigFor(
rc({
kind: "cargo",
command: "cargo",
args: ["run", "-p", "app", "--bin", "app", "--", "--port", "1"],
debugAdapter: "codelldb",
}),
"/p",
)!;
expect(l.adapter).toBe("codelldb");
expect(l.program).toBeNull();
expect(l.args).toEqual(["--port", "1"]);
expect(l.cwd).toBe("/p");
expect(extra(l)).toEqual({
cargo: { args: ["build", "-p", "app", "--bin", "app"], filter: { name: "app", kind: "bin" } },
sourceLanguages: ["rust"],
});
});
it("cargo run under lldb-dap builds the same way and passes the environment as strings", () => {
const l = launchConfigFor(
rc({ kind: "cargo", command: "cargo", args: ["run"], env: { RUST_LOG: "debug" }, debugAdapter: "lldb-dap" }),
"/p",
)!;
expect(l.adapter).toBe("lldb-dap");
expect(extra(l)).toEqual({ cargo: { args: ["build"], filter: { kind: "bin" } }, env: ["RUST_LOG=debug"] });
});
it("cargo test builds test binaries without running", () => {
const l = launchConfigFor(
rc({ kind: "cargo", command: "cargo", args: ["test", "--workspace"], debugAdapter: "codelldb" }),
"/p",
)!;
expect(extra(l).cargo).toEqual({ args: ["test", "--no-run", "--workspace"] });
});
it("debugpy: script, module, console script, venv interpreter", () => {
const script = launchConfigFor(
rc({ kind: "python", command: ".venv/bin/python", args: ["manage.py", "runserver"], debugAdapter: "debugpy" }),
"/p",
)!;
expect(script.program).toBe("/p/manage.py");
expect(script.args).toEqual(["runserver"]);
expect(extra(script)).toMatchObject({ python: "/p/.venv/bin/python", django: true, console: "integratedTerminal" });
const mod = launchConfigFor(
rc({ kind: "python", command: "python3", args: ["-m", "app", "-v"], debugAdapter: "debugpy" }),
"/p",
)!;
expect(mod.program).toBeNull();
expect(mod.args).toEqual(["-v"]);
expect(extra(mod)).toMatchObject({ module: "app", python: "python3" });
const pytest = launchConfigFor(
rc({ kind: "pytest", command: ".venv/bin/pytest", args: ["-x"], debugAdapter: "debugpy" }),
"/p",
)!;
expect(extra(pytest)).toMatchObject({ module: "pytest", python: "/p/.venv/bin/python" });
expect(pytest.args).toEqual(["-x"]);
const globalPytest = launchConfigFor(rc({ kind: "pytest", command: "pytest", debugAdapter: "debugpy" }), "/p")!;
expect(extra(globalPytest).python).toBe("python3");
});
it("debugpy: Jinja2 templates for Flask, or when a template has a breakpoint (#6371)", () => {
const debug = (command: string, args: string[], templates = { django: false, jinja: false }) =>
extra(launchConfigFor(rc({ kind: "python", command, args, debugAdapter: "debugpy" }), "/p", null, templates));
expect(debug("uv", ["run", "flask", "--app", "app", "run"])).toMatchObject({ module: "flask", jinja: true });
expect(debug("python3", ["-m", "flask", "run"]).jinja).toBe(true);
expect(debug("python3", ["app.py"]).jinja).toBeUndefined();
const jinja = { django: false, jinja: true };
expect(debug("python3", ["app.py"], jinja).jinja).toBe(true);
// Django renders its own templates: `jinja` would make debugpy read them as Jinja2.
expect(debug("python3", ["manage.py", "runserver"], jinja)).toMatchObject({ django: true });
expect(debug("python3", ["manage.py", "runserver"], jinja).jinja).toBeUndefined();
expect(debug("uv", ["run", "django-admin", "runserver"], jinja).jinja).toBeUndefined();
// A Django Template's breakpoint: `django` however the server starts.
const asgi = debug("uv", ["run", "uvicorn", "mysite.asgi:application"], { django: true, jinja: false });
expect(asgi).toMatchObject({ module: "uvicorn", django: true });
expect(asgi.jinja).toBeUndefined();
});
it("debugpy: Flask without the in-browser debugger, and no reloader unless subprocesses are attached", () => {
const debug = (kind: string, args: string[]) =>
launchConfigFor(rc({ kind, command: "python3", args, debugAdapter: "debugpy" }), "/p")!.args;
expect(debug("flask", ["-m", "flask", "run", "--port", "5001"])).toEqual([
"run",
"--no-debugger",
"--port",
"5001",
]);
expect(debug("python", ["-m", "flask", "--app", "app", "run", "--no-debugger"])).toEqual([
"--app",
"app",
"run",
"--no-debugger",
]);
expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app", "--reload"]);
const settings = useProject.getState().settings;
try {
useProject.setState({ settings: { ...settings, debugger: { python: { attachSubprocess: false } } } as never });
expect(debug("flask", ["-m", "flask", "run"])).toEqual(["run", "--no-debugger", "--no-reload"]);
expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app"]);
} finally {
useProject.setState({ settings });
}
});
it("Jinja2 templates: their extensions anywhere, markup or text in a templates folder", () => {
for (const p of [
"/p/mail/welcome.j2",
"/p/x/page.JINJA",
"/p/a.jinja2",
"/p/templates/base.html",
"/p/app/templates/x/y.txt",
])
expect(isJinjaTemplate(p), p).toBe(true);
for (const p of ["/p/static/index.html", "/p/templates.py", "/p/templates/style.css", "/p/app.py"])
expect(isJinjaTemplate(p), p).toBe(false);
// A file whose Override File Type is Jinja2 Template.
expect(isJinjaTemplate("/p/static/index.html", "jinja")).toBe(true);
expect(isJinjaTemplate("/p/static/index.html", "jinja-html")).toBe(true);
});
it("debugpy: uv / poetry run the debuggee in the project's environment", () => {
const debug = (command: string, args: string[]) =>
launchConfigFor(rc({ kind: "python", command, args, debugAdapter: "debugpy" }), "/p")!;
const script = debug("uv", ["run", "python", "main.py", "-v"]);
expect(script.program).toBe("/p/main.py");
expect(script.args).toEqual(["-v"]);
expect(extra(script)).toMatchObject({ python: ["uv", "run", "python"], debugLauncherPython: "python3" });
expect(extra(script).module).toBeUndefined();
const mod = debug("poetry", ["run", "python3", "-m", "app"]);
expect(extra(mod)).toMatchObject({ module: "app", python: ["poetry", "run", "python3"] });
const tool = debug("uv", ["run", "pytest", "-x"]);
expect(extra(tool)).toMatchObject({ module: "pytest", python: ["uv", "run", "python"] });
expect(tool.args).toEqual(["-x"]);
const django = debug("pdm", ["run", "python", "manage.py", "runserver"]);
expect(extra(django)).toMatchObject({ django: true, python: ["pdm", "run", "python"] });
// Django templates' breakpoints need `django: true` however Django is started (#6370).
expect(extra(debug("uv", ["run", "python", "-m", "django", "runserver"]))).toMatchObject({
module: "django",
django: true,
});
const admin = debug("uv", ["run", "django-admin", "runserver"]);
expect(extra(admin)).toMatchObject({ module: "django", django: true });
expect(admin.args).toEqual(["runserver"]);
expect(extra(debug("python3", ["-m", "app"])).django).toBeUndefined();
// The manager's own options stay with it; `uv run app.py` / `-m app` start Python directly (#5874).
const withOptions = debug("uv", ["run", "--with", "rich", "--no-sync", "main.py", "-v"]);
expect(withOptions.program).toBe("/p/main.py");
expect(withOptions.args).toEqual(["-v"]);
expect(extra(withOptions)).toMatchObject({ python: ["uv", "run", "--with", "rich", "--no-sync", "python"] });
expect(extra(withOptions).module).toBeUndefined();
const member = debug("uv", ["run", "--project", "member", "--module", "app", "x"]);
expect(extra(member)).toMatchObject({ module: "app", python: ["uv", "run", "--project", "member", "python"] });
expect(member.args).toEqual(["x"]);
const toolWithOptions = debug("uv", ["run", "--frozen", "pytest", "-x"]);
expect(extra(toolWithOptions)).toMatchObject({ module: "pytest", python: ["uv", "run", "--frozen", "python"] });
expect(toolWithOptions.args).toEqual(["-x"]);
});
it("debugpy: Windows falls back to python, not the python3 Store alias", () => {
usePlatform.setState({ os: "windows" });
const globalPytest = launchConfigFor(rc({ kind: "pytest", command: "pytest", debugAdapter: "debugpy" }), "/p")!;
expect(extra(globalPytest).python).toBe("python");
const uv = launchConfigFor(
rc({ kind: "python", command: "uv", args: ["run", "python", "main.py"], debugAdapter: "debugpy" }),
"/p",
)!;
expect(extra(uv)).toMatchObject({ python: ["uv", "run", "python"], debugLauncherPython: "python" });
});
it("debugpy: a Windows venv interpreter or tool path debugs with that interpreter", () => {
usePlatform.setState({ os: "windows" });
const venv = "C:\\work\\app\\.venv\\Scripts\\";
const script = launchConfigFor(
rc({ kind: "python", command: `${venv}python.exe`, args: ["main.py"], debugAdapter: "debugpy" }),
"C:/work/app",
)!;
expect(extra(script).python).toBe(`${venv}python.exe`);
expect(extra(script).module).toBeUndefined();
expect(script.program).toBe("C:/work/app/main.py");
const pytest = launchConfigFor(
rc({ kind: "pytest", command: `${venv}pytest.exe`, args: ["-x"], debugAdapter: "debugpy" }),
"C:/work/app",
)!;
expect(extra(pytest)).toMatchObject({ module: "pytest", python: `${venv}python.exe` });
});
it("js-debug: npm scripts run through the package manager, node scripts directly", () => {
const npm = launchConfigFor(
rc({ kind: "npm", command: "pnpm", args: ["run", "dev"], cwd: "web", debugAdapter: "js-debug" }),
"/p",
)!;
expect(npm.cwd).toBe("/p/web");
expect(npm.program).toBeNull();
expect(extra(npm)).toMatchObject({ type: "pwa-node", runtimeExecutable: "pnpm", runtimeArgs: ["run", "dev"] });
const node = launchConfigFor(rc({ command: "node", args: ["server.js", "--x"], debugAdapter: "js-debug" }), "/p")!;
expect(node.program).toBe("/p/server.js");
expect(node.args).toEqual(["--x"]);
expect(extra(node).runtimeExecutable).toBeUndefined();
// A located Node.js (e.g. 22.6+ for TypeScript) is used instead of PATH's.
const ts = launchConfigFor(
rc({ command: "/opt/node22/bin/node", args: ["a.ts"], debugAdapter: "js-debug" }),
"/p",
)!;
expect(ts.program).toBe("/p/a.ts");
expect(extra(ts).runtimeExecutable).toBe("/opt/node22/bin/node");
});
it("js-debug: TypeScript through a runner or type-stripping flag runs as the runtime", () => {
const flag = launchConfigFor(
rc({
kind: "node",
command: "/bin/node",
args: ["--experimental-strip-types", "a.ts"],
debugAdapter: "js-debug",
}),
"/p",
)!;
expect(flag.program).toBeNull();
expect(extra(flag)).toMatchObject({
runtimeExecutable: "/bin/node",
runtimeArgs: ["--experimental-strip-types", "a.ts"],
});
const tsx = launchConfigFor(
rc({ kind: "node", command: "npx", args: ["tsx", "a.ts"], debugAdapter: "js-debug" }),
"/p",
)!;
expect(extra(tsx)).toMatchObject({ runtimeExecutable: "npx", runtimeArgs: ["tsx", "a.ts"] });
});
it("deno: waits on its inspector at a free port that js-debug attaches to", () => {
const l = launchConfigFor(
rc({
kind: "deno",
command: "/bin/deno",
args: ["run", "-A", "--inspect", "main.ts", "x"],
debugAdapter: "js-debug",
}),
"/p",
)!;
expect(l.adapter).toBe("js-debug");
expect(l.program).toBeNull();
expect(extra(l)).toMatchObject({
type: "pwa-node",
runtimeExecutable: "/bin/deno",
runtimeArgs: ["run", "--inspect-wait=127.0.0.1:${freePort}", "-A", "main.ts", "x"],
attachSimplePort: "${freePort}",
});
const test = launchConfigFor(
rc({ kind: "deno", command: "deno", args: ["test"], debugAdapter: "js-debug" }),
"/p",
)!;
expect(extra(test).runtimeArgs).toEqual(["test", "--inspect-wait=127.0.0.1:${freePort}"]);
});
it("deno task and Bun can't be debugged, with a reason", () => {
const task = rc({ kind: "deno", command: "deno", args: ["task", "dev"], debugAdapter: "js-debug" });
expect(launchConfigFor(task, "/p")).toBeNull();
expect(debugUnsupportedReason(task)).toContain("deno run");
const bun = rc({ kind: "bun", command: "/bin/bun", args: ["run", "a.ts"], debugAdapter: "js-debug" });
expect(launchConfigFor(bun, "/p")).toBeNull();
expect(debugUnsupportedReason(bun)).toContain("WebKit");
// `bun run <script>` of package.json may start Node, which js-debug follows.
const script = rc({ kind: "npm", command: "bun", args: ["run", "dev"], debugAdapter: "js-debug" });
expect(debugUnsupportedReason(script)).toBeNull();
expect(launchConfigFor(script, "/p")).not.toBeNull();
});
it("never hands a Java command to java-debug as a program to start", () => {
const tomcat = rc({
name: "tomcat9",
command: "C:\\apache-tomcat-9.0.98\\bin\\catalina.bat",
args: ["run"],
debugAdapter: "java-debug",
});
const gradle = rc({ kind: "gradle", command: "./gradlew", args: ["build"], debugAdapter: "java-debug" });
for (const c of [tomcat, gradle]) {
expect(launchConfigFor(c, "/p")).toBeNull();
expect(debugUnsupportedReason(c)).toContain("Remote JVM Debug");
}
});
it("delve: debug mode for run, test mode for test", () => {
const run = launchConfigFor(rc({ kind: "go", command: "go", args: ["run", "."], debugAdapter: "delve" }), "/p")!;
expect(run.program).toBe("/p");
expect(extra(run)).toMatchObject({
mode: "debug",
__goTargets: ["."],
__goExecutable: "go",
__goToolArgs: [],
__goUseTags: true,
});
const sub = launchConfigFor(
rc({ kind: "go", command: "go", args: ["run", "./cmd/api"], debugAdapter: "delve" }),
"/p",
)!;
expect(sub.program).toBe("/p/cmd/api");
const test = launchConfigFor(
rc({ kind: "go", command: "go", args: ["test", "./..."], debugAdapter: "delve" }),
"/p",
)!;
expect(extra(test)).toMatchObject({ mode: "test", __goTargets: ["./..."], __goToolArgs: [], __goUseTags: true });
expect(test.program).toBe("/p/...");
// A gutter test / subtest: the package, and `-run` for the test binary.
const one = launchConfigFor(
rc({
kind: "go",
command: "go",
args: ["test", "./calc", "-run", "^TestSub$/^zero_case$"],
debugAdapter: "delve",
}),
"/p",
)!;
expect(one.program).toBe("/p/calc");
expect(one.args).toEqual(["-test.run", "^TestSub$/^zero_case$"]);
const eq = launchConfigFor(
rc({ kind: "go", command: "go", args: ["test", "-run=^TestA$", "."], debugAdapter: "delve" }),
"/p",
)!;
expect(eq.program).toBe("/p");
expect(eq.args).toEqual(["-test.run", "^TestA$"]);
});
it("other adapters launch the command as the program", () => {
const l = launchConfigFor(
rc({ command: "./bin/app", args: ["a"], env: { K: "v" }, debugAdapter: "netcoredbg" }),
"/p",
)!;
expect(l).toMatchObject({
adapter: "netcoredbg",
program: "/p/bin/app",
args: ["a"],
env: { K: "v" },
request: "launch",
});
});
it("native programs under CodeLLDB / lldb-dap / GDB: program, args, cwd, env", () => {
const run = rc({ command: "./build/app", args: ["-v"], cwd: "c", env: { K: "v" } });
const lldb = launchConfigFor({ ...run, debugAdapter: "codelldb" }, "/p")!;
expect(lldb).toMatchObject({
adapter: "codelldb",
program: "/p/c/build/app",
args: ["-v"],
cwd: "/p/c",
env: { K: "v" },
});
expect(lldb.extraJson).toBeNull();
const dap = launchConfigFor({ ...run, debugAdapter: "lldb-dap" }, "/p")!;
expect(dap).toMatchObject({ adapter: "lldb-dap", program: "/p/c/build/app" });
// lldb-dap takes `KEY=VALUE` strings.
expect(extra(dap)).toEqual({ env: ["K=v"] });
// GDB's DAP launch takes the environment as an object, like the default flow.
const gdb = launchConfigFor({ ...run, debugAdapter: "gdb" }, "/p")!;
expect(gdb).toMatchObject({
adapter: "gdb",
program: "/p/c/build/app",
args: ["-v"],
cwd: "/p/c",
env: { K: "v" },
});
expect(gdb.extraJson).toBeNull();
});
it.each(["native", "cmake-application"])(
"maps %s CMake debug-info paths to the editor's canonical source root",
(kind) => {
const config = rc({
kind,
command: "/private/tmp/project/build/app",
debugAdapter: "lldb-dap",
env: { K: "v" },
params: { cmakeSourceRoot: "/tmp/project", cmakeCanonicalSourceRoot: "/private/tmp/project" },
});
expect(extra(launchConfigFor(config, "/private/tmp/project")!)).toEqual({
env: ["K=v"],
sourceMap: [["/tmp/project", "/private/tmp/project"]],
});
},
);
it("Zig under CodeLLDB: no source-language mode, so evaluation stays LLDB's own", () => {
const exe = launchConfigFor(
rc({ kind: "zig-application", command: "./zig-out/bin/app", debugAdapter: "codelldb" }),
"/p",
)!;
expect(exe.adapter).toBe("codelldb");
expect(extra(exe)).toEqual({ sourceLanguages: [] });
const single = launchConfigFor(
rc({
kind: "native",
command: "/tmp/openidea-run/calc-1",
debugAdapter: "codelldb",
params: { language: "zig" },
}),
"/p",
)!;
expect(extra(single)).toEqual({ sourceLanguages: [] });
// Rust-compiled binaries keep CodeLLDB's default (Rust) source mode.
const rust = launchConfigFor(
rc({ kind: "native", command: "/tmp/openidea-run/app-1", debugAdapter: "codelldb" }),
"/p",
)!;
expect(rust.extraJson).toBeNull();
});
it("Godot: the editor's adapter plays the main scene or the chosen one of the project", () => {
const project = rc({
kind: "godot",
command: "/Apps/Godot",
args: ["--path", "."],
cwd: "game",
debugAdapter: "godot",
});
const main = launchConfigFor(project, "/p")!;
expect(main).toMatchObject({ adapter: "godot", request: "launch", cwd: "/p/game", program: null });
expect(extra(main)).toEqual({ project: "/p/game", scene: "main" });
const scene = launchConfigFor({ ...project, args: ["--path", ".", "res://levels/one.tscn"] }, "/p")!;
expect(extra(scene)).toEqual({ project: "/p/game", scene: "res://levels/one.tscn" });
const relative = launchConfigFor({ ...project, cwd: null, args: ["--path", "game", "menu.tscn"] }, "/p")!;
expect(extra(relative)).toEqual({ project: "/p/game", scene: "res://menu.tscn" });
});
});
describe("Go build configuration fields", () => {
it("separates split build flags from program args for legacy run and test configs", () => {
for (const sub of ["run", "test"]) {
const launch = launchConfigFor(
rc({
kind: "go",
command: "go",
args: [sub, "-tags", "demo", "-gcflags", "all=-N -l", "./cmd/app", "--", "hello"],
debugAdapter: "delve",
}),
"/p",
)!;
expect(launch.program).toBe("/p/cmd/app");
expect(launch.args).toEqual(["hello"]);
expect(JSON.parse(launch.extraJson!).buildFlags).toBe("-tags demo -gcflags 'all=-N -l'");
}
});
it("uses the selected module for building and the working directory for execution", () => {
const launch = launchConfigFor(
rc({
kind: "go",
command: "go",
args: ["build", "."],
cwd: "runtime",
debugAdapter: "delve",
params: {
goKind: "build",
scope: "directory",
target: "cmd/app",
module: "backend",
toolArgs: "-tags demo",
programArgs: 'hello "two words"',
useTags: "true",
},
}),
"/p",
)!;
expect(launch.program).toBe("/p/backend/cmd/app");
expect(launch.cwd).toBe("/p/runtime");
expect(launch.args).toEqual(["hello", "two words"]);
expect(JSON.parse(launch.extraJson!)).toMatchObject({
mode: "debug",
__goToolArgs: ["-tags", "demo"],
__goUseTags: true,
});
});
});
it("netcoredbg launches the evaluated DLL with resolved profile arguments, env and cwd", () => {
const c = rc({
kind: "dotnet",
command: "dotnet",
debugAdapter: "netcoredbg",
cwd: "/repo/work",
env: { ASPNETCORE_URLS: "http://localhost:5165", DEMO: "override" },
params: { targetPath: "/repo/custom/Renamed.dll", programArgs: "--name 'hello world'", profileArgs: "--profile" },
});
expect(launchConfigFor(c, "/repo")).toMatchObject({
adapter: "netcoredbg",
program: "/repo/custom/Renamed.dll",
args: ["--name", "hello world"],
cwd: "/repo/work",
env: c.env,
});
expect(launchConfigFor({ ...c, params: { ...c.params, programArgs: "" } }, "/repo")?.args).toEqual(["--profile"]);
expect(launchConfigFor({ ...c, params: {} }, "/repo")).toBeNull();
});
describe("Go debug target resolution inputs", () => {
it("preserves import paths and multiple source files for Go to resolve", () => {
const imported = launchConfigFor(
rc({ kind: "go", command: "/sdk/bin/go", args: ["test", "example.com/app/calc"], debugAdapter: "delve" }),
"/p",
)!;
expect(imported.program).toBe("example.com/app/calc");
expect(extra(imported)).toMatchObject({
__goTargets: ["example.com/app/calc"],
__goExecutable: "/sdk/bin/go",
__goBuildDir: "/p",
});
const files = launchConfigFor(
rc({ kind: "go", command: "go", args: ["run", "main.go", "helper.go", "hello"], debugAdapter: "delve" }),
"/p",
)!;
expect(files.args).toEqual(["hello"]);
expect(extra(files).__goTargets).toEqual(["main.go", "helper.go"]);
const packages = launchConfigFor(
rc({ kind: "go", command: "go", args: ["build", "./a", "./b"], debugAdapter: "delve" }),
"/p",
)!;
expect(extra(packages).__goTargets).toEqual(["./a", "./b"]);
});
it("keeps a typed recursive package target and the module's selected compiler", () => {
const launch = launchConfigFor(
rc({
kind: "go",
command: "./sdk/bin/go",
args: ["test", "./..."],
debugAdapter: "delve",
params: { goKind: "test", scope: "package", target: "./...", module: "backend" },
}),
"/p",
)!;
expect(extra(launch)).toMatchObject({
__goTargets: ["./..."],
__goBuildDir: "/p/backend",
__goExecutable: "/p/backend/sdk/bin/go",
});
});
});
it("raw and typed Go debug configurations inherit custom tags unless explicitly disabled", () => {
for (const typed of [false, true]) {
for (const useTags of [undefined, "true", "false"]) {
const params = {
...(typed ? { goKind: "test", scope: "package", target: "." } : {}),
...(useTags === undefined ? {} : { useTags }),
};
const launch = launchConfigFor(
rc({ kind: "go", command: "go", args: ["test", "-tags=explicit", "."], debugAdapter: "delve", params }),
"/p",
)!;
expect(extra(launch).__goUseTags).toBe(useTags !== "false");
}
}
});
describe("withEnvFiles", () => {
it("leaves configurations without .env files alone (no backend call)", async () => {
const config = rc({ env: { A: "1" } });
await expect(withEnvFiles(config, "/p")).resolves.toBe(config);
expect(ipc.runEnvFiles).not.toHaveBeenCalled();
});
it("loads .env files under the explicit variables, resolved against the project root", async () => {
ipc.runEnvFiles.mockResolvedValueOnce({ status: "ok", data: { FROM_FILE: "file", A: "file" } });
const out = await withEnvFiles(rc({ env: { A: "explicit" }, envFiles: [".env", "env/local.env"] }), "/p");
expect(ipc.runEnvFiles).toHaveBeenCalledWith(["/p/.env", "/p/env/local.env"]);
expect(out.env).toEqual({ FROM_FILE: "file", A: "explicit" });
});
});